Skip to content

[18.0][IMP] report_qweb_pdf_watermark: support pypdf >= 2.0 #1023

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: 18.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 24 additions & 16 deletions report_qweb_pdf_watermark/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,38 +88,38 @@ Changelog
12.0.1.0.0 (2019-11-18)
-----------------------

- [MIG] Migration to V12.
- [MIG] Migration to V12.

13.0.1.0.0 (2021-01-27)
-----------------------

- [MIG] Migration to V13.
- [MIG] Migration to V13.

14.0.1.0.0 (2021-01-29)
-----------------------

- [MIG] Migration to V14.
- [MIG] Migration to V14.

15.0.1.0.0 (2022-01-11)
-----------------------

- [MIG] Migration to V15.
- Define pdf watermark in company settings
- [MIG] Migration to V15.
- Define pdf watermark in company settings

16.0.1.0.0 (2023-03-13)
-----------------------

- [MIG] Migration to V16.
- [MIG] Migration to V16.

17.0.1.0.0 (2024-01-12)
-----------------------

- [MIG] Migration to V17.
- [MIG] Migration to V17.

18.0.1.0.0 (2025-01-06)
-----------------------

- [MIG] Migration to V18.
- [MIG] Migration to V18.

Bug Tracker
===========
Expand All @@ -142,14 +142,14 @@ Authors
Contributors
------------

- Holger Brunn <[email protected]>
- Stefan Rijnhart <[email protected]>
- Rod Schouteden <[email protected]>
- Robin Goots <[email protected]>
- Foram Shah <[email protected]>
- bosd <[email protected]>
- Sander Lienaerts <[email protected]>
- Anjeel Haria
- Holger Brunn <[email protected]>
- Stefan Rijnhart <[email protected]>
- Rod Schouteden <[email protected]>
- Robin Goots <[email protected]>
- Foram Shah <[email protected]>
- bosd <[email protected]>
- Sander Lienaerts <[email protected]>
- Anjeel Haria

Maintainers
-----------
Expand All @@ -164,6 +164,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 <https://odoo-community.org/page/maintainer-role>`__:

|maintainer-hbrunn|

This module is part of the `OCA/reporting-engine <https://github.com/OCA/reporting-engine/tree/18.0/report_qweb_pdf_watermark>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
1 change: 1 addition & 0 deletions report_qweb_pdf_watermark/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"summary": "Add watermarks to your QWEB PDF reports",
"website": "https://github.com/OCA/reporting-engine",
"depends": ["web"],
"maintainers": ["hbrunn"],
"data": [
"views/ir_actions_report_xml.xml",
"views/res_company.xml",
Expand Down
31 changes: 22 additions & 9 deletions report_qweb_pdf_watermark/models/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,17 @@
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

Check warning on line 24 in report_qweb_pdf_watermark/models/report.py

View check run for this annotation

Codecov / codecov/patch

report_qweb_pdf_watermark/models/report.py#L24

Added line #L24 was not covered by tests
except ImportError:
from PyPDF2 import ( # pylint: disable=W0404
PdfFileReader as PdfReader,
)
from PyPDF2 import (
PdfFileWriter as PdfWriter,
)
from PyPDF2.utils import PdfReadError # pypdf < 2.0
except ImportError:
logger.debug("Can not import PyPDF2")

Expand Down Expand Up @@ -105,11 +114,11 @@
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()
Expand All @@ -121,7 +130,7 @@
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)

Expand All @@ -132,12 +141,16 @@
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)
Expand Down
2 changes: 2 additions & 0 deletions report_qweb_pdf_watermark/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,8 @@ <h2><a class="toc-backref" href="#toc-entry-16">Maintainers</a></h2>
<p>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.</p>
<p>Current <a class="reference external" href="https://odoo-community.org/page/maintainer-role">maintainer</a>:</p>
<p><a class="reference external image-reference" href="https://github.com/hbrunn"><img alt="hbrunn" src="https://github.com/hbrunn.png?size=40px" /></a></p>
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/reporting-engine/tree/18.0/report_qweb_pdf_watermark">OCA/reporting-engine</a> project on GitHub.</p>
<p>You are welcome to contribute. To learn how please visit <a class="reference external" href="https://odoo-community.org/page/Contribute">https://odoo-community.org/page/Contribute</a>.</p>
</div>
Expand Down