pdf-lib
Version:
Library for creating and modifying PDF files in JavaScript
57 lines (56 loc) • 2.94 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
}
Object.defineProperty(exports, "__esModule", { value: true });
var pdf_structures_1 = require("../pdf-structures");
var utils_1 = require("../../utils");
var parseIndirectObj_1 = __importDefault(require("./parseIndirectObj"));
var parseTrailer_1 = require("./parseTrailer");
var parseXRefTable_1 = __importDefault(require("./parseXRefTable"));
/**
* Accepts an array of bytes as input. Checks to see if the first characters in the
* trimmed input make up a PDF Linearization Param Dict, followed by an xref table
* or stream, and finally a trailer.
*
* If so, returns a tuple containing (1) an object representing those three objects
* and (2) a subarray of the input with the characters making up the parsed objects
* removed. The "onParseDict" parse handler will be called with the linearization
* param dict. The "onParseLinearization" parse handler will also be called with
* objects representing the three parsed linearization objects.
*
* If not, null is returned.
*/
var parseLinearization = function (input, index, parseHandlers) {
if (parseHandlers === void 0) { parseHandlers = {}; }
var trimmed = utils_1.trimArray(input);
// Try to parse a dictionary from the input
var paramDictMatch = parseIndirectObj_1.default(trimmed, index, parseHandlers);
if (!paramDictMatch)
return undefined;
// Make sure it is a Linearization Param Dictionary
var _a = paramDictMatch, paramDict = _a[0], remaining1 = _a[1];
if (!(paramDict.pdfObject instanceof pdf_structures_1.PDFLinearizationParams)) {
return undefined;
}
// TODO: Do the parseHandlers really need to be passed to parseIndirectObj?
// Next we should find a cross reference stream or xref table
var xrefMatch = parseXRefTable_1.default(remaining1) ||
parseIndirectObj_1.default(remaining1, index, parseHandlers) ||
utils_1.error('Found Linearization param dict but no first page xref table or stream.');
var _b = xrefMatch, xref = _b[0], remaining2 = _b[1];
var trailerMatch = parseTrailer_1.parseTrailer(remaining2, index) ||
parseTrailer_1.parseTrailerWithoutDict(remaining2, index);
// Per the PDF spec, a trailer should always be present - but some PDFs in the
// wild are missing them anyways
if (!trailerMatch) {
console.warn('Found Linearization param dict and cross reference index, but no associated trailer.');
}
var _c = trailerMatch || [undefined, undefined], trailer = _c[0], remaining3 = _c[1];
var linearization = { paramDict: paramDict, xref: xref, trailer: trailer };
if (parseHandlers.onParseLinearization) {
parseHandlers.onParseLinearization(linearization);
}
return [linearization, remaining3 || remaining2];
};
exports.default = parseLinearization;
;