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

Overlaid page on another (merge_page) is flipped vertically #2139

Closed
uunbound opened this issue Sep 1, 2023 · 5 comments
Closed

Overlaid page on another (merge_page) is flipped vertically #2139

uunbound opened this issue Sep 1, 2023 · 5 comments

Comments

@uunbound
Copy link

uunbound commented Sep 1, 2023

I have a PdfReader instance in my code that calls the merge_page function to merge another PdfReader instance page into itself. Something seems to be broken between versions 3.15.3 and 3.15.4 where the merged page is flipped vertically and is merged upside-down.

Environment

Which environment were you using when you encountered the problem?

$ python -m platform
Linux-5.15.49-linuxkit-pr-aarch64-with-glibc2.31

$ python -c "import pypdf;print(pypdf._debug_versions)"
pypdf==3.15.4, crypt_provider=('cryptography', '41.0.3'), PIL=10.0.0

Code + PDF

This is a minimal, complete example that shows the issue:

from io import BytesIO
from pypdf import PdfReader, PdfWriter

def merge_pdfs(cover_stream, header_stream, content_stream) -> bytes:
    """
    Merges the given PDF streams into a single PDF stream.
    :param cover_stream: Cover page(s) PDF stream.
    :param header_stream: Header page PDF stream (single page).
    :param content_stream: Content page(s) PDF stream.
    :return: A PDF stream from the merge of the cover page(s) and content pages which are overlaid with the header page.
    """
    cover_pdf = PdfReader(cover_stream)
    header_pdf = PdfReader(header_stream)
    content_pdf = PdfReader(content_stream)

    header_page = header_pdf.pages[0]

    output_pdf = PdfWriter()

    output_pdf.add_page(cover_pdf.pages[0])

    # Overlay header on each page of the content
    for page_number in range(len(content_pdf.pages)):
        content_page = content_pdf.pages[page_number]
        content_page.merge_page(header_page)
        output_pdf.add_page(content_page)

    # Create a BytesIO stream for the output PDF
    output_stream = BytesIO()
    output_pdf.write(output_stream)

    # Move the stream position to the beginning
    output_stream.seek(0)

    return output_stream.getvalue()

I have a single-page cover, single-page header, and multi-page content that I merge using the merge_pdfs function. This function inputs three byte streams one per type and merges them into one PDF. First, a new output PDF is created and the cover page is added to it. Then for each page in the content PDF, the header page is merged with it and then added to the output PDF. The merging of the header page into the content page is done to overlay the header on content. This worked fine until version 3.15.3 but broke in version 3.15.4. The overlay is merged upside down (flipped) and is overlayed at the bottom of the page rather than at the top.

Unfortunately, I can not share the PDF as it contains sensitive information. If the issue or the explanation is not clear, please let me know.

Traceback

There is no exception thrown.

@uunbound uunbound changed the title Overlayed page on another (merge_page) is flipped vertically Overlaid page on another (merge_page) is flipped vertically Sep 1, 2023
@stefan6419846
Copy link
Collaborator

Have you tried running content_page.transfer_rotation_to_content() before merging?

@uunbound
Copy link
Author

uunbound commented Sep 1, 2023

No. Is it something that the new release has introduced intentionally? I didn't have to do that in previous versions.

@uunbound
Copy link
Author

uunbound commented Sep 1, 2023

I just tried it and it fixed the issue! Thanks @stefan6419846 !

@stefan6419846
Copy link
Collaborator

By the way: This is nothing which changed in-between, but I recently added this to the corresponding tutorial as I observed some similar issues about wrongly rotated watermarks, while this method has been buried in the API docs only (#2113). It might have worked before as #2086 significantly changed the watermarking process, but probably only as some side-effect.

Feel free to close this issue as this does not seem to be something which requires further fixing in PyPDF itself.

@uunbound
Copy link
Author

uunbound commented Sep 1, 2023

Thanks for helping out @stefan6419846 . I am closing the issue.

@uunbound uunbound closed this as completed Sep 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants