Skip to content
Snippets Groups Projects

:sparkles: Compatibility with pypdf 3+

Merged Vincent Hatakeyama requested to merge topic/16.0/fix-pypdf2 into branch/16.0
3 files
+ 18
8
Compare changes
  • Side-by-side
  • Inline
Files
3
+ 12
7
##############################################################################
#
# Redner Odoo module
# Copyright © 2016 XCG Consulting <https://xcg-consulting.fr>
# Copyright © 2016, 2024 XCG Consulting <https://xcg-consulting.fr>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
@@ -36,11 +36,13 @@ from ..utils.formats import Formats
logger = logging.getLogger(__name__)
_pypdf2 = False
try:
from PyPDF2 import PdfFileReader, PdfFileWriter
from pypdf import PdfWriter
except ImportError:
logger.debug("Cannot import PyPDF2")
from PyPDF2 import PdfFileReader, PdfFileWriter
_pypdf2 = True
class RednerReport(models.TransientModel):
@@ -137,10 +139,13 @@ class RednerReport(models.TransientModel):
:param reports_path: list of path of pdf files
:returns: path of the merged pdf
"""
writer = PdfFileWriter()
writer = PdfFileWriter() if _pypdf2 else PdfWriter()
for path in reports_path:
reader = PdfFileReader(path)
writer.appendPagesFromReader(reader)
if _pypdf2:
reader = PdfFileReader(path)
writer.appendPagesFromReader(reader)
else:
writer.append(path)
merged_file_fd, merged_file_path = tempfile.mkstemp(
suffix=".pdf", prefix="report.merged.tmp."
)
Loading