Skip to content

Commit 25b5d21

Browse files
Fix #5270.
src/__init__.py:_remove_dest_range(): don't fail if we can't find link destination. tests/test_pagedelete.py: test_5270(): new. changes.txt: mark #5270 as fixed.
1 parent 0b31ac0 commit 25b5d21

3 files changed

Lines changed: 32 additions & 2 deletions

File tree

‎changes.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Change Log
77
Fixed issues:
88

99
* **Fixed** `4755 <https://github.com/pymupdf/PyMuPDF/issues/4755>`_: Unexpected behavior of annot.set_rect() with carets and strikeouts
10+
* **Fixed** `5270 <https://github.com/pymupdf/PyMuPDF/issues/5270>`_: delete_page() raises a SWIG TypeError when another page has an invalid UTF-16 GoTo destination
1011

1112
Other:
1213

‎src/__init__.py‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19305,8 +19305,15 @@ def _remove_dest_range(pdf, numbers):
1930519305
target = mupdf.pdf_array_get( dest, 0)
1930619306
pno = mupdf.pdf_lookup_page_number( pdf, target)
1930719307
elif mupdf.pdf_is_string( dest):
19308-
location, _, _ = mupdf.fz_resolve_link( pdf.super(), mupdf.pdf_to_text_string( dest))
19309-
pno = location.page
19308+
try:
19309+
location, _, _ = mupdf.fz_resolve_link( pdf.super(), mupdf.pdf_to_text_string( dest))
19310+
except Exception:
19311+
# We're only looking for links that are definitely to
19312+
# one of the deleted pages, so we don't care if we can't
19313+
# resolve the link.
19314+
pass
19315+
else:
19316+
pno = location.page
1931019317
if pno < 0: # page number lookup did not work
1931119318
continue
1931219319
n1 = pno

‎tests/test_pagedelete.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,25 @@ def make_document():
203203
document.delete_pages(from_page=-2, to_page=-1)
204204
document = make_document()
205205
document.delete_pages(-2, -1)
206+
207+
208+
def test_5270():
209+
state = dict()
210+
for destination in 'FEFF0041', 'FEFFD800':
211+
print(f'{destination=}', flush=1)
212+
with pymupdf.open() as document:
213+
for number in range(3):
214+
document.new_page().insert_text((40, 40), f'synthetic page {number}')
215+
xref = document.get_new_xref()
216+
document.update_object(xref, f'<</Type /Annot /Subtype /Link /Rect [0 0 20 20] /A <</S /GoTo /D <{destination}>>>>>')
217+
document.xref_set_key(document.page_xref(0), 'Annots', f'[{xref} 0 R]')
218+
document.save(os.path.normpath(f'{__file__}/../../tests/test_5270_out.pdf'))
219+
try:
220+
document.delete_page(1)
221+
except Exception as e:
222+
state[destination] = e
223+
raise
224+
print(f'{len(document)=}')
225+
assert len(document) == 2
226+
227+
print(f'{state=}')

0 commit comments

Comments
 (0)