py-pdf / pypdf

A pure-python PDF library capable of splitting, merging, cropping, and transforming the pages of PDF files
https://pypdf.readthedocs.io/en/latest/
Other
8.08k stars 1.39k forks source link

KeyError: '/Root' due to invalid start of xref #1756

Closed owurman closed 1 year ago

owurman commented 1 year ago

I was trying to get the pages for the attached PDF but received a KeyError: '/Root'. The file appears to be encrypted to me, but pdf.is_encrypted is False.

Environment

Which environment were you using when you encountered the problem?

$ python -m platform
macOS-10.16-x86_64-i386-64bit

$ python -c "import pypdf;print(pypdf.__version__)"
3.7.0

Code + PDF

import pypdf

reader = pypdf.PdfReader("641-Attachment-B-Pediatric-Cardiac-Arrest-8-1-2019.pdf")
assert (not reader.is_encrypted)
len(reader.pages)

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! 641-Attachment-B-Pediatric-Cardiac-Arrest-8-1-2019.pdf

It's a public document so it should be fine to add to your tests.

Traceback

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pypdf/_page.py", line 2155, in __len__
    return self.length_function()
           ^^^^^^^^^^^^^^^^^^^^^^
  File "pypdf/_reader.py", line 452, in _get_num_pages
    self._flatten()
  File "pypdf/_reader.py", line 1186, in _flatten
    catalog = self.trailer[TK.ROOT].get_object()
              ~~~~~~~~~~~~^^^^^^^^^
  File "pypdf/generic/_data_structures.py", line 291, in __getitem__
    return dict.__getitem__(self, key).get_object()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
KeyError: '/Root'
MartinThoma commented 1 year ago

A PDF file should look like this:

image

with:

A trailer giving the location of the cross-reference table and of certain special objects within the body of the file

The trailer of that file is empty, thus the error.

MartinThoma commented 1 year ago

This command fixes it:

gs -o repaired.pdf -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress 641-Attachment-B-Pediatric-Cardiac-Arrest-8-1-2019.pdf

See https://superuser.com/q/278562/64857

owurman commented 1 year ago

Thanks. Is it a reasonable ask that a better message be given if the trailer is missing? I'm guessing that actually repairing the PDF as ghostscript does is beyond the scope of what you want pypdf to do...

MartinThoma commented 1 year ago

The problem is not this specific case. Sure, we can (and regularly do) add robustness improvements. It's just a never ending story. There is an infinite number of ways the standard can be broken

MartinThoma commented 1 year ago

I was hoping that we could use similar techniques as web browsers / beautiful soup does for HTML for that problem. I just didn't have the time to look into it so far.

pubpub-zz commented 1 year ago

@owurman I've prepared a PR to improve robustness if you want to try it.

MartinThoma commented 1 year ago

The robustness improvement was just added to main and will be released this weekend with pypdf>3.7.1.

MartinThoma commented 1 year ago

@owurman If you want I can add you as a contributor to https://pypdf.readthedocs.io/en/latest/meta/CONTRIBUTORS.html