[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:
+1
-1
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user