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.
This commit is contained in:
Tim van der Meij 2020-08-22 22:21:38 +02:00
parent 525cc733d2
commit 935d95b462
No known key found for this signature in database
GPG Key ID: 8C3FD2925A5F2762
2 changed files with 18 additions and 6 deletions

View File

@ -552,6 +552,7 @@ class PDFDocument {
this.stream = stream; this.stream = stream;
this.xref = new XRef(stream, pdfManager); this.xref = new XRef(stream, pdfManager);
this._pagePromises = []; this._pagePromises = [];
this._version = null;
const idCounters = { const idCounters = {
font: 0, font: 0,
@ -574,9 +575,12 @@ class PDFDocument {
parse(recoveryMode) { parse(recoveryMode) {
this.setup(recoveryMode); this.setup(recoveryMode);
const version = this.catalog.catDict.get("Version"); // The `checkHeader` method is called before this method and parses the
if (isName(version)) { // version from the header. The specification states in section 7.5.2
this.pdfFormatVersion = version.name; // 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. // Check if AcroForms are present in the document.
@ -693,9 +697,9 @@ class PDFDocument {
} }
version += String.fromCharCode(ch); version += String.fromCharCode(ch);
} }
if (!this.pdfFormatVersion) { if (!this._version) {
// Remove the "%PDF-" prefix. // Remove the "%PDF-" prefix.
this.pdfFormatVersion = version.substring(5); this._version = version.substring(5);
} }
} }
@ -727,7 +731,7 @@ class PDFDocument {
Trapped: isName, Trapped: isName,
}; };
let version = this.pdfFormatVersion; let version = this._version;
if ( if (
typeof version !== "string" || typeof version !== "string" ||
!PDF_HEADER_VERSION_REGEXP.test(version) !PDF_HEADER_VERSION_REGEXP.test(version)

View File

@ -76,6 +76,14 @@ class Catalog {
this.pageKidsCountCache = new RefSetCache(); 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() { get metadata() {
const streamRef = this.catDict.getRaw("Metadata"); const streamRef = this.catDict.getRaw("Metadata");
if (!isRef(streamRef)) { if (!isRef(streamRef)) {