Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return arr;
};
PDFParser.prototype.consumeExtraWhiteSpace = function() {
var chr;
while ((chr = this._data[this._offset]) === 10 || chr === 13 || chr === 0 || chr === 9 || chr === 12 || chr === 32 ) { this._offset++; }
};
// ---------------------------------------------------------------------------
// Add our functions to PDFKit
// ---------------------------------------------------------------------------
//noinspection JSUnresolvedVariable
pdfkit.prototype.getEmptyPageStats = function() {
this.emptyPageCountData = [
this.page._imageCount,
this.page.content.uncompressedLength,
this.page.resources.uncompressedLength,
this.page.dictionary.uncompressedLength
];
};
//noinspection JSUnresolvedVariable
pdfkit.prototype.isEmptyPage = function() {
// Verify we have a valid page, and a valid pdfkit page
if (!this.page || !this.page.document) {
return false;
}
if (!Array.isArray(this.emptyPageCountData)) {
}
if (this.page.dictionary && !this.page.dictionary.offset) {
this._waiting--;
}
if (this.page.resources && !this.page.resources.offset) {
this._waiting--;
}
this.page = { end: function() {} };
}
};
// ---------------------------------------------------------------------------
// We need to Hijack the PDFKit Document End and _Finalize to support deletion and auto-deletion
// ---------------------------------------------------------------------------
//noinspection JSUnresolvedVariable
pdfkit.prototype._PKIEnd = pdfkit.prototype.end;
//noinspection JSUnresolvedVariable
pdfkit.prototype.end = function() {
if (this.isEmptyPage()) {
this.deletePage();
}
this._PKIEnd();
};
//noinspection JSUnresolvedVariable
pdfkit.prototype._PKIFinalize = pdfkit.prototype._finalize;
//noinspection JSUnresolvedVariable
pdfkit.prototype._finalize = function(fn) {
var i = 0;
while (i < this._offsets.length) {
//noinspection JSUnresolvedVariable
pdfkit.prototype._PKIEnd = pdfkit.prototype.end;
//noinspection JSUnresolvedVariable
pdfkit.prototype.end = function() {
if (this.isEmptyPage()) {
this.deletePage();
}
this._PKIEnd();
};
//noinspection JSUnresolvedVariable
pdfkit.prototype._PKIFinalize = pdfkit.prototype._finalize;
//noinspection JSUnresolvedVariable
pdfkit.prototype._finalize = function(fn) {
var i = 0;
while (i < this._offsets.length) {
// Eliminate any NULL Offsets from deleted pages
if (this._offsets[i] === null) {
this._offsets.splice(i, 1);
continue;
}
i++;
}
this._PKIFinalize(fn);
};
//noinspection JSUnresolvedVariable
/**
* This imports all the PDF pages
* @param data - this is the PDF document buffer
// ---------------------------------------------------------------------------
// Add our functions to PDFKit
// ---------------------------------------------------------------------------
//noinspection JSUnresolvedVariable
pdfkit.prototype.getEmptyPageStats = function() {
this.emptyPageCountData = [
this.page._imageCount,
this.page.content.uncompressedLength,
this.page.resources.uncompressedLength,
this.page.dictionary.uncompressedLength
];
};
//noinspection JSUnresolvedVariable
pdfkit.prototype.isEmptyPage = function() {
// Verify we have a valid page, and a valid pdfkit page
if (!this.page || !this.page.document) {
return false;
}
if (!Array.isArray(this.emptyPageCountData)) {
this.emptyPageCountData = [0,18,0,0];
}
if (this.page._imageCount > this.emptyPageCountData[0]) { // 0;
return false;
}
// A Transform is placed in the content on a new page and its length is 18
if (this.page.content.uncompressedLength > this.emptyPageCountData[1]) { // 18
return false;
// ---------------------------------------------------------------------------
// We need to Hijack the PDFKit Document End and _Finalize to support deletion and auto-deletion
// ---------------------------------------------------------------------------
//noinspection JSUnresolvedVariable
pdfkit.prototype._PKIEnd = pdfkit.prototype.end;
//noinspection JSUnresolvedVariable
pdfkit.prototype.end = function() {
if (this.isEmptyPage()) {
this.deletePage();
}
this._PKIEnd();
};
//noinspection JSUnresolvedVariable
pdfkit.prototype._PKIFinalize = pdfkit.prototype._finalize;
//noinspection JSUnresolvedVariable
pdfkit.prototype._finalize = function(fn) {
var i = 0;
while (i < this._offsets.length) {
// Eliminate any NULL Offsets from deleted pages
if (this._offsets[i] === null) {
this._offsets.splice(i, 1);
continue;
}
i++;
}
this._PKIFinalize(fn);
};
//noinspection JSUnresolvedVariable
return false;
}
//noinspection RedundantIfStatementJS
if (this.page.dictionary.uncompressedLength > this.emptyPageCountData[3]) { // 0
return false;
}
return true;
};
// ---------------------------------------------------------------------------
// This will not remove the page data that is already written and embedded in the PDF already
// But it will remove the index to the page.
// If the page has not been written yet, it will then be eliminated from the pdf
// ---------------------------------------------------------------------------
//noinspection JSUnresolvedVariable
pdfkit.prototype.deletePage = function(id) {
var pages = this._root.data.Pages.data;
if (id >= 0 && id < pages.Kids.length) {
var delPage = pages.Kids[id];
pages.Kids.splice(id, 1);
pages.Count--;
// Only the current (last) page can be deleted and the stuff removed from the stream
if (this.page.dictionary.id === delPage.id) {
// If we haven't called ".end" on these Page Refs; we aren't going to but we need to
// Decrease the reference count
if (this.page.content && !this.page.content.offset) {
this._waiting--;
}
if (this.page.dictionary && !this.page.dictionary.offset) {
} else {
console.error("Missing Page: ", pageIdx);
return false;
}
}
} else if (pdfObjects[idx].data.Type === "Page") {
this._handleImportOfPage(pdfObjects, idx);
} else {
console.error("Missing Page: ", idx);
return false;
}
return true;
};
//noinspection JSUnresolvedVariable
pdfkit.prototype._handleImportOfPage = function(pdfObjects, idx) {
var page = new PDFImportedPage(this, pdfObjects, idx);
this._root.data.Pages.data.Kids.push(page.dictionary);
this._root.data.Pages.data.Count++;
};
pdfkit.prototype.textRotate = function(angle, x, y) {
var cos, rad, sin;
rad = angle * Math.PI / 180;
cos = Math.cos(rad);
sin = Math.sin(rad);
this.transform(cos, sin, -sin, cos, x, y);
};
//noinspection JSUnresolvedVariable
module.exports = pdfkit;
this._handleImportOfPage(pdfObjects, idx);
} else {
console.error("Missing Page: ", idx);
return false;
}
return true;
};
//noinspection JSUnresolvedVariable
pdfkit.prototype._handleImportOfPage = function(pdfObjects, idx) {
var page = new PDFImportedPage(this, pdfObjects, idx);
this._root.data.Pages.data.Kids.push(page.dictionary);
this._root.data.Pages.data.Count++;
};
pdfkit.prototype.textRotate = function(angle, x, y) {
var cos, rad, sin;
rad = angle * Math.PI / 180;
cos = Math.cos(rad);
sin = Math.sin(rad);
this.transform(cos, sin, -sin, cos, x, y);
};
//noinspection JSUnresolvedVariable
module.exports = pdfkit;
if (this._offsets[i] === null) {
this._offsets.splice(i, 1);
continue;
}
i++;
}
this._PKIFinalize(fn);
};
//noinspection JSUnresolvedVariable
/**
* This imports all the PDF pages
* @param data - this is the PDF document buffer
* @param options - the only current option is "denormalize"
*/
pdfkit.prototype.importPDF = function(data, options) {
this._waiting++;
var emptyPage = this.isEmptyPage();
var curPage;
var completed;
var versionId;
var pages = this._root.data.Pages.data;
options = options || {};
this._writtenReferences = {};
try {
//noinspection JSUnresolvedVariable
if (!Buffer.isBuffer(data)) {
//noinspection JSUnresolvedFunction
data = Buffer.from(data, 'binary');
}