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 merge of a cropped page #879

Closed
wants to merge 9 commits into from
8 changes: 7 additions & 1 deletion PyPDF2/_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,13 @@ def _merge_page(
page2content = page2.get_contents()
if page2content is not None:
page2content = ContentStream(page2content, self.pdf)
rect = page2.trimbox
if (
page2.cropbox.width < page2.trimbox.width
or page2.cropbox.height < page2.trimbox.height
):
rect = page2.cropbox
else:
rect = page2.trimbox
page2content.operations.insert(
0,
(
Expand Down
16 changes: 16 additions & 0 deletions tests/test_page.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import os
from copy import deepcopy
import io
from io import BytesIO
from pathlib import Path

Expand Down Expand Up @@ -205,6 +206,21 @@ def test_page_rotation_non90():
assert exc.value.args[0] == "Rotation angle must be a multiple of 90"


def test_page_merge_cropped():
MartinThoma marked this conversation as resolved.
Show resolved Hide resolved
p = PdfReader(os.path.join(RESOURCE_ROOT, "issue-604.pdf"))
a = deepcopy(p.pages[1]) # crossed to ease test reading
b = deepcopy(p.pages[2])
a.cropbox = RectangleObject([100, 100, 300, 200])
w = PdfWriter()
w.add_page(a)
w.add_page(b)
c = deepcopy(b)
c.merge_page(a)
w.add_page(c)
o = io.BytesIO()
w.write(o)


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