diff --git a/report_qweb_pdf_watermark/README.rst b/report_qweb_pdf_watermark/README.rst index d2afa15346..b2090b219a 100644 --- a/report_qweb_pdf_watermark/README.rst +++ b/report_qweb_pdf_watermark/README.rst @@ -146,6 +146,14 @@ OCA, or the Odoo Community Association, is a nonprofit organization whose mission is to support the collaborative development of Odoo features and promote its widespread use. +.. |maintainer-hbrunn| image:: https://github.com/hbrunn.png?size=40px + :target: https://github.com/hbrunn + :alt: hbrunn + +Current `maintainer `__: + +|maintainer-hbrunn| + This module is part of the `OCA/reporting-engine `_ project on GitHub. You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/report_qweb_pdf_watermark/__manifest__.py b/report_qweb_pdf_watermark/__manifest__.py index 26b350f8db..c9662d998e 100644 --- a/report_qweb_pdf_watermark/__manifest__.py +++ b/report_qweb_pdf_watermark/__manifest__.py @@ -10,6 +10,7 @@ "summary": "Add watermarks to your QWEB PDF reports", "website": "https://github.com/OCA/reporting-engine", "depends": ["web"], + "maintainers": ["hbrunn"], "data": [ "demo/report.xml", "views/ir_actions_report_xml.xml", diff --git a/report_qweb_pdf_watermark/models/report.py b/report_qweb_pdf_watermark/models/report.py index b48516933d..024780c323 100644 --- a/report_qweb_pdf_watermark/models/report.py +++ b/report_qweb_pdf_watermark/models/report.py @@ -19,8 +19,16 @@ logger.error("ImportError: The PdfImagePlugin could not be imported") try: - from PyPDF2 import PdfFileReader, PdfFileWriter # pylint: disable=W0404 - from PyPDF2.utils import PdfReadError # pylint: disable=W0404 + + try: + from PyPDF2 import PdfReader, PdfWriter # pylint: disable=W0404 + from PyPDF2.errors import PdfReadError # pypdf >= 2.0 + except ImportError: + from PyPDF2 import ( # pylint: disable=W0404 + PdfFileReader as PdfReader, + PdfFileWriter as PdfWriter, + ) + from PyPDF2.utils import PdfReadError # pypdf < 2.0 except ImportError: logger.debug("Can not import PyPDF2") @@ -108,11 +116,11 @@ def _run_wkhtmltopdf( if not watermark: return result - pdf = PdfFileWriter() + pdf = PdfWriter() pdf_watermark = None try: - pdf_watermark = PdfFileReader(BytesIO(watermark)) - except PdfReadError: + pdf_watermark = PdfReader(BytesIO(watermark)) + except (UnicodeDecodeError, PdfReadError): # let's see if we can convert this with pillow try: Image.init() @@ -124,7 +132,7 @@ def _run_wkhtmltopdf( if isinstance(resolution, tuple): resolution = resolution[0] image.save(pdf_buffer, "pdf", resolution=resolution) - pdf_watermark = PdfFileReader(pdf_buffer) + pdf_watermark = PdfReader(pdf_buffer) except Exception as e: logger.exception("Failed to load watermark", e) @@ -135,12 +143,16 @@ def _run_wkhtmltopdf( if not self.pdf_has_usable_pages(pdf_watermark.numPages): return result - for page in PdfFileReader(BytesIO(result)).pages: + for page in PdfReader(BytesIO(result)).pages: watermark_page = pdf.addBlankPage( page.mediaBox.getWidth(), page.mediaBox.getHeight() ) - watermark_page.mergePage(pdf_watermark.getPage(0)) - watermark_page.mergePage(page) + # merge_page is >= 2.0, mergePage < 2.0 + merge_page = ( + getattr(watermark_page, "merge_page", None) or watermark_page.mergePage + ) + merge_page(pdf_watermark.getPage(0)) + merge_page(page) pdf_content = BytesIO() pdf.write(pdf_content) diff --git a/report_qweb_pdf_watermark/static/description/index.html b/report_qweb_pdf_watermark/static/description/index.html index 97a381e8be..b111a43f20 100644 --- a/report_qweb_pdf_watermark/static/description/index.html +++ b/report_qweb_pdf_watermark/static/description/index.html @@ -1,4 +1,3 @@ - @@ -498,6 +497,8 @@

Maintainers

OCA, or the Odoo Community Association, is a nonprofit organization whose mission is to support the collaborative development of Odoo features and promote its widespread use.

+

Current maintainer:

+

hbrunn

This module is part of the OCA/reporting-engine project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.