@adnsistemas/pdf-lib 2.7.1 → 2.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/pdf-lib.js CHANGED
@@ -10331,6 +10331,12 @@
10331
10331
  class PDFWriter {
10332
10332
  constructor(context, objectsPerTick, snapshot) {
10333
10333
  this.parsedObjects = 0;
10334
+ /**
10335
+ * If PDF has an XRef Stream, then the last object will be probably be skipped on saving.
10336
+ * If that's the case, this property will have that object number, and the PDF /Size can
10337
+ * be corrected, to be accurate.
10338
+ */
10339
+ this._largestSkippedObjectNum = 0;
10334
10340
  this.shouldWaitForTick = (n) => {
10335
10341
  this.parsedObjects += n;
10336
10342
  return this.parsedObjects % this.objectsPerTick === 0;
@@ -10339,6 +10345,27 @@
10339
10345
  this.objectsPerTick = objectsPerTick;
10340
10346
  this.snapshot = snapshot;
10341
10347
  }
10348
+ /**
10349
+ * For incremental saves, defers the decision to the snapshot.
10350
+ * For full saves, checks that the object is not an XRef stream object.
10351
+ * @param {boolean} incremental If making an incremental save, or a full save of the PDF
10352
+ * @param {number} objNum Object number
10353
+ * @param {PDFObject} object PDFObject used to check if it is an XRef stream, when not 'incremental' saving
10354
+ * @returns {boolean} whether the object should be saved or not
10355
+ */
10356
+ shouldSave(incremental, objNum, object) {
10357
+ let should = true;
10358
+ if (incremental) {
10359
+ should = this.snapshot.shouldSave(objNum);
10360
+ }
10361
+ else {
10362
+ should = !(object instanceof PDFRawStream &&
10363
+ object.dict.lookup(PDFName.of('Type')) === PDFName.of('XRef'));
10364
+ }
10365
+ if (!should && this._largestSkippedObjectNum < objNum)
10366
+ this._largestSkippedObjectNum = objNum;
10367
+ return should;
10368
+ }
10342
10369
  serializeToBuffer() {
10343
10370
  return __awaiter(this, void 0, void 0, function* () {
10344
10371
  const incremental = !(this.snapshot instanceof DefaultDocumentSnapshot);
@@ -10352,7 +10379,7 @@
10352
10379
  buffer[offset++] = CharCodes$1.Newline;
10353
10380
  for (let idx = 0, len = indirectObjects.length; idx < len; idx++) {
10354
10381
  const [ref, object] = indirectObjects[idx];
10355
- if (!this.snapshot.shouldSave(ref.objectNumber)) {
10382
+ if (!this.shouldSave(incremental, ref.objectNumber, object)) {
10356
10383
  continue;
10357
10384
  }
10358
10385
  const objectNumber = String(ref.objectNumber);
@@ -10398,8 +10425,16 @@
10398
10425
  return refSize + objectSize;
10399
10426
  }
10400
10427
  createTrailerDict(prevStartXRef) {
10428
+ /**
10429
+ * if last object (XRef Stream) is not in the output, then size is one less.
10430
+ * An XRef Stream object should always be the largest object number in PDF
10431
+ */
10432
+ const size = this.context.largestObjectNumber +
10433
+ (this._largestSkippedObjectNum === this.context.largestObjectNumber
10434
+ ? 0
10435
+ : 1);
10401
10436
  return this.context.obj({
10402
- Size: this.context.largestObjectNumber + 1,
10437
+ Size: size,
10403
10438
  Root: this.context.trailerInfo.Root,
10404
10439
  Encrypt: this.context.trailerInfo.Encrypt,
10405
10440
  Info: this.context.trailerInfo.Info,
@@ -10409,6 +10444,7 @@
10409
10444
  }
10410
10445
  computeBufferSize(incremental) {
10411
10446
  return __awaiter(this, void 0, void 0, function* () {
10447
+ this._largestSkippedObjectNum = 0;
10412
10448
  const header = PDFHeader.forVersion(1, 7);
10413
10449
  let size = this.snapshot.pdfSize;
10414
10450
  if (!incremental) {
@@ -10421,7 +10457,7 @@
10421
10457
  for (let idx = 0, len = indirectObjects.length; idx < len; idx++) {
10422
10458
  const indirectObject = indirectObjects[idx];
10423
10459
  const [ref, object] = indirectObject;
10424
- if (!this.snapshot.shouldSave(ref.objectNumber))
10460
+ if (!this.shouldSave(incremental, ref.objectNumber, object))
10425
10461
  continue;
10426
10462
  if (security)
10427
10463
  this.encrypt(ref, object, security);