From ad1b55b760599df1f5a3f5d4afd7491652ccd5a3 Mon Sep 17 00:00:00 2001 From: "Antoine Dupuis (andu)" Date: Mon, 25 Oct 2021 10:01:45 +0000 Subject: [PATCH] [FIX] base: catch PDFs that fail on PdfFileWriter.write Issue: Our functionality for merging PDFs from multiple vendor bills relies on PyPDF2. It is well-known that PyPDF2 is sometimes unable to manipulate even perfectly normal PDFs. To provide a helpful error message to the user when this happens (i.e. give them the names of the vendor bills corresponding to the offending pdfs), the `_get_unreadeable_pdfs()` function is called right before we try to merge the PDFs in `_merge_pdfs`. This function is meant to identify the offending PDFs and provide them to the user. However, _get_unreadable_pdfs did not notice the problem with 3 of my customer's pdfs, because the error was only triggered once the PdfFileWriter.write function was called in _merge_pdfs. This function, however, is not called in _get_unreadable_pdfs, therefore, it did not notice that anything was wrong. Fix: - Change _get_unreadable_pdfs so that it also makes a call to PdfFileWriter.write - As soon as PdfFileWriter.write fails once, it will continue failing when we append more PDF streams to the PdfFileWriter. I therefore suggest initialising a different PdfFileWriter at each iteration of the for loop in order for an offending PDF to not cause a false positive on subsequent PDFs. closes odoo/odoo#78966 X-original-commit: 0b4efd37376223806e7ff7273f520b2d17337bc8 Signed-off-by: Nicolas Lempereur (nle) Signed-off-by: Antoine Dupuis (andu) --- odoo/addons/base/models/ir_actions_report.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/odoo/addons/base/models/ir_actions_report.py b/odoo/addons/base/models/ir_actions_report.py index 1259476bc38..d8fe7a3aad0 100644 --- a/odoo/addons/base/models/ir_actions_report.py +++ b/odoo/addons/base/models/ir_actions_report.py @@ -710,11 +710,13 @@ class IrActionsReport(models.Model): def _get_unreadable_pdfs(self, streams): unreadable_streams = [] - writer = PdfFileWriter() for stream in streams: + writer = PdfFileWriter() + result_stream = io.BytesIO() try: reader = PdfFileReader(stream) writer.appendPagesFromReader(reader) + writer.write(result_stream) except utils.PdfReadError: unreadable_streams.append(stream)