From f4cc0579a3e0ee9c117c7dd0f6090edc6e94384d Mon Sep 17 00:00:00 2001 From: wan Date: Thu, 4 Mar 2021 16:59:36 +0000 Subject: [FIX] tools: support malformed more malformed pdf We do not want to fail ever if loading the attachment fails. We still log a warning in every case though. Some malformed PDFs lead to seeking the wrong bytes while looking for some parts, raising a ValueError closes odoo/odoo#67283 Signed-off-by: Olivier Dony (odo) --- odoo/tools/pdf.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'odoo') diff --git a/odoo/tools/pdf.py b/odoo/tools/pdf.py index 6929e2684ab..91ceadcb5f0 100644 --- a/odoo/tools/pdf.py +++ b/odoo/tools/pdf.py @@ -85,10 +85,16 @@ class OdooPdfFileReader(PdfFileReader): # If the PDF is owner-encrypted, try to unwrap it by giving it an empty user password. self.decrypt('') - if not self.trailer["/Root"].get("/Names", {}).get("/EmbeddedFiles", {}).get("/Names"): + try: + file_path = self.trailer["/Root"].get("/Names", {}).get("/EmbeddedFiles", {}).get("/Names") + except Exception: + # malformed pdf (i.e. invalid xref page) return [] - for i in range(0, len(self.trailer["/Root"]["/Names"]["/EmbeddedFiles"]["/Names"]), 2): - attachment = self.trailer["/Root"]["/Names"]["/EmbeddedFiles"]["/Names"][i+1].getObject() + + if not file_path: + return [] + for i in range(0, len(file_path), 2): + attachment = file_path[i+1].getObject() yield (attachment["/F"], attachment["/EF"]["/F"].getObject().getData()) -- cgit v1.2.3