Skip to content

Commit d58fd1f

Browse files
committed
Address missing /Resources object
Intercept missing page resources and generate the object.
1 parent ad4b871 commit d58fd1f

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

src/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8242,6 +8242,8 @@ def _show_pdf_page(self, fz_srcpage, overlay=1, matrix=None, xref=0, oc=0, clip=
82428242
# 1. insert Xobject in Resources
82438243
#-------------------------------------------------------------
82448244
resources = mupdf.pdf_dict_get_inheritable(tpageref, PDF_NAME('Resources'))
8245+
if not resources.m_internal:
8246+
resources = mupdf.pdf_dict_put_dict(tpageref,PDF_NAME('Resources'), 5)
82458247
subres = mupdf.pdf_dict_get(resources, PDF_NAME('XObject'))
82468248
if not subres.m_internal:
82478249
subres = mupdf.pdf_dict_put_dict(resources, PDF_NAME('XObject'), 5)
@@ -16708,6 +16710,8 @@ def JM_merge_resources( page, temp_res):
1670816710
'''
1670916711
# page objects /Resources, /Resources/ExtGState, /Resources/Font
1671016712
resources = mupdf.pdf_dict_get(page.obj(), PDF_NAME('Resources'))
16713+
if not resources.m_internal:
16714+
resources = mupdf.pdf_dict_put_dict(page.obj(), PDF_NAME('Resources'), 5)
1671116715
main_extg = mupdf.pdf_dict_get(resources, PDF_NAME('ExtGState'))
1671216716
main_fonts = mupdf.pdf_dict_get(resources, PDF_NAME('Font'))
1671316717

tests/resources/test_4141.pdf

3.39 KB
Binary file not shown.

tests/test_4141.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import pymupdf
2+
3+
import os.path
4+
5+
6+
def test_4141():
7+
"""survive missing /Resources object in a number of cases."""
8+
path = os.path.abspath(f"{__file__}/../../tests/resources/test_4141.pdf")
9+
doc = pymupdf.open(path)
10+
page = doc[0]
11+
# make sure the right test file
12+
assert doc.xref_get_key(page.xref, "Resources") == ("null", "null")
13+
page.insert_htmlbox((100, 100, 200, 200), "Hallo") # will fail without the fix
14+
doc.close()
15+
doc = pymupdf.open(doc.name)
16+
page = doc[0]
17+
tw = pymupdf.TextWriter(page.rect)
18+
tw.append((100, 100), "Hallo")
19+
tw.write_text(page) # will fail without the fix

0 commit comments

Comments
 (0)