Skip to content

Commit 13f133f

Browse files
committed
Intercept erroneous widgets
When copying widgets as part of insert_pdf() processing, badly defined source widgets cause MuPDF exceptions when building their grafted object version. This fix intercepts MuPDF exceptions and ignores such widgets.
1 parent 29e4b40 commit 13f133f

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

src/utils.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1987,7 +1987,11 @@ def get_acroform(doc):
19871987
w_obj.pdf_dict_get(pymupdf.PDF_NAME("Parent"))
19881988
)
19891989
if parent_xref == 0: # parent not in target yet
1990-
w_obj_graft = mupdf.pdf_graft_mapped_object(graftmap, w_obj)
1990+
try:
1991+
w_obj_graft = mupdf.pdf_graft_mapped_object(graftmap, w_obj)
1992+
except Exception as e:
1993+
pymupdf.message_warning(f"cannot copy widget at {xref=}: {e}")
1994+
continue
19911995
w_obj_tar = mupdf.pdf_add_object(tarpdf, w_obj_graft)
19921996
tar_xref = w_obj_tar.pdf_to_num()
19931997
w_obj_tar_ind = mupdf.pdf_new_indirect(tarpdf, tar_xref, 0)

tests/resources/test_4614.pdf

397 KB
Binary file not shown.

tests/test_4614.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import pymupdf
2+
import os
3+
4+
5+
def test_4614():
6+
script_dir = os.path.dirname(__file__)
7+
filename = os.path.join(script_dir, "resources", "test_4614.pdf")
8+
src = pymupdf.open(filename)
9+
doc = pymupdf.open()
10+
doc.insert_pdf(src)

0 commit comments

Comments
 (0)