From 2fcfa2cc189e6786697c067cbfd26a843b90b3b7 Mon Sep 17 00:00:00 2001 From: wan Date: Thu, 4 Mar 2021 16:59:36 +0000 Subject: [PATCH] [FIX] tools: support malformed more malformed pdf MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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#68171 X-original-commit: f4cc0579a3e0ee9c117c7dd0f6090edc6e94384d Signed-off-by: Olivier Dony (odo) Signed-off-by: William André (wan) --- odoo/tools/pdf.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) 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())