Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/core/writers/PDFStreamWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class PDFStreamWriter extends PDFWriter {
for (let idx = 0, len = indirectObjects.length; idx < len; idx++) {
const indirectObject = indirectObjects[idx];
const [ref, object] = indirectObject;
if (!this.snapshot.shouldSave(ref.objectNumber)) {
if (!this.shouldSave(incremental, ref.objectNumber, indirectObjects)) {
continue;
}

Expand Down
5 changes: 3 additions & 2 deletions src/core/writers/PDFWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ class PDFWriter {
// only the last XRef Stream will be regenerated on save
if (!this._lastXRefObjectNumber) {
// if no XRef Stream, then nothing should be skipped
this._lastXRefObjectNumber = this.context.largestObjectNumber + 1;
const checkWatermark = this._lastXRefObjectNumber - 10; // max number of objects in the final part of the PDF to check
// if we are adding a XRef Stream, then its number if this.context.largestObjectNumber + 1, so adding 10 ensures we won't skip current generade XRref Stream
this._lastXRefObjectNumber = this.context.largestObjectNumber + 10;
const checkWatermark = this._lastXRefObjectNumber - 20; // max number of objects in the final part of the PDF to check
// search the last XRef Stream, if there is one, objects are expected to be in object number order
for (let idx = objects.length - 1; idx > 0; idx--) {
// if not in last 'rangeToCheck' objects, there is none that should be skipped, most probably a linearized PDF, or without XRef Streams
Expand Down
27 changes: 27 additions & 0 deletions tests/api/PDFDocument.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,33 @@ describe('PDFDocument', () => {
const reloaded2 = await PDFDocument.load(noStmIB);
expect(reloaded2.getPageCount()).toBe(pdfDoc.getPageCount());
});

it('calculates correct pdf size', async () => {
const pdfDoc = await PDFDocument.load(v15PdfBytes);
const stmB = await pdfDoc.save();
const str = Buffer.from(stmB).toString();
let match = str.match(/Size(.*)/g);
expect(match?.length).toBe(1);
if (!match) return;
expect(match[0]).toBe('Size 16');
match = str.match(/Index(.*)/g);
if (!match) return;
expect(match[0]).toBe('Index [ 0 13 14 2 ]');
let tail = str.substring(str.length - 64);
tail = tail.substring(tail.indexOf('startxref') + 9).trim();
expect(tail.startsWith('3280')).toBeTruthy();
const noStmB = await pdfDoc.save({ useObjectStreams: false });
const noStrmStr = Buffer.from(noStmB).toString();
let matchNoStm = noStrmStr.match(/Size(.*)/g);
expect(matchNoStm?.length).toBe(1);
if (!matchNoStm) return;
expect(matchNoStm[0]).toBe('Size 13');
matchNoStm = noStrmStr.match(/Index(.*)/g);
expect(matchNoStm).toBeNull();
tail = noStrmStr.substring(str.length - 64);
tail = tail.substring(tail.indexOf('startxref') + 9).trim();
expect(tail.startsWith('3631')).toBeTruthy();
});
});

describe('saveIncremental() method', () => {
Expand Down
Loading