Skip to content

Commit

Permalink
BUG : Fix merge of a cropped page
Browse files Browse the repository at this point in the history
tracked in py-pdf#636

thee smaller box between cropBox is and  trimBox(== mediaBox by default) is used
  • Loading branch information
pubpub-zz committed May 16, 2022
1 parent 4429066 commit 2cb9f42
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
13 changes: 9 additions & 4 deletions PyPDF2/_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,16 +352,21 @@ def _mergePage(
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
page2content.operations.insert(
0,
(
map(
FloatObject,
[
page2.trimBox.getLowerLeft_x(),
page2.trimBox.getLowerLeft_y(),
page2.trimBox.getWidth(),
page2.trimBox.getHeight(),
rect.getLowerLeft_x(),
rect.getLowerLeft_y(),
rect.getWidth(),
rect.getHeight(),
],
),
"re",
Expand Down
18 changes: 17 additions & 1 deletion tests/test_page.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import json
import os
import copy
import io

import pytest

from PyPDF2 import PdfFileReader
from PyPDF2 import PdfFileReader,PdfFileWriter
from PyPDF2._page import PageObject
from PyPDF2.generic import RectangleObject

Expand Down Expand Up @@ -128,3 +130,17 @@ def test_page_rotation_non90():
with pytest.raises(ValueError) as exc:
page.rotateClockwise(91)
assert exc.value.args[0] == "Rotation angle must be a multiple of 90"

def test_page_merge_cropped():
p = PdfFileReader(os.path.join(RESOURCE_ROOT, "issue-604.pdf"))
a= copy.deepcopy(p.getPage(1)) # crossed to ease test reading
b= copy.deepcopy(p.getPage(2))
a.cropBox = RectangleObject([100,100,300,200])
w = PdfFileWriter()
w.addPage(a)
w.addPage(b)
c=copy.deepcopy(b)
c.mergePage(a)
w.addPage(c)
o = io.BytesIO()
w.write(o)

0 comments on commit 2cb9f42

Please sign in to comment.