[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) <odo@openerp.com>
This commit is contained in:
Benjamin Frantzen (bfr)
2020-06-03 14:08:37 +00:00
parent 42a424db20
commit b1d19c61b1
+1 -1
View File
@@ -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()