Generic build of PDF.js library.

615 lines
20 KiB

/* Copyright 2012 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
14 years ago
import {
assert, error, info, isArray, isArrayBuffer, isNum, isSpace, isString,
MissingDataException, OPS, shadow, stringToBytes, stringToPDFString, Util,
warn
} from '../shared/util';
import { Catalog, ObjectLoader, XRef } from './obj';
import { Dict, isDict, isName, isStream } from './primitives';
import { NullStream, Stream, StreamsSequenceStream } from './stream';
import { OperatorList, PartialEvaluator } from './evaluator';
import { AnnotationFactory } from './annotation';
import { calculateMD5 } from './crypto';
import { Linearization } from './parser';
var Page = (function PageClosure() {
var DEFAULT_USER_UNIT = 1.0;
var LETTER_SIZE_MEDIABOX = [0, 0, 612, 792];
function isAnnotationRenderable(annotation, intent) {
return (intent === 'display' && annotation.viewable) ||
(intent === 'print' && annotation.printable);
}
function Page(pdfManager, xref, pageIndex, pageDict, ref, fontCache,
builtInCMapCache) {
this.pdfManager = pdfManager;
this.pageIndex = pageIndex;
14 years ago
this.pageDict = pageDict;
this.xref = xref;
this.ref = ref;
this.fontCache = fontCache;
this.builtInCMapCache = builtInCMapCache;
this.evaluatorOptions = pdfManager.evaluatorOptions;
this.resourcesPromise = null;
var uniquePrefix = 'p' + this.pageIndex + '_';
var idCounters = {
obj: 0,
};
this.idFactory = {
createObjId() {
return uniquePrefix + (++idCounters.obj);
},
};
14 years ago
}
Page.prototype = {
getPageProp: function Page_getPageProp(key) {
return this.pageDict.get(key);
14 years ago
},
getInheritedPageProp: function Page_getInheritedPageProp(key, getArray) {
var dict = this.pageDict, valueArray = null, loopCount = 0;
var MAX_LOOP_COUNT = 100;
getArray = getArray || false;
// Always walk up the entire parent chain, to be able to find
// e.g. \Resources placed on multiple levels of the tree.
while (dict) {
var value = getArray ? dict.getArray(key) : dict.get(key);
if (value !== undefined) {
if (!valueArray) {
valueArray = [];
}
valueArray.push(value);
}
if (++loopCount > MAX_LOOP_COUNT) {
warn('getInheritedPageProp: maximum loop count exceeded for ' + key);
return valueArray ? valueArray[0] : undefined;
}
dict = dict.get('Parent');
14 years ago
}
if (!valueArray) {
return undefined;
}
if (valueArray.length === 1 || !isDict(valueArray[0])) {
return valueArray[0];
}
return Dict.merge(this.xref, valueArray);
14 years ago
},
14 years ago
get content() {
return this.getPageProp('Contents');
14 years ago
},
14 years ago
get resources() {
// For robustness: The spec states that a \Resources entry has to be
// present, but can be empty. Some document omit it still, in this case
// we return an empty dictionary.
return shadow(this, 'resources',
this.getInheritedPageProp('Resources') || Dict.empty);
14 years ago
},
14 years ago
get mediaBox() {
var mediaBox = this.getInheritedPageProp('MediaBox', true);
14 years ago
// Reset invalid media box to letter size.
if (!isArray(mediaBox) || mediaBox.length !== 4) {
return shadow(this, 'mediaBox', LETTER_SIZE_MEDIABOX);
}
return shadow(this, 'mediaBox', mediaBox);
},
get cropBox() {
var cropBox = this.getInheritedPageProp('CropBox', true);
// Reset invalid crop box to media box.
if (!isArray(cropBox) || cropBox.length !== 4) {
return shadow(this, 'cropBox', this.mediaBox);
}
return shadow(this, 'cropBox', cropBox);
14 years ago
},
get userUnit() {
var obj = this.getPageProp('UserUnit');
if (!isNum(obj) || obj <= 0) {
obj = DEFAULT_USER_UNIT;
}
return shadow(this, 'userUnit', obj);
},
14 years ago
get view() {
13 years ago
// From the spec, 6th ed., p.963:
// "The crop, bleed, trim, and art boxes should not ordinarily
// extend beyond the boundaries of the media box. If they do, they are
// effectively reduced to their intersection with the media box."
var mediaBox = this.mediaBox, cropBox = this.cropBox;
if (mediaBox === cropBox) {
return shadow(this, 'view', mediaBox);
}
var intersection = Util.intersect(cropBox, mediaBox);
return shadow(this, 'view', intersection || mediaBox);
14 years ago
},
14 years ago
get rotate() {
var rotate = this.getInheritedPageProp('Rotate') || 0;
14 years ago
// Normalize rotation so it's a multiple of 90 and between 0 and 270
if (rotate % 90 !== 0) {
14 years ago
rotate = 0;
} else if (rotate >= 360) {
rotate = rotate % 360;
} else if (rotate < 0) {
// The spec doesn't cover negatives, assume its counterclockwise
// rotation. The following is the other implementation of modulo.
rotate = ((rotate % 360) + 360) % 360;
}
return shadow(this, 'rotate', rotate);
},
getContentStream: function Page_getContentStream() {
var content = this.content;
var stream;
14 years ago
if (isArray(content)) {
// fetching items
var xref = this.xref;
14 years ago
var i, n = content.length;
var streams = [];
for (i = 0; i < n; ++i) {
streams.push(xref.fetchIfRef(content[i]));
}
stream = new StreamsSequenceStream(streams);
} else if (isStream(content)) {
stream = content;
} else {
// replacing non-existent page content with empty one
stream = new NullStream();
14 years ago
}
return stream;
},
loadResources: function Page_loadResources(keys) {
if (!this.resourcesPromise) {
// TODO: add async getInheritedPageProp and remove this.
this.resourcesPromise = this.pdfManager.ensure(this, 'resources');
}
return this.resourcesPromise.then(() => {
var objectLoader = new ObjectLoader(this.resources.map,
keys,
this.xref);
return objectLoader.load();
});
},
Change the signatures of the `PartialEvaluator` "constructor" and its `getOperatorList`/`getTextContent` methods to take parameter objects Currently these methods accept a large number of parameters, which creates quite unwieldy call-sites. When invoking them, you have to remember not only what arguments to supply, but also the correct order, to avoid runtime errors. Furthermore, since some of the parameters are optional, you also have to remember to pass e.g. `null` or `undefined` for those ones. Also, adding new parameters to these methods (which happens occasionally), often becomes unnecessarily tedious (based on personal experience). Please note that I do *not* think that we need/should convert *every* single method in `evaluator.js` (or elsewhere in `/core` files) to take parameter objects. However, in my opinion, once a method starts relying on approximately five parameter (or even more), passing them in individually becomes quite cumbersome. With these changes, I obviously needed to update the `evaluator_spec.js` unit-tests. The main change there, except the new method signatures[1], is that it's now re-using *one* `PartialEvalutor` instance, since I couldn't see any compelling reason for creating a new one in every single test. *Note:* If this patch is accepted, my intention is to (time permitting) see if it makes sense to convert additional methods in `evaluator.js` (and other `/core` files) in a similar fashion, but I figured that it'd be a good idea to limit the initial scope somewhat. --- [1] A fun fact here, note how the `PartialEvaluator` signature used in `evaluator_spec.js` wasn't even correct in the current `master`.
8 years ago
getOperatorList({ handler, task, intent, renderInteractiveForms, }) {
var contentStreamPromise = this.pdfManager.ensure(this,
'getContentStream');
var resourcesPromise = this.loadResources([
'ExtGState',
'ColorSpace',
'Pattern',
'Shading',
'XObject',
'Font'
// ProcSet
// Properties
]);
Change the signatures of the `PartialEvaluator` "constructor" and its `getOperatorList`/`getTextContent` methods to take parameter objects Currently these methods accept a large number of parameters, which creates quite unwieldy call-sites. When invoking them, you have to remember not only what arguments to supply, but also the correct order, to avoid runtime errors. Furthermore, since some of the parameters are optional, you also have to remember to pass e.g. `null` or `undefined` for those ones. Also, adding new parameters to these methods (which happens occasionally), often becomes unnecessarily tedious (based on personal experience). Please note that I do *not* think that we need/should convert *every* single method in `evaluator.js` (or elsewhere in `/core` files) to take parameter objects. However, in my opinion, once a method starts relying on approximately five parameter (or even more), passing them in individually becomes quite cumbersome. With these changes, I obviously needed to update the `evaluator_spec.js` unit-tests. The main change there, except the new method signatures[1], is that it's now re-using *one* `PartialEvalutor` instance, since I couldn't see any compelling reason for creating a new one in every single test. *Note:* If this patch is accepted, my intention is to (time permitting) see if it makes sense to convert additional methods in `evaluator.js` (and other `/core` files) in a similar fashion, but I figured that it'd be a good idea to limit the initial scope somewhat. --- [1] A fun fact here, note how the `PartialEvaluator` signature used in `evaluator_spec.js` wasn't even correct in the current `master`.
8 years ago
var partialEvaluator = new PartialEvaluator({
pdfManager: this.pdfManager,
xref: this.xref,
handler,
pageIndex: this.pageIndex,
idFactory: this.idFactory,
fontCache: this.fontCache,
builtInCMapCache: this.builtInCMapCache,
options: this.evaluatorOptions,
});
var dataPromises = Promise.all([contentStreamPromise, resourcesPromise]);
var pageListPromise = dataPromises.then(([contentStream]) => {
var opList = new OperatorList(intent, handler, this.pageIndex);
handler.send('StartRenderPage', {
transparency: partialEvaluator.hasBlendModes(this.resources),
pageIndex: this.pageIndex,
intent,
});
Change the signatures of the `PartialEvaluator` "constructor" and its `getOperatorList`/`getTextContent` methods to take parameter objects Currently these methods accept a large number of parameters, which creates quite unwieldy call-sites. When invoking them, you have to remember not only what arguments to supply, but also the correct order, to avoid runtime errors. Furthermore, since some of the parameters are optional, you also have to remember to pass e.g. `null` or `undefined` for those ones. Also, adding new parameters to these methods (which happens occasionally), often becomes unnecessarily tedious (based on personal experience). Please note that I do *not* think that we need/should convert *every* single method in `evaluator.js` (or elsewhere in `/core` files) to take parameter objects. However, in my opinion, once a method starts relying on approximately five parameter (or even more), passing them in individually becomes quite cumbersome. With these changes, I obviously needed to update the `evaluator_spec.js` unit-tests. The main change there, except the new method signatures[1], is that it's now re-using *one* `PartialEvalutor` instance, since I couldn't see any compelling reason for creating a new one in every single test. *Note:* If this patch is accepted, my intention is to (time permitting) see if it makes sense to convert additional methods in `evaluator.js` (and other `/core` files) in a similar fashion, but I figured that it'd be a good idea to limit the initial scope somewhat. --- [1] A fun fact here, note how the `PartialEvaluator` signature used in `evaluator_spec.js` wasn't even correct in the current `master`.
8 years ago
return partialEvaluator.getOperatorList({
stream: contentStream,
task,
resources: this.resources,
operatorList: opList,
}).then(function () {
return opList;
});
});
// Fetch the page's annotations and add their operator lists to the
// page's operator list to render them.
Change the signatures of the `PartialEvaluator` "constructor" and its `getOperatorList`/`getTextContent` methods to take parameter objects Currently these methods accept a large number of parameters, which creates quite unwieldy call-sites. When invoking them, you have to remember not only what arguments to supply, but also the correct order, to avoid runtime errors. Furthermore, since some of the parameters are optional, you also have to remember to pass e.g. `null` or `undefined` for those ones. Also, adding new parameters to these methods (which happens occasionally), often becomes unnecessarily tedious (based on personal experience). Please note that I do *not* think that we need/should convert *every* single method in `evaluator.js` (or elsewhere in `/core` files) to take parameter objects. However, in my opinion, once a method starts relying on approximately five parameter (or even more), passing them in individually becomes quite cumbersome. With these changes, I obviously needed to update the `evaluator_spec.js` unit-tests. The main change there, except the new method signatures[1], is that it's now re-using *one* `PartialEvalutor` instance, since I couldn't see any compelling reason for creating a new one in every single test. *Note:* If this patch is accepted, my intention is to (time permitting) see if it makes sense to convert additional methods in `evaluator.js` (and other `/core` files) in a similar fashion, but I figured that it'd be a good idea to limit the initial scope somewhat. --- [1] A fun fact here, note how the `PartialEvaluator` signature used in `evaluator_spec.js` wasn't even correct in the current `master`.
8 years ago
var annotationsPromise = this.pdfManager.ensure(this, 'annotations');
return Promise.all([pageListPromise, annotationsPromise]).then(
function ([pageOpList, annotations]) {
if (annotations.length === 0) {
pageOpList.flush(true);
return pageOpList;
}
// Collect the operator list promises for the annotations. Each promise
// is resolved with the complete operator list for a single annotation.
var i, ii, opListPromises = [];
for (i = 0, ii = annotations.length; i < ii; i++) {
if (isAnnotationRenderable(annotations[i], intent)) {
opListPromises.push(annotations[i].getOperatorList(
partialEvaluator, task, renderInteractiveForms));
}
}
return Promise.all(opListPromises).then(function(opLists) {
pageOpList.addOp(OPS.beginAnnotations, []);
for (i = 0, ii = opLists.length; i < ii; i++) {
pageOpList.addOpList(opLists[i]);
}
pageOpList.addOp(OPS.endAnnotations, []);
pageOpList.flush(true);
return pageOpList;
});
});
14 years ago
},
Change the signatures of the `PartialEvaluator` "constructor" and its `getOperatorList`/`getTextContent` methods to take parameter objects Currently these methods accept a large number of parameters, which creates quite unwieldy call-sites. When invoking them, you have to remember not only what arguments to supply, but also the correct order, to avoid runtime errors. Furthermore, since some of the parameters are optional, you also have to remember to pass e.g. `null` or `undefined` for those ones. Also, adding new parameters to these methods (which happens occasionally), often becomes unnecessarily tedious (based on personal experience). Please note that I do *not* think that we need/should convert *every* single method in `evaluator.js` (or elsewhere in `/core` files) to take parameter objects. However, in my opinion, once a method starts relying on approximately five parameter (or even more), passing them in individually becomes quite cumbersome. With these changes, I obviously needed to update the `evaluator_spec.js` unit-tests. The main change there, except the new method signatures[1], is that it's now re-using *one* `PartialEvalutor` instance, since I couldn't see any compelling reason for creating a new one in every single test. *Note:* If this patch is accepted, my intention is to (time permitting) see if it makes sense to convert additional methods in `evaluator.js` (and other `/core` files) in a similar fashion, but I figured that it'd be a good idea to limit the initial scope somewhat. --- [1] A fun fact here, note how the `PartialEvaluator` signature used in `evaluator_spec.js` wasn't even correct in the current `master`.
8 years ago
extractTextContent({ handler, task, normalizeWhitespace,
combineTextItems, }) {
var contentStreamPromise = this.pdfManager.ensure(this,
'getContentStream');
var resourcesPromise = this.loadResources([
'ExtGState',
'XObject',
'Font'
]);
var dataPromises = Promise.all([contentStreamPromise, resourcesPromise]);
return dataPromises.then(([contentStream]) => {
Change the signatures of the `PartialEvaluator` "constructor" and its `getOperatorList`/`getTextContent` methods to take parameter objects Currently these methods accept a large number of parameters, which creates quite unwieldy call-sites. When invoking them, you have to remember not only what arguments to supply, but also the correct order, to avoid runtime errors. Furthermore, since some of the parameters are optional, you also have to remember to pass e.g. `null` or `undefined` for those ones. Also, adding new parameters to these methods (which happens occasionally), often becomes unnecessarily tedious (based on personal experience). Please note that I do *not* think that we need/should convert *every* single method in `evaluator.js` (or elsewhere in `/core` files) to take parameter objects. However, in my opinion, once a method starts relying on approximately five parameter (or even more), passing them in individually becomes quite cumbersome. With these changes, I obviously needed to update the `evaluator_spec.js` unit-tests. The main change there, except the new method signatures[1], is that it's now re-using *one* `PartialEvalutor` instance, since I couldn't see any compelling reason for creating a new one in every single test. *Note:* If this patch is accepted, my intention is to (time permitting) see if it makes sense to convert additional methods in `evaluator.js` (and other `/core` files) in a similar fashion, but I figured that it'd be a good idea to limit the initial scope somewhat. --- [1] A fun fact here, note how the `PartialEvaluator` signature used in `evaluator_spec.js` wasn't even correct in the current `master`.
8 years ago
var partialEvaluator = new PartialEvaluator({
pdfManager: this.pdfManager,
xref: this.xref,
handler,
pageIndex: this.pageIndex,
idFactory: this.idFactory,
fontCache: this.fontCache,
builtInCMapCache: this.builtInCMapCache,
options: this.evaluatorOptions,
});
return partialEvaluator.getTextContent({
stream: contentStream,
task,
resources: this.resources,
normalizeWhitespace,
combineTextItems,
});
});
14 years ago
},
getAnnotationsData: function Page_getAnnotationsData(intent) {
var annotations = this.annotations;
var annotationsData = [];
for (var i = 0, n = annotations.length; i < n; ++i) {
if (!intent || isAnnotationRenderable(annotations[i], intent)) {
annotationsData.push(annotations[i].data);
}
}
return annotationsData;
},
get annotations() {
var annotations = [];
var annotationRefs = this.getInheritedPageProp('Annots') || [];
var annotationFactory = new AnnotationFactory();
for (var i = 0, n = annotationRefs.length; i < n; ++i) {
var annotationRef = annotationRefs[i];
var annotation = annotationFactory.create(this.xref, annotationRef,
this.pdfManager,
this.idFactory);
if (annotation) {
annotations.push(annotation);
}
14 years ago
}
return shadow(this, 'annotations', annotations);
14 years ago
}
};
return Page;
14 years ago
})();
/**
13 years ago
* The `PDFDocument` holds all the data of the PDF file. Compared to the
14 years ago
* `PDFDoc`, this one doesn't have any job management code.
13 years ago
* Right now there exists one PDFDocument on the main thread + one object
14 years ago
* for each worker. If there is no worker support enabled, there are two
13 years ago
* `PDFDocument` objects on the main thread created.
14 years ago
*/
13 years ago
var PDFDocument = (function PDFDocumentClosure() {
var FINGERPRINT_FIRST_BYTES = 1024;
var EMPTY_FINGERPRINT = '\x00\x00\x00\x00\x00\x00\x00' +
'\x00\x00\x00\x00\x00\x00\x00\x00\x00';
function PDFDocument(pdfManager, arg) {
var stream;
if (isStream(arg)) {
stream = arg;
} else if (isArrayBuffer(arg)) {
stream = new Stream(arg);
} else {
13 years ago
error('PDFDocument: Unknown argument type');
}
assert(stream.length > 0, 'stream must have data');
this.pdfManager = pdfManager;
14 years ago
this.stream = stream;
this.xref = new XRef(stream, pdfManager);
14 years ago
}
function find(stream, needle, limit, backwards) {
var pos = stream.pos;
var end = stream.end;
var strBuf = [];
if (pos + limit > end) {
14 years ago
limit = end - pos;
}
for (var n = 0; n < limit; ++n) {
strBuf.push(String.fromCharCode(stream.getByte()));
}
var str = strBuf.join('');
14 years ago
stream.pos = pos;
var index = backwards ? str.lastIndexOf(needle) : str.indexOf(needle);
if (index === -1) {
14 years ago
return false; /* not found */
}
14 years ago
stream.pos += index;
return true; /* found */
}
var DocumentInfoValidators = {
get entries() {
// Lazily build this since all the validation functions below are not
// defined until after this file loads.
return shadow(this, 'entries', {
Title: isString,
Author: isString,
Subject: isString,
Keywords: isString,
Creator: isString,
Producer: isString,
CreationDate: isString,
ModDate: isString,
Trapped: isName
});
}
};
13 years ago
PDFDocument.prototype = {
parse: function PDFDocument_parse(recoveryMode) {
this.setup(recoveryMode);
var version = this.catalog.catDict.get('Version');
if (isName(version)) {
this.pdfFormatVersion = version.name;
}
try {
// checking if AcroForm is present
this.acroForm = this.catalog.catDict.get('AcroForm');
if (this.acroForm) {
this.xfa = this.acroForm.get('XFA');
var fields = this.acroForm.get('Fields');
if ((!fields || !isArray(fields) || fields.length === 0) &&
!this.xfa) {
// no fields and no XFA -- not a form (?)
this.acroForm = null;
}
}
} catch (ex) {
if (ex instanceof MissingDataException) {
throw ex;
}
info('Something wrong with AcroForm entry');
this.acroForm = null;
}
},
14 years ago
get linearization() {
var linearization = null;
if (this.stream.length) {
try {
linearization = Linearization.create(this.stream);
} catch (err) {
if (err instanceof MissingDataException) {
throw err;
}
info(err);
}
14 years ago
}
// shadow the prototype getter with a data property
return shadow(this, 'linearization', linearization);
},
get startXRef() {
var stream = this.stream;
var startXRef = 0;
var linearization = this.linearization;
if (linearization) {
// Find end of first obj.
stream.reset();
if (find(stream, 'endobj', 1024)) {
14 years ago
startXRef = stream.pos + 6;
}
14 years ago
} else {
14 years ago
// Find startxref by jumping backward from the end of the file.
var step = 1024;
var found = false, pos = stream.end;
14 years ago
while (!found && pos > 0) {
pos -= step - 'startxref'.length;
if (pos < 0) {
pos = 0;
}
stream.pos = pos;
14 years ago
found = find(stream, 'startxref', step, true);
}
if (found) {
14 years ago
stream.skip(9);
var ch;
do {
ch = stream.getByte();
} while (isSpace(ch));
14 years ago
var str = '';
while (ch >= 0x20 && ch <= 0x39) { // < '9'
str += String.fromCharCode(ch);
ch = stream.getByte();
14 years ago
}
startXRef = parseInt(str, 10);
if (isNaN(startXRef)) {
14 years ago
startXRef = 0;
}
14 years ago
}
}
// shadow the prototype getter with a data property
return shadow(this, 'startXRef', startXRef);
},
get mainXRefEntriesOffset() {
var mainXRefEntriesOffset = 0;
var linearization = this.linearization;
if (linearization) {
14 years ago
mainXRefEntriesOffset = linearization.mainXRefEntriesOffset;
}
14 years ago
// shadow the prototype getter with a data property
return shadow(this, 'mainXRefEntriesOffset', mainXRefEntriesOffset);
},
// Find the header, remove leading garbage and setup the stream
// starting from the header.
13 years ago
checkHeader: function PDFDocument_checkHeader() {
14 years ago
var stream = this.stream;
stream.reset();
if (find(stream, '%PDF-', 1024)) {
// Found the header, trim off any garbage before it.
stream.moveStart();
// Reading file format version
var MAX_VERSION_LENGTH = 12;
var version = '', ch;
while ((ch = stream.getByte()) > 0x20) { // SPACE
if (version.length >= MAX_VERSION_LENGTH) {
break;
}
version += String.fromCharCode(ch);
}
if (!this.pdfFormatVersion) {
// removing "%PDF-"-prefix
this.pdfFormatVersion = version.substring(5);
}
14 years ago
return;
}
// May not be a PDF file, continue anyway.
},
parseStartXRef: function PDFDocument_parseStartXRef() {
var startXRef = this.startXRef;
this.xref.setStartXRef(startXRef);
},
setup: function PDFDocument_setup(recoveryMode) {
this.xref.parse(recoveryMode);
var pageFactory = {
createPage: (pageIndex, dict, ref, fontCache, builtInCMapCache) => {
return new Page(this.pdfManager, this.xref, pageIndex, dict, ref,
fontCache, builtInCMapCache);
}
};
this.catalog = new Catalog(this.pdfManager, this.xref, pageFactory);
14 years ago
},
get numPages() {
var linearization = this.linearization;
var num = linearization ? linearization.numPages : this.catalog.numPages;
// shadow the prototype getter
return shadow(this, 'numPages', num);
},
get documentInfo() {
var docInfo = {
PDFFormatVersion: this.pdfFormatVersion,
IsAcroFormPresent: !!this.acroForm,
IsXFAPresent: !!this.xfa
};
var infoDict;
try {
infoDict = this.xref.trailer.get('Info');
} catch (err) {
if (err instanceof MissingDataException) {
throw err;
}
info('The document information dictionary is invalid.');
}
if (infoDict) {
var validEntries = DocumentInfoValidators.entries;
// Only fill the document info with valid entries from the spec.
for (var key in validEntries) {
if (infoDict.has(key)) {
var value = infoDict.get(key);
// Make sure the value conforms to the spec.
if (validEntries[key](value)) {
docInfo[key] = (typeof value !== 'string' ?
value : stringToPDFString(value));
} else {
info('Bad value in document info for "' + key + '"');
}
}
}
}
return shadow(this, 'documentInfo', docInfo);
},
get fingerprint() {
var xref = this.xref, hash, fileID = '';
var idArray = xref.trailer.get('ID');
if (idArray && isArray(idArray) && idArray[0] && isString(idArray[0]) &&
idArray[0] !== EMPTY_FINGERPRINT) {
hash = stringToBytes(idArray[0]);
} else {
if (this.stream.ensureRange) {
this.stream.ensureRange(0,
Math.min(FINGERPRINT_FIRST_BYTES, this.stream.end));
}
hash = calculateMD5(this.stream.bytes.subarray(0,
FINGERPRINT_FIRST_BYTES), 0, FINGERPRINT_FIRST_BYTES);
}
for (var i = 0, n = hash.length; i < n; i++) {
var hex = hash[i].toString(16);
fileID += hex.length === 1 ? '0' + hex : hex;
}
return shadow(this, 'fingerprint', fileID);
},
getPage: function PDFDocument_getPage(pageIndex) {
return this.catalog.getPage(pageIndex);
},
cleanup: function PDFDocument_cleanup() {
return this.catalog.cleanup();
14 years ago
}
};
13 years ago
return PDFDocument;
14 years ago
})();
export {
Page,
PDFDocument,
};