First of all, thank you very much for creating and maintaining this useful library.
Second, excuse me if this is not a bug (quite probably), and it's just me not knowing how to use the library well.
Sometimes, when using showPDFPage to add a watermark, the watermark appears reversed.
This is the code snippet I'm using to apply the watermarks:
def apply_watermark_to_pdf(pdf_stream, watermark_stream):
"""Return a watermarked PDF by merging two PDFs.
Parameters
----------
pdf_stream : BytesIO
PDF contents to be watermaked.
watermark_stream : BytesIO
The image that will be used as watermark.
Returns
-------
BytesIO
The watermarked PDF.
"""
original_pdf = fitz.open(stream=pdf_stream, filetype="pdf")
watermark = fitz.open(stream=watermark_stream, filetype="pdf")
for page in original_pdf:
page.showPDFpage(page.rect, watermark)
return original_pdf.write()
This is the PDF file where the watermark appears mirrored.
The watermark image should appear as it originally is, not reversed, or mirrored, or rotated.
This is what I consider the expected behavior (notice the "Preview Only" watermark being properly displayed):

This is what I'm sometimes getting, the watermark appears "reversed" (notice the "Preview Only" watermark appearing reversed and starting at the bottom of the page):

>>> print(sys.version, "\n", sys.platform, "\n", fitz.__doc__)
3.6.12 (default, Sep 10 2020, 17:53:20)
[GCC 8.3.0]
linux
PyMuPDF 1.18.7: Python bindings for the MuPDF 1.18.0 library.
Version date: 2021-01-31 00:00:01.
Built for Python 3.6 on linux (64-bit)
This is a known (and frequent) problem. For some background, please read this section of the docu.
The PDF page has a "non-localized" geometry change - which in your case reverts up and down.
Check the proper containment of such instructions via page.is_wrapped() and, if false do page.wrap_contents() before your first insert (of images, text, annotations, etc.).
@JorjMcKie thanks for your quick response!
In the documentation, I found some references to page._wrapContents() instead of page.wrap_contents() which led me to the below error.
~I've tried modifying my code to look like this:~
def apply_watermark_to_pdf(pdf_stream, watermark_stream):
"""Return a watermarked PDF by merging two PDFs.
Parameters
----------
pdf_stream : BytesIO
PDF contents to be watermaked.
watermark_stream : BytesIO
The image that will be used as watermark.
Returns
-------
BytesIO
The watermarked PDF.
"""
original_pdf = fitz.open(stream=pdf_stream, filetype="pdf")
watermark = fitz.open(stream=watermark_stream, filetype="pdf")
for page in original_pdf:
if not page._isWrapped:
page._wrapContents()
page.showPDFpage(page.rect, watermark)
return original_pdf.write()
~But I'm getting this error: AttributeError: 'Page' object has no attribute '_wrapContents'~
~Sorry, am I not using it correctly?~
The method name is wrap_contents() now. Changing to snake_case instead of camelCase ...
Von meinem iPhone gesendet
Am 24/2/21 um 12:56 schrieb Ricardo Molina notifications@github.com:

@JorjMcKiehttps://github.com/JorjMcKie thanks for your quick response!
I've tried modifying my code to look like this:
def apply_watermark_to_pdf(pdf_stream, watermark_stream):
"""Return a watermarked PDF by merging two PDFs.
Parameters
----------
pdf_stream : BytesIO
PDF contents to be watermaked.
watermark_stream : BytesIO
The image that will be used as watermark.
Returns
-------
BytesIO
The watermarked PDF.
"""
original_pdf = fitz.open(stream=pdf_stream, filetype="pdf")
watermark = fitz.open(stream=watermark_stream, filetype="pdf")
for page in original_pdf:
if not page._isWrapped:
page._wrapContents()
page.showPDFpage(page.rect, watermark)
return original_pdf.write()
But I'm getting this error: AttributeError: 'Page' object has no attribute '_wrapContents'
Sorry, am I not using it correctly?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHubhttps://github.com/pymupdf/PyMuPDF/issues/919#issuecomment-785219736, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AB7IDIVUAAHSFR7IFZKEE63TAUVSLANCNFSM4YE2WXQA.
Thank you so much for your kind help Jorj! My problem has been solved :)
Most helpful comment
Thank you so much for your kind help Jorj! My problem has been solved :)