Skip to content

Commit

Permalink
BUG : Invalid font pointed during merge_resources (#1641)
Browse files Browse the repository at this point in the history
Fixed #1640
  • Loading branch information
pubpub-zz authored Feb 25, 2023
1 parent 2e4b657 commit 5173238
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
5 changes: 4 additions & 1 deletion pypdf/_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,9 @@ def compute_unique_key(base_key: str) -> Tuple[str, bool]:
value.
"""
value = page2res.raw_get(base_key)
# TODO : possible improvement : in case of writer, the indirect_reference
# can not be found because translated : this may be improved

# try the current key first (e.g. "foo"), but otherwise iterate
# through "foo-0", "foo-1", etc. new_res can contain only finitely
# many keys, thus this'll eventually end, even if it's been crafted
Expand Down Expand Up @@ -643,7 +646,7 @@ def compute_unique_key(base_key: str) -> Tuple[str, bool]:
if is_pdf_writer:
new_res[newname] = page2res.raw_get(key).clone(pdf)
try:
new_res[newname] = new_res[key].indirect_reference
new_res[newname] = new_res[newname].indirect_reference
except AttributeError:
pass
else:
Expand Down
24 changes: 17 additions & 7 deletions tests/test_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -1092,17 +1092,27 @@ def test_merge_page_resources_smoke_test():

@pytest.mark.external
def test_merge_transformed_page_into_blank():
url = "https://github.com/py-pdf/pypdf/files/10540507/visitcard.pdf"
name = "visitcard.pdf"
r = PdfReader(BytesIO(get_pdf_from_url(url, name=name)))
url = "https://github.com/py-pdf/pypdf/files/10768334/badges_3vjrh_7LXDZ_1-1.pdf"
name = "badges_3vjrh_7LXDZ_1.pdf"
r1 = PdfReader(BytesIO(get_pdf_from_url(url, name=name)))
url = "https://github.com/py-pdf/pypdf/files/10768335/badges_3vjrh_7LXDZ_2-1.pdf"
name = "badges_3vjrh_7LXDZ_2.pdf"
r2 = PdfReader(BytesIO(get_pdf_from_url(url, name=name)))
w = PdfWriter()
w.add_blank_page(100, 100)
w.pages[0].merge_translated_page(r1.pages[0], 0, 0, True, True)
w.pages[0].merge_translated_page(r2.pages[0], 1000, 1000, True, True)
assert (
w.pages[0]["/Resources"]["/Font"].raw_get("/F2+0").idnum
!= w.pages[0]["/Resources"]["/Font"].raw_get("/F2+0-0").idnum
)
w.add_blank_page(100, 100)
for x in range(4):
for y in range(7):
w.pages[0].merge_translated_page(
r.pages[0],
x * r.pages[0].trimbox[2],
y * r.pages[0].trimbox[3],
w.pages[1].merge_translated_page(
r1.pages[0],
x * r1.pages[0].trimbox[2],
y * r1.pages[0].trimbox[3],
True,
True,
)

0 comments on commit 5173238

Please sign in to comment.