Skip to content

"KeyError: '/Resources'" on page merge #2147

@stefan6419846

Description

@stefan6419846

Merging/watermarking a PDF file with another might raise a KeyError when retrieving the resources.

Environment

Which environment were you using when you encountered the problem?

$ python -m platform
Linux-5.14.21-150400.24.81-default-x86_64-with-glibc2.31

$ python -c "import pypdf;print(pypdf._debug_versions)"
pypdf==3.15.5, crypt_provider=('pycryptodome', '3.18.0'), PIL=10.0.0

Code + PDF

This is a minimal, complete example that shows the issue:

from pypdf import PdfReader, PdfWriter

watermark = PdfReader("watermark.pdf").pages[0]

pdf_file = PdfWriter(clone_from="file.pdf")
for page in pdf_file.pages:
    page.merge_page(watermark, over=True)

The watermark is https://github.com/py-pdf/pypdf/files/12428857/watermark.pdf I have not yet found a base document I can share for reproduction.

Traceback

This is the complete traceback I see:

Traceback (most recent call last):
  File "/home/stefan/temp/run1.py", line 7, in <module>
    page.merge_page(watermark, over=True)
  File "/home/stefan/temp/venv/lib/python3.9/site-packages/pypdf/_page.py", line 1044, in merge_page
    self._merge_page(page2, over=over, expand=expand)
  File "/home/stefan/temp/venv/lib/python3.9/site-packages/pypdf/_page.py", line 1071, in _merge_page
    return self._merge_page_writer(
  File "/home/stefan/temp/venv/lib/python3.9/site-packages/pypdf/_page.py", line 1182, in _merge_page_writer
    original_resources = cast(DictionaryObject, self[PG.RESOURCES].get_object())
  File "/home/stefan/temp/venv/lib/python3.9/site-packages/pypdf/generic/_data_structures.py", line 320, in __getitem__
    return dict.__getitem__(self, key).get_object()
KeyError: '/Resources'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions