Skip to content

Commit c78eebb

Browse files
Merge pull request #19007 from Snuffleupagus/issue-18986
Try to improve handling of missing trailer dictionaries in `XRef.indexObjects` (issue 18986)
2 parents cbb02eb + e92a929 commit c78eebb

File tree

5 files changed

+34
-1
lines changed

5 files changed

+34
-1
lines changed

src/core/xref.js

+25
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,31 @@ class XRef {
680680
if (this.topDict) {
681681
return this.topDict;
682682
}
683+
684+
// When no trailer dictionary candidate exists, try picking the first
685+
// dictionary that contains a /Root entry (fixes issue18986.pdf).
686+
if (!trailerDicts.length) {
687+
for (const [num, entry] of this.entries.entries()) {
688+
if (!entry) {
689+
continue;
690+
}
691+
const ref = Ref.get(num, entry.gen);
692+
let obj;
693+
694+
try {
695+
obj = this.fetch(ref);
696+
} catch {
697+
continue;
698+
}
699+
if (obj instanceof BaseStream) {
700+
obj = obj.dict;
701+
}
702+
if (obj instanceof Dict && obj.has("Root")) {
703+
return obj;
704+
}
705+
}
706+
}
707+
683708
// nothing helps
684709
throw new InvalidPDFException("Invalid PDF structure.");
685710
}

test/pdfs/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@
156156
!bug1020858.pdf
157157
!prefilled_f1040.pdf
158158
!bug1050040.pdf
159+
!issue18986.pdf
159160
!bug1200096.pdf
160161
!bug1068432.pdf
161162
!issue12295.pdf

test/pdfs/issue18986.pdf

862 Bytes
Binary file not shown.

test/test_manifest.json

+7
Original file line numberDiff line numberDiff line change
@@ -4727,6 +4727,13 @@
47274727
"link": false,
47284728
"type": "eq"
47294729
},
4730+
{
4731+
"id": "issue18986",
4732+
"file": "pdfs/issue18986.pdf",
4733+
"md5": "e147084fabd9677366f6ae3586dd311b",
4734+
"rounds": 1,
4735+
"type": "load"
4736+
},
47304737
{
47314738
"id": "issue6652",
47324739
"file": "pdfs/issue6652.pdf",

test/unit/api_spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ describe("api", function () {
623623
expect(false).toEqual(true);
624624
} catch (reason) {
625625
expect(reason instanceof InvalidPDFException).toEqual(true);
626-
expect(reason.message).toEqual("Invalid PDF structure.");
626+
expect(reason.message).toEqual("Invalid Root reference.");
627627
}
628628

629629
await loadingTask.destroy();

0 commit comments

Comments
 (0)