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:
parent
525cc733d2
commit
935d95b462
@ -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)
|
||||
|
@ -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)) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user