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

Bug in mediabox expansion when applying non-right angle rotation #2281

Closed
MrinalJain17 opened this issue Oct 31, 2023 · 0 comments · Fixed by #2282
Closed

Bug in mediabox expansion when applying non-right angle rotation #2281

MrinalJain17 opened this issue Oct 31, 2023 · 0 comments · Fixed by #2282

Comments

@MrinalJain17
Copy link
Contributor

  • I have a pdf that I want to rotate by an angle that is not a multiple of 90. Let's say 175 degrees.
  • In addition, I want to update the mediabox coordinates as needed after the rotation.

Currently, the mediabox expansion is broken.

The bug is caused by these lines of code:

pypdf/pypdf/_page.py

Lines 1658 to 1662 in 9047079

lowerleft = (min(corners[0], lowerleft[0]), min(corners[1], lowerleft[1]))
upperright = (
max(corners[2], upperright[0]),
max(corners[3], upperright[1]),
)

Environment

Which environment were you using when you encountered the problem?

$ python -m platform
macOS-14.0-x86_64-i386-64bit

$ python -c "import pypdf;print(pypdf._debug_versions)"
pypdf==3.15.5, crypt_provider=('cryptography', '39.0.1'), PIL=10.0.1

Code + PDF

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

from pypdf import PdfWriter, PdfReader, Transformation

reader = PdfReader("input.pdf")
writer = PdfWriter()

transformation = Transformation().rotate(175)

for page_box in reader.pages:
    page_box.add_transformation(transformation, expand=True)
    writer.add_page(page_box)

with open("output.pdf", "wb") as f:
    writer.write(f)
  • Input pdf: input.pdf
  • Current output of the code above: current_output.pdf
    • Notice the white space covering the top-half of each page. This represents an incorrect expansion of the media box.
  • Expected output of the code above: expected_output.pdf
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

Successfully merging a pull request may close this issue.

1 participant