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

Try to improve handling of missing trailer dictionaries in XRef.indexObjects (issue 18986) #19007

Merged
merged 1 commit into from
Nov 5, 2024
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
25 changes: 25 additions & 0 deletions src/core/xref.js
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,31 @@ class XRef {
if (this.topDict) {
return this.topDict;
}

// When no trailer dictionary candidate exists, try picking the first
// dictionary that contains a /Root entry (fixes issue18986.pdf).
if (!trailerDicts.length) {
for (const [num, entry] of this.entries.entries()) {
if (!entry) {
continue;
}
const ref = Ref.get(num, entry.gen);
let obj;

try {
obj = this.fetch(ref);
} catch {
continue;
}
if (obj instanceof BaseStream) {
obj = obj.dict;
}
if (obj instanceof Dict && obj.has("Root")) {
return obj;
}
}
}

// nothing helps
throw new InvalidPDFException("Invalid PDF structure.");
}
Expand Down
1 change: 1 addition & 0 deletions test/pdfs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@
!bug1020858.pdf
!prefilled_f1040.pdf
!bug1050040.pdf
!issue18986.pdf
!bug1200096.pdf
!bug1068432.pdf
!issue12295.pdf
Expand Down
Binary file added test/pdfs/issue18986.pdf
Binary file not shown.
7 changes: 7 additions & 0 deletions test/test_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4727,6 +4727,13 @@
"link": false,
"type": "eq"
},
{
"id": "issue18986",
"file": "pdfs/issue18986.pdf",
"md5": "e147084fabd9677366f6ae3586dd311b",
"rounds": 1,
"type": "load"
},
{
"id": "issue6652",
"file": "pdfs/issue6652.pdf",
Expand Down
2 changes: 1 addition & 1 deletion test/unit/api_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ describe("api", function () {
expect(false).toEqual(true);
} catch (reason) {
expect(reason instanceof InvalidPDFException).toEqual(true);
expect(reason.message).toEqual("Invalid PDF structure.");
expect(reason.message).toEqual("Invalid Root reference.");
}

await loadingTask.destroy();
Expand Down