diff --git a/src/core/xref.js b/src/core/xref.js index 313f18a71d917..75e5d1626d4e1 100644 --- a/src/core/xref.js +++ b/src/core/xref.js @@ -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."); } diff --git a/test/pdfs/.gitignore b/test/pdfs/.gitignore index b10be1106d6b6..4bf8b655473e2 100644 --- a/test/pdfs/.gitignore +++ b/test/pdfs/.gitignore @@ -156,6 +156,7 @@ !bug1020858.pdf !prefilled_f1040.pdf !bug1050040.pdf +!issue18986.pdf !bug1200096.pdf !bug1068432.pdf !issue12295.pdf diff --git a/test/pdfs/issue18986.pdf b/test/pdfs/issue18986.pdf new file mode 100644 index 0000000000000..f23047bf73c0b Binary files /dev/null and b/test/pdfs/issue18986.pdf differ diff --git a/test/test_manifest.json b/test/test_manifest.json index 7cfd0356ee2fc..65cff4331dbd0 100644 --- a/test/test_manifest.json +++ b/test/test_manifest.json @@ -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", diff --git a/test/unit/api_spec.js b/test/unit/api_spec.js index bb78c9c32b31d..62380c6d3dba6 100644 --- a/test/unit/api_spec.js +++ b/test/unit/api_spec.js @@ -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();