From 2dbe39aed5361341cdc87607e671ef19234e0196 Mon Sep 17 00:00:00 2001
From: "Antoine Dupuis (andu)" <andu@odoo.com>
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#78955

X-original-commit: 0b4efd37376223806e7ff7273f520b2d17337bc8
Signed-off-by: Nicolas Lempereur (nle) <nle@odoo.com>
Signed-off-by: Antoine Dupuis (andu) <andu@odoo.com>
---
 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 b5d68d330549..c4b79741b6ff 100644
--- a/odoo/addons/base/models/ir_actions_report.py
+++ b/odoo/addons/base/models/ir_actions_report.py
@@ -692,11 +692,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)
 
-- 
GitLab