Skip to content
Snippets Groups Projects
Commit 13065fe2 authored by Vincent Hatakeyama's avatar Vincent Hatakeyama
Browse files

:sparkles: Remove pypdf compatibility code to use Odoo pypdf compatibility instead.

Requires a Odoo with [ADD] *: pypdf 3.x compatibility (https://github.com/odoo/odoo/commit/fddf53c9b6bcaea1a9ff7e041c0ccbb65a4647c8)
parent 921a4e6a
No related branches found
Tags 17.0.1.4.0
1 merge request!74✨ Remove pypdf compatibility code to use Odoo pypdf compatibility instead.
...@@ -2,6 +2,12 @@ ...@@ -2,6 +2,12 @@
Changelog Changelog
========= =========
17.0.1.4.0
----------
Remove pypdf compatibility code to use Odoo pypdf compatibility instead.
Requires a Odoo with `[ADD] *: pypdf 3.x compatibility <https://github.com/odoo/odoo/commit/fddf53c9b6bcaea1a9ff7e041c0ccbb65a4647c8>`_.
17.0.1.3.2 17.0.1.3.2
---------- ----------
......
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
{ {
"name": "Redner", "name": "Redner",
"license": "AGPL-3", "license": "AGPL-3",
"version": "17.0.1.3.2", "version": "17.0.1.4.0",
"category": "Reporting", "category": "Reporting",
"author": "XCG Consulting", "author": "XCG Consulting",
"website": "https://orbeet.io/", "website": "https://orbeet.io/",
......
############################################################################## ##############################################################################
# #
# Redner Odoo module # Redner Odoo module
# Copyright © 2016, 2024 XCG Consulting <https://xcg-consulting.fr> # Copyright © 2016, 2024, 2025 XCG Consulting <https://xcg-consulting.fr>
# #
# This program is free software: you can redistribute it and/or modify # This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as # it under the terms of the GNU Affero General Public License as
...@@ -30,9 +30,10 @@ ...@@ -30,9 +30,10 @@
from odoo import _, api, fields, models # type: ignore[import-untyped] from odoo import _, api, fields, models # type: ignore[import-untyped]
from odoo.exceptions import ValidationError # type: ignore[import-untyped] from odoo.exceptions import ValidationError # type: ignore[import-untyped]
from odoo.tools.pdf import PdfFileReader, PdfFileWriter # type: ignore[import-untyped]
from ..utils import formats # type: ignore[import-untyped] from ..utils import formats # type: ignore[import-untyped]
from ..utils.formats import Formats # type: ignore[import-untyped] from ..utils.formats import Formats # type: ignore[import-untyped]
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
...@@ -33,17 +34,9 @@ ...@@ -33,17 +34,9 @@
from ..utils import formats # type: ignore[import-untyped] from ..utils import formats # type: ignore[import-untyped]
from ..utils.formats import Formats # type: ignore[import-untyped] from ..utils.formats import Formats # type: ignore[import-untyped]
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
_pypdf2 = False
try:
from pypdf import PdfWriter
except ImportError:
from PyPDF2 import PdfFileReader, PdfFileWriter
_pypdf2 = True
class RednerReport(models.TransientModel): class RednerReport(models.TransientModel):
_name = "redner.report" _name = "redner.report"
...@@ -134,8 +127,8 @@ ...@@ -134,8 +127,8 @@
return result_path return result_path
@api.model @api.model
def _merge_pdf(self, reports_path): def _merge_pdf(self, reports_path: list[str]):
"""Merge PDF files into one. """Merge PDF files into one.
:param reports_path: list of path of pdf files :param reports_path: list of path of pdf files
:returns: path of the merged pdf :returns: path of the merged pdf
""" """
...@@ -138,6 +131,8 @@ ...@@ -138,6 +131,8 @@
"""Merge PDF files into one. """Merge PDF files into one.
:param reports_path: list of path of pdf files :param reports_path: list of path of pdf files
:returns: path of the merged pdf :returns: path of the merged pdf
""" """
writer = PdfFileWriter() if _pypdf2 else PdfWriter() # unlike odoo.tool.pdf.merge_pdf, does not store both files to merge and
# merged files in memory; only the merged file and a single file.
writer = PdfFileWriter()
for path in reports_path: for path in reports_path:
...@@ -143,4 +138,4 @@ ...@@ -143,4 +138,4 @@
for path in reports_path: for path in reports_path:
if _pypdf2: with open(path) as fd:
reader = PdfFileReader(path) reader = PdfFileReader(fd, strict=False)
writer.appendPagesFromReader(reader) writer.appendPagesFromReader(reader)
...@@ -146,6 +141,4 @@ ...@@ -146,6 +141,4 @@
writer.appendPagesFromReader(reader) writer.appendPagesFromReader(reader)
else:
writer.append(path)
merged_file_fd, merged_file_path = tempfile.mkstemp( merged_file_fd, merged_file_path = tempfile.mkstemp(
suffix=".pdf", prefix="report.merged.tmp." suffix=".pdf", prefix="report.merged.tmp."
) )
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment