[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#68171

X-original-commit: f4cc0579a3e0ee9c117c7dd0f6090edc6e94384d
Signed-off-by: Olivier Dony (odo) <odo@openerp.com>
Signed-off-by: William André (wan) <wan@odoo.com>
This commit is contained in:
wan
2021-03-19 19:41:02 +00:00
parent ec2174f683
commit 2fcfa2cc18
+9 -3
View File
@@ -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())