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
13 changes: 9 additions & 4 deletions PyPDF2/_page.py
Expand Up @@ -539,16 +539,21 @@ def _merge_page(
page2content = page2.getContents()
if page2content is not None:
page2content = ContentStream(page2content, self.pdf)
if(page2.cropBox.getWidth()<page2.trimBox.getWidth() or
page2.cropBox.getHeight()<page2.trimBox.getHeight()):
rect = page2.cropBox
else:
rect = page2.trimBox
MartinThoma marked this conversation as resolved.
Show resolved Hide resolved
page2content.operations.insert(
0,
(
map(
FloatObject,
[
page2.trimbox.left,
page2.trimbox.bottom,
page2.trimbox.width,
page2.trimbox.height,
rect.left,
rect.bottom,
rect.width,
rect.height,
],
),
"re",
Expand Down
19 changes: 18 additions & 1 deletion tests/test_page.py
@@ -1,10 +1,11 @@
import json
import os
from copy import deepcopy
import io

import pytest

from PyPDF2 import PdfReader, Transformation
from PyPDF2 import PdfReader, PdfWriter, Transformation
from PyPDF2._page import PageObject
from PyPDF2.constants import PageAttributes as PG
from PyPDF2.generic import DictionaryObject, NameObject, RectangleObject
Expand Down Expand Up @@ -176,6 +177,21 @@ def test_page_rotation_non90():
page.rotate_clockwise(91)
assert exc.value.args[0] == "Rotation angle must be a multiple of 90"


MartinThoma marked this conversation as resolved.
Show resolved Hide resolved
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.page[1]) # crossed to ease test reading
b = deepcopy(p.page[2])
MartinThoma marked this conversation as resolved.
Show resolved Hide resolved
a.cropbox = RectangleObject([100, 100, 300, 200])
w = PdfWriter()
w.add_page(a)
w.add_page(b)
c = deepcopy(b)
c.mergePage(a)
MartinThoma marked this conversation as resolved.
Show resolved Hide resolved
w.add_page(c)
o = io.BytesIO()
w.write(o)


def test_page_scale():
op = Transformation()
Expand All @@ -185,3 +201,4 @@ def test_page_scale():

assert op.scale(sx=2).ctm == (2, 0, 0, 2, 0, 0)
assert op.scale(sy=3).ctm == (3, 0, 0, 3, 0, 0)

MartinThoma marked this conversation as resolved.
Show resolved Hide resolved