Skip to content

Commit 114ded8

Browse files
tests/: added new test_4790().
tests/test_pagedelete.py:test_4790(): New test, expects the current incorrect behaviour, checks workarounds fix the issue, that new Document.save() flag <raise_on_repair> works, and that new Document.repair() works. tests/resources/test_4790.pdf: new input file.
1 parent 3d11e9b commit 114ded8

2 files changed

Lines changed: 78 additions & 0 deletions

File tree

tests/resources/test_4790.pdf

15.2 KB
Binary file not shown.

tests/test_pagedelete.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,81 @@ def test_4462():
113113
document.save(path2)
114114
with pymupdf.open(path2) as document:
115115
assert len(document) == 2
116+
117+
118+
def test_4790():
119+
path = os.path.normpath(f'{__file__}/../../tests/resources/test_4790.pdf')
120+
path2 = os.path.normpath(f'{__file__}/../../tests/test_4790_out.pdf')
121+
print()
122+
page_to_delete = 1
123+
124+
# Reproduce the problem.
125+
with pymupdf.open(path) as document:
126+
wt = pymupdf.TOOLS.mupdf_warnings()
127+
assert not wt, f'{wt=}'
128+
assert len(document) == 2, f'{len(document)=}'
129+
document.delete_pages(page_to_delete)
130+
assert len(document) == 1, f'{len(document)=}'
131+
document.save(path2)
132+
wt = pymupdf.TOOLS.mupdf_warnings()
133+
assert wt == 'repairing PDF document', f'{wt=}'
134+
with pymupdf.open(path2) as document:
135+
# Expect incorrect result.
136+
assert len(document) == 2, f'{len(document)=}'
137+
138+
# Call mupdf.pdf_repair_xref() before delete_pages(); this works around the
139+
# problem.
140+
with pymupdf.open(path) as document:
141+
document_pdf = pymupdf._as_pdf_document(document)
142+
pymupdf.mupdf.pdf_repair_xref(document_pdf)
143+
wt = pymupdf.TOOLS.mupdf_warnings()
144+
assert wt == 'repairing PDF document', f'{wt=}'
145+
document.delete_pages(page_to_delete)
146+
document.save(path2)
147+
with pymupdf.open(path2) as document:
148+
# Expect correct result.
149+
assert len(document) == 1
150+
151+
# Call mupdf.pdf_check_document() before delete_pages(); this works around
152+
# the problem.
153+
with pymupdf.open(path) as document:
154+
document_pdf = pymupdf._as_pdf_document(document)
155+
pymupdf.mupdf.pdf_check_document(document_pdf)
156+
wt = pymupdf.TOOLS.mupdf_warnings()
157+
assert wt == 'repairing PDF document', f'{wt=}'
158+
document.delete_pages(page_to_delete)
159+
document.save(path2)
160+
with pymupdf.open(path2) as document:
161+
# Expect correct result.
162+
assert len(document) == 1
163+
164+
# Check that document is marked as repaired after save.
165+
with pymupdf.open(path) as document:
166+
assert not document.is_repaired, f'{document.is_repaired=}'
167+
document.save(path2)
168+
assert document.is_repaired, f'{document.is_repaired=}'
169+
wt = pymupdf.TOOLS.mupdf_warnings()
170+
assert wt == 'repairing PDF document', f'{wt=}'
171+
172+
# Check that raise_on_repair=True works.
173+
with pymupdf.open(path) as document:
174+
try:
175+
document.save(path2, raise_on_repair=True)
176+
except Exception as e:
177+
print(f'Received expected exception: {e}', flush=1)
178+
else:
179+
assert 0, 'Did not get expected exception.'
180+
wt = pymupdf.TOOLS.mupdf_warnings()
181+
assert wt == 'repairing PDF document'
182+
183+
# Check that Document.repair() works.
184+
with pymupdf.open(path) as document:
185+
document.repair()
186+
wt = pymupdf.TOOLS.mupdf_warnings()
187+
assert wt == 'repairing PDF document'
188+
document.delete_pages(page_to_delete)
189+
document.save(path2, raise_on_repair=True)
190+
with pymupdf.open(path2) as document:
191+
# Expect correct result.
192+
assert len(document) == 1, f'{len(document)=}'
193+

0 commit comments

Comments
 (0)