From b1d19c61b1e3318be7b940fda6abfb3591c90c7d Mon Sep 17 00:00:00 2001 From: "Benjamin Frantzen (bfr)" Date: Wed, 3 Jun 2020 08:52:25 +0000 Subject: [PATCH] [FIX] tools.pdf: do not crash when PDF has no attachments Depending on the PDF version and the presence of embedded files, the PDF document catalog `trailer["/Root"]` may not have any Name Dictionary `["/Names"]`, and the latter may not have any `EmbeddedFiles` entry. In that case `getAttachments()` should return an empty list rather than raising a `KeyError`. closes odoo/odoo#52312 Ref: Section 7.7.2, 7.7.4 and 7.11.4 of [PDF 1.7 spec](https://www.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/PDF32000_2008.pdf) Signed-off-by: Olivier Dony (odo) --- odoo/tools/pdf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/odoo/tools/pdf.py b/odoo/tools/pdf.py index 9003ad64789..8e317fa2bde 100644 --- a/odoo/tools/pdf.py +++ b/odoo/tools/pdf.py @@ -59,7 +59,7 @@ class OdooPdfFileReader(PdfFileReader): # OVERRIDE of PdfFileReader to add the management of multiple embedded files. def getAttachments(self): - if "/Names" not in self.trailer["/Root"]: + if not self.trailer["/Root"].get("/Names", {}).get("/EmbeddedFiles", {}).get("/Names"): return [] for i in range(0, len(self.trailer["/Root"]["/Names"]["/EmbeddedFiles"]["/Names"]), 2): attachment = self.trailer["/Root"]["/Names"]["/EmbeddedFiles"]["/Names"][i+1].getObject()