From 935d95b4629ce70363d6a4cf67951ae77fae7905 Mon Sep 17 00:00:00 2001 From: Tim van der Meij Date: Sat, 22 Aug 2020 22:21:38 +0200 Subject: [PATCH] Move the version logic from the document to the catalog The `Version` entry is part of the catalog, not of the document, so its logic should be placed there instead. The document should look in the catalog to fetch it, and not have knowledge of `catDict`, which is a member internal to the catalog. Moreover, make the version member private on the document instance. It's only used internally and was also never intended to be public. For users it's exposed by the `getMetadata` API endpoint as `PDFFormatVersion`. Finally, clarify how the version from the header and the version from the catalog are treated using a comment. --- src/core/document.js | 16 ++++++++++------ src/core/obj.js | 8 ++++++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/core/document.js b/src/core/document.js index 404b046da..3076490dc 100644 --- a/src/core/document.js +++ b/src/core/document.js @@ -552,6 +552,7 @@ class PDFDocument { this.stream = stream; this.xref = new XRef(stream, pdfManager); this._pagePromises = []; + this._version = null; const idCounters = { font: 0, @@ -574,9 +575,12 @@ class PDFDocument { parse(recoveryMode) { this.setup(recoveryMode); - const version = this.catalog.catDict.get("Version"); - if (isName(version)) { - this.pdfFormatVersion = version.name; + // The `checkHeader` method is called before this method and parses the + // version from the header. The specification states in section 7.5.2 + // that the version from the catalog, if present, should overwrite the + // version from the header. + if (this.catalog.version) { + this._version = this.catalog.version; } // Check if AcroForms are present in the document. @@ -693,9 +697,9 @@ class PDFDocument { } version += String.fromCharCode(ch); } - if (!this.pdfFormatVersion) { + if (!this._version) { // Remove the "%PDF-" prefix. - this.pdfFormatVersion = version.substring(5); + this._version = version.substring(5); } } @@ -727,7 +731,7 @@ class PDFDocument { Trapped: isName, }; - let version = this.pdfFormatVersion; + let version = this._version; if ( typeof version !== "string" || !PDF_HEADER_VERSION_REGEXP.test(version) diff --git a/src/core/obj.js b/src/core/obj.js index 3eb437fbe..47dcd902b 100644 --- a/src/core/obj.js +++ b/src/core/obj.js @@ -76,6 +76,14 @@ class Catalog { this.pageKidsCountCache = new RefSetCache(); } + get version() { + const version = this.catDict.get("Version"); + if (!isName(version)) { + return shadow(this, "version", null); + } + return shadow(this, "version", version.name); + } + get metadata() { const streamRef = this.catDict.getRaw("Metadata"); if (!isRef(streamRef)) {