Skip to content
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

[16.0][IMP] report_qweb_pdf_watermark: support pypdf >= 2.0 #951

Open
wants to merge 2 commits into
base: 16.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
8 changes: 8 additions & 0 deletions report_qweb_pdf_watermark/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <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/16.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": [
"demo/report.xml",
"views/ir_actions_report_xml.xml",
Expand Down
30 changes: 21 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,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

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

View check run for this annotation

Codecov / codecov/patch

report_qweb_pdf_watermark/models/report.py#L25

Added line #L25 was not covered by tests
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")

Expand Down Expand Up @@ -108,11 +116,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 @@ -124,7 +132,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 @@ -135,12 +143,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
3 changes: 2 additions & 1 deletion report_qweb_pdf_watermark/static/description/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
Expand Down Expand Up @@ -498,6 +497,8 @@ <h2><a class="toc-backref" href="#toc-entry-14">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/16.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
Loading