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: Fix getter of the PageObject.rotation property with an indirect object #1602

Merged
merged 1 commit into from
Feb 5, 2023
Merged
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
11 changes: 5 additions & 6 deletions pypdf/_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,10 @@ def rotation(self) -> int:
This number has to be a multiple of 90 degrees: 0, 90, 180, or 270 are
valid values. This property does not affect ``/Contents``.
"""
return int(self.get(PG.ROTATE, 0))
rotate_obj = self.get(PG.ROTATE, 0)
return (
rotate_obj if isinstance(rotate_obj, int) else rotate_obj.get_object()
)

@rotation.setter
def rotation(self, r: Union[int, float]) -> None:
Expand Down Expand Up @@ -547,11 +550,7 @@ def rotate(self, angle: int) -> "PageObject":
"""
if angle % 90 != 0:
raise ValueError("Rotation angle must be a multiple of 90")
rotate_obj = self.get(PG.ROTATE, 0)
current_angle = (
rotate_obj if isinstance(rotate_obj, int) else rotate_obj.get_object()
)
self[NameObject(PG.ROTATE)] = NumberObject(current_angle + angle)
self[NameObject(PG.ROTATE)] = NumberObject(self.rotation + angle)
return self

def rotate_clockwise(self, angle: int) -> "PageObject": # deprecated
Expand Down
Binary file added resources/indirect-rotation.pdf
Binary file not shown.
8 changes: 8 additions & 0 deletions tests/test_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,14 @@ def test_page_rotation():
)


def test_page_indirect_rotation():
reader = PdfReader(RESOURCE_ROOT / "indirect-rotation.pdf")
page = reader.pages[0]

# test rotation
assert page.rotation == 0


def test_page_scale():
op = Transformation()
with pytest.raises(ValueError) as exc:
Expand Down