Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ROB : cope with invalid parent xref (#1089) #1133

Merged
merged 1 commit into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions PyPDF2/_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -1317,6 +1317,15 @@ def read(self, stream: StreamType) -> None:
if found:
continue
# no xref table found at specified location
if "/Root" in self.trailer and not self.strict:
# if Root has been already found, just raise warning
warnings.warn("Invalid parent xref., rebuild xref", PdfReadWarning)
try:
self._rebuild_xref_table(stream)
break
except Exception:
raise PdfReadError("can not rebuild xref")
break
raise PdfReadError("Could not find xref table at specified location")
# if not zero-indexed, verify that the table is correct; change it if necessary
if self.xref_index and not self.strict:
Expand Down
11 changes: 5 additions & 6 deletions tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -770,12 +770,12 @@ def test_get_fields():
assert dict(fields["c1-1"]) == ({"/FT": "/Btn", "/T": "c1-1"})


# covers also issue 1089
@pytest.mark.filterwarnings("ignore::PyPDF2.errors.PdfReadWarning")
def test_get_fields_read_else_block():
url = "https://corpora.tika.apache.org/base/docs/govdocs1/934/934771.pdf"
name = "tika-934771.pdf"
with pytest.raises(PdfReadError) as exc:
PdfReader(BytesIO(get_pdf_from_url(url, name=name)))
assert exc.value.args[0] == "Could not find xref table at specified location"
PdfReader(BytesIO(get_pdf_from_url(url, name=name)))


def test_get_fields_read_else_block2():
Expand All @@ -786,12 +786,11 @@ def test_get_fields_read_else_block2():
assert fields is None


@pytest.mark.filterwarnings("ignore::PyPDF2.errors.PdfReadWarning")
def test_get_fields_read_else_block3():
url = "https://corpora.tika.apache.org/base/docs/govdocs1/957/957721.pdf"
name = "tika-957721.pdf"
with pytest.raises(PdfReadError) as exc:
PdfReader(BytesIO(get_pdf_from_url(url, name=name)))
assert exc.value.args[0] == "Could not find xref table at specified location"
PdfReader(BytesIO(get_pdf_from_url(url, name=name)))


def test_metadata_is_none():
Expand Down