From bcf9ba01dcb011dfdad8b5754df828e81ff9db61 Mon Sep 17 00:00:00 2001 From: Christophe Monniez Date: Tue, 17 Jan 2023 08:27:32 +0000 Subject: [PATCH] [IMP] base: adapt for PyPDF2 >= 2.0.0 X-original-commit: 8e0564dff20876105295a1efec0bc519f4bd1aeb Part-of: odoo/odoo#113354 --- odoo/addons/base/models/ir_actions_report.py | 8 ++++-- odoo/addons/base/tests/minimal.pdf | Bin 556 -> 691 bytes odoo/tools/pdf.py | 26 ++++++++++++++++--- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/odoo/addons/base/models/ir_actions_report.py b/odoo/addons/base/models/ir_actions_report.py index 296cbd508e2..327d75dfcbe 100644 --- a/odoo/addons/base/models/ir_actions_report.py +++ b/odoo/addons/base/models/ir_actions_report.py @@ -22,13 +22,17 @@ import json from lxml import etree from contextlib import closing from reportlab.graphics.barcode import createBarcodeDrawing -from PyPDF2 import PdfFileWriter, PdfFileReader, utils +from PyPDF2 import PdfFileWriter, PdfFileReader from collections import OrderedDict from collections.abc import Iterable from PIL import Image, ImageFile # Allow truncated images ImageFile.LOAD_TRUNCATED_IMAGES = True +try: + from PyPDF2.errors import PdfReadError +except ImportError: + from PyPDF2.utils import PdfReadError _logger = logging.getLogger(__name__) @@ -615,7 +619,7 @@ class IrActionsReport(models.Model): try: reader = PdfFileReader(stream) writer.appendPagesFromReader(reader) - except utils.PdfReadError: + except PdfReadError: raise UserError(_("Odoo is unable to merge the generated PDFs.")) result_stream = io.BytesIO() streams.append(result_stream) diff --git a/odoo/addons/base/tests/minimal.pdf b/odoo/addons/base/tests/minimal.pdf index 2a73de072da33f2ac53f0be5cc26ac7b6f2e6757..ec69682def7424dbdc1d31a91042e89c24c083d0 100644 GIT binary patch literal 691 zcmah{y>8nu5N@}EbF$y!F(qmJ*aZv$m0bftg2a%$B^hYz?5a@2f}+thbGE!fro2Hi zdCNOw>saIsk|9%ql9Xi|@KAyvb>H20_uYw&L9f5ZKYOI{@Ot}BI53doAEeU(dL+)V zgr-3fpaU^8hF2M*-#-y(S7?#T8AK7mBDE|VTeJI80*!^*a9eim?#g+gfdf6r(h^R< zcbU(CR=KNk>&(`@rWP6tT|(eEMw&8atu4V)&eg<-wLSEu@wgA}u2_`lF#1(xA_qF0 zkG1Q@)-`gf@-8HbjCQ*>G;MTatJjhav6$)a;D0d{vs|MxcP=#ca3WK*q*5ys=j7t1 zB!3@&c_Zet!KeMFraxdG?~;YT^EP~b*^QIygPmYEzF(Yn7a#syk=RUjNnOtPLgAD! z2nky)2!p^2U=iszulZN!6&JorsfDe?zORrY|Pj+`hUb(H8!ENYuWeA8*O|$?vsB4(!Xs0 literal 556 zcmZWm!A`lIp4X)d;^o5x6r7L`{4aFcW1_gV7B$+AC^`iF$H-l 2.0 + # https://pypdf2.readthedocs.io/en/latest/user/migration-1-to-2.html#classes + from PyPDF2 import PdfReader + import PyPDF2 + # monkey patch to discard unused arguments as the old arguments were not discarded in the transitional class + # https://pypdf2.readthedocs.io/en/2.0.0/_modules/PyPDF2/_reader.html#PdfReader + class PdfFileReader(PdfReader): + def __init__(self, *args, **kwargs): + if "strict" not in kwargs and len(args) < 2: + kwargs["strict"] = True # maintain the default + kwargs = {k:v for k, v in kwargs.items() if k in ('strict', 'stream')} + super().__init__(*args, **kwargs) + + PyPDF2.PdfFileReader = PdfFileReader + from PyPDF2 import PdfFileWriter, PdfFileReader + PdfFileWriter._addObject = PdfFileWriter._add_object +except ImportError: + from PyPDF2 import PdfFileWriter, PdfFileReader + +from PyPDF2.generic import DictionaryObject, NameObject, ArrayObject, DecodedStreamObject, NumberObject, createStringObject, ByteStringObject + try: from fontTools.ttLib import TTFont except ImportError: @@ -410,7 +430,7 @@ class OdooPdfFileWriter(PdfFileWriter): DictionaryObject({ NameObject('/CheckSum'): createStringObject(md5(attachment['content']).hexdigest()), NameObject('/ModDate'): createStringObject(datetime.now().strftime(DEFAULT_PDF_DATETIME_FORMAT)), - NameObject('/Size'): NameObject(str(len(attachment['content']))), + NameObject('/Size'): NameObject(f"/{len(attachment['content'])}"), }), }) if attachment.get('subtype'):