Skip to content

cannot merge PDF with nullobject articles #3508

@Noah-Houghton

Description

@Noah-Houghton

When merging an older PDF with one we generate, we found that add_filtered_articles within merge_pdfs was failing with a NullObject has no attribute 'get' error. I found during local testing that modifying the logic to check explicitly for NullObject (and to continue if found) was enough to get things working again, but I don't know enough about the PDF spec to know if that's a valid state that should be supported. The PDFs do load normally in e.g. Adobe Acrobat or Firefox PDF reader.

I'm waiting for permission to share the erroring PDFs; happy to provide anything that could help diagnose in the meantime.

Here's the snippet I replaced in PdfWriter.add_filtered_articles:

    obj = a.get_object() # capture output of get_object
    if isinstance(obj, NullObject):  # check if it's null
        continue
    thr = obj.get("/T") # continue with existing logic

Environment

Which environment were you using when you encountered the problem?

$ python -m platform
Linux-6.6.87.2-microsoft-standard-WSL2-x86_64-with-glibc2.35

$ python -c "import pypdf;print(pypdf._debug_versions)"
pypdf==6.1.1, crypt_provider=('cryptography', '45.0.7'), PIL=11.3.0

Code + PDF

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

def append_pdfs(*pdfs):
    merger = PdfWriter()
    temp = BytesIO()
    for pdf in pdfs:
        merger.append(pdf)
    merger.write(temp)
    temp.seek(0)
    return temp

Share here the PDF file(s) that cause the issue. The smaller they are, the
better. Let us know if we may add them to our tests!

Traceback

This is the complete traceback I see:

  File "<project path>/utils/file_utils.py", line 102, in append_pdfs
    merger.append(pdf)
  File "<project path>/lib/python3.11/site-packages/pypdf/_writer.py", line 2692, in append
    self.merge(
  File "<project path>/lib/python3.11/site-packages/pypdf/_writer.py", line 2889, in merge
    self.add_filtered_articles("", srcpages, reader)
  File "<project path>/lib/python3.11/site-packages/pypdf/_writer.py", line 2980, in add_filtered_articles
    thr = a.get_object().get("/T")
AttributeError: 'NullObject' object has no attribute 'get'

Metadata

Metadata

Assignees

No one assigned

    Labels

    is-robustness-issueFrom a users perspective, this is about robustnessneeds-pdfThe issue needs a PDF file to show the problem

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions