Skip to content

Commit

Permalink
BUG: Mediabox expansion size when applying non-right angle rotation (#…
Browse files Browse the repository at this point in the history
…2282)

Fixes #2281
  • Loading branch information
MrinalJain17 authored Nov 14, 2023
1 parent 112bfab commit 03bce94
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
5 changes: 0 additions & 5 deletions pypdf/_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -1655,11 +1655,6 @@ def add_transformation(

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

self.mediabox.lower_left = lowerleft
self.mediabox.upper_right = upperright
Expand Down
29 changes: 29 additions & 0 deletions tests/test_page.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Test the pypdf._page module."""
import json
import math
from copy import deepcopy
from io import BytesIO
from pathlib import Path
Expand Down Expand Up @@ -122,6 +123,34 @@ def test_page_operations(pdf_path, password):
page.extract_text()


@pytest.mark.parametrize(
("angle", "expected_width", "expected_height"),
[
(175, 680, 844),
(45, 994, 994),
(-80, 888, 742),
]
)
def test_mediabox_expansion_after_rotation(angle: float, expected_width: int, expected_height: int):
"""
Mediabox dimensions after rotation at a non-right angle with expension are correct.
The test was validated against pillow (see PR #2282)
"""
pdf_path = RESOURCE_ROOT / "crazyones.pdf"
reader = PdfReader(pdf_path)

transformation = Transformation().rotate(angle)
for page_box in reader.pages:
page_box.add_transformation(transformation, expand=True)

mediabox = reader.pages[0].mediabox

# Deviation of upto 2 pixels is acceptable
assert math.isclose(mediabox.width, expected_width, abs_tol=2)
assert math.isclose(mediabox.height, expected_height, abs_tol=2)


def test_transformation_equivalence():
pdf_path = RESOURCE_ROOT / "labeled-edges-center-image.pdf"
reader_base = PdfReader(pdf_path)
Expand Down

0 comments on commit 03bce94

Please sign in to comment.