@adnsistemas/pdf-lib 2.7.1 → 2.7.3

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.
Files changed (47) hide show
  1. package/README.md +4 -3
  2. package/cjs/api/PDFDocument.js +1 -1
  3. package/cjs/api/PDFDocument.js.map +1 -1
  4. package/cjs/api/snapshot/IncrementalDocumentSnapshot.d.ts +1 -1
  5. package/cjs/api/snapshot/IncrementalDocumentSnapshot.d.ts.map +1 -1
  6. package/cjs/api/snapshot/IncrementalDocumentSnapshot.js +3 -7
  7. package/cjs/api/snapshot/IncrementalDocumentSnapshot.js.map +1 -1
  8. package/cjs/core/objects/PDFString.js +1 -1
  9. package/cjs/core/parser/PDFObjectParser.d.ts.map +1 -1
  10. package/cjs/core/parser/PDFObjectParser.js +2 -1
  11. package/cjs/core/parser/PDFObjectParser.js.map +1 -1
  12. package/cjs/core/writers/PDFWriter.d.ts +20 -0
  13. package/cjs/core/writers/PDFWriter.d.ts.map +1 -1
  14. package/cjs/core/writers/PDFWriter.js +64 -3
  15. package/cjs/core/writers/PDFWriter.js.map +1 -1
  16. package/dist/pdf-lib.esm.js +69 -13
  17. package/dist/pdf-lib.esm.js.map +1 -1
  18. package/dist/pdf-lib.esm.min.js +3 -3
  19. package/dist/pdf-lib.esm.min.js.map +1 -1
  20. package/dist/pdf-lib.js +69 -13
  21. package/dist/pdf-lib.js.map +1 -1
  22. package/dist/pdf-lib.min.js +3 -3
  23. package/dist/pdf-lib.min.js.map +1 -1
  24. package/es/api/PDFDocument.js +1 -1
  25. package/es/api/PDFDocument.js.map +1 -1
  26. package/es/api/snapshot/IncrementalDocumentSnapshot.d.ts +1 -1
  27. package/es/api/snapshot/IncrementalDocumentSnapshot.d.ts.map +1 -1
  28. package/es/api/snapshot/IncrementalDocumentSnapshot.js +3 -7
  29. package/es/api/snapshot/IncrementalDocumentSnapshot.js.map +1 -1
  30. package/es/core/objects/PDFString.js +1 -1
  31. package/es/core/parser/PDFObjectParser.d.ts.map +1 -1
  32. package/es/core/parser/PDFObjectParser.js +3 -2
  33. package/es/core/parser/PDFObjectParser.js.map +1 -1
  34. package/es/core/writers/PDFWriter.d.ts +20 -0
  35. package/es/core/writers/PDFWriter.d.ts.map +1 -1
  36. package/es/core/writers/PDFWriter.js +64 -3
  37. package/es/core/writers/PDFWriter.js.map +1 -1
  38. package/package.json +1 -1
  39. package/src/api/PDFDocument.ts +1 -1
  40. package/src/api/snapshot/IncrementalDocumentSnapshot.ts +5 -11
  41. package/src/core/objects/PDFString.ts +1 -1
  42. package/src/core/parser/PDFObjectParser.ts +5 -2
  43. package/src/core/writers/PDFWriter.ts +72 -3
  44. package/ts3.4/cjs/api/snapshot/IncrementalDocumentSnapshot.d.ts +1 -1
  45. package/ts3.4/cjs/core/writers/PDFWriter.d.ts +23 -0
  46. package/ts3.4/es/api/snapshot/IncrementalDocumentSnapshot.d.ts +1 -1
  47. package/ts3.4/es/core/writers/PDFWriter.d.ts +23 -0
package/dist/pdf-lib.js CHANGED
@@ -9008,7 +9008,7 @@
9008
9008
  pushByte(CharCodes$1.LeftParen);
9009
9009
  else if (byte === CharCodes$1.RightParen)
9010
9010
  pushByte(CharCodes$1.RightParen);
9011
- else if (byte === CharCodes$1.Backspace)
9011
+ else if (byte === CharCodes$1.BackSlash)
9012
9012
  pushByte(CharCodes$1.BackSlash);
9013
9013
  else if (byte >= CharCodes$1.Zero && byte <= CharCodes$1.Seven) {
9014
9014
  octal += char;
@@ -9988,13 +9988,9 @@
9988
9988
  this.context = context;
9989
9989
  }
9990
9990
  shouldSave(objectNumber) {
9991
- if (objectNumber > this.lastObjectNumber) {
9991
+ if (objectNumber > this.lastObjectNumber)
9992
9992
  return true;
9993
- }
9994
- if (this.changedObjects.includes(objectNumber)) {
9995
- return true;
9996
- }
9997
- return false;
9993
+ return this.changedObjects.has(objectNumber);
9998
9994
  }
9999
9995
  markRefForSave(ref) {
10000
9996
  this.markRefsForSave([ref]);
@@ -10002,7 +9998,7 @@
10002
9998
  markRefsForSave(refs) {
10003
9999
  refs.forEach((ref) => {
10004
10000
  if (ref)
10005
- this.changedObjects.push(ref.objectNumber);
10001
+ this.changedObjects.add(ref.objectNumber);
10006
10002
  });
10007
10003
  }
10008
10004
  markObjForSave(obj) {
@@ -10331,6 +10327,17 @@
10331
10327
  class PDFWriter {
10332
10328
  constructor(context, objectsPerTick, snapshot) {
10333
10329
  this.parsedObjects = 0;
10330
+ /**
10331
+ * If PDF has an XRef Stream, then the last object will be probably be skipped on saving.
10332
+ * If that's the case, this property will have that object number, and the PDF /Size can
10333
+ * be corrected, to be accurate.
10334
+ */
10335
+ this._largestSkippedObjectNum = 0;
10336
+ /**
10337
+ * Used to check wheter an object should be saved or not, preserves the object number of the
10338
+ * last XRef Stream object, if there is one.
10339
+ */
10340
+ this._lastXRefObjectNumber = 0;
10334
10341
  this.shouldWaitForTick = (n) => {
10335
10342
  this.parsedObjects += n;
10336
10343
  return this.parsedObjects % this.objectsPerTick === 0;
@@ -10339,6 +10346,44 @@
10339
10346
  this.objectsPerTick = objectsPerTick;
10340
10347
  this.snapshot = snapshot;
10341
10348
  }
10349
+ /**
10350
+ * For incremental saves, defers the decision to the snapshot.
10351
+ * For full saves, checks that the object is not the last XRef stream object.
10352
+ * @param {boolean} incremental If making an incremental save, or a full save of the PDF
10353
+ * @param {number} objNum Object number
10354
+ * @param {[PDFRef, PDFObject][]} objects List of objects that form the PDF
10355
+ * @returns {boolean} whether the object should be saved or not
10356
+ */
10357
+ shouldSave(incremental, objNum, objects) {
10358
+ let should = true;
10359
+ if (incremental) {
10360
+ should = this.snapshot.shouldSave(objNum);
10361
+ }
10362
+ else {
10363
+ // only the last XRef Stream will be regenerated on save
10364
+ if (!this._lastXRefObjectNumber) {
10365
+ // if no XRef Stream, then nothing should be skipped
10366
+ this._lastXRefObjectNumber = this.context.largestObjectNumber + 1;
10367
+ const checkWatermark = this._lastXRefObjectNumber - 10; // max number of objects in the final part of the PDF to check
10368
+ // search the last XRef Stream, if there is one, objects are expected to be in object number order
10369
+ for (let idx = objects.length - 1; idx > 0; idx--) {
10370
+ // if not in last 'rangeToCheck' objects, there is none that should be skipped, most probably a linearized PDF, or without XRef Streams
10371
+ if (objects[idx][0].objectNumber < checkWatermark)
10372
+ break;
10373
+ const object = objects[idx][1];
10374
+ if (object instanceof PDFRawStream &&
10375
+ object.dict.lookup(PDFName.of('Type')) === PDFName.of('XRef')) {
10376
+ this._lastXRefObjectNumber = objects[idx][0].objectNumber;
10377
+ break;
10378
+ }
10379
+ }
10380
+ }
10381
+ should = objNum !== this._lastXRefObjectNumber;
10382
+ }
10383
+ if (!should && this._largestSkippedObjectNum < objNum)
10384
+ this._largestSkippedObjectNum = objNum;
10385
+ return should;
10386
+ }
10342
10387
  serializeToBuffer() {
10343
10388
  return __awaiter(this, void 0, void 0, function* () {
10344
10389
  const incremental = !(this.snapshot instanceof DefaultDocumentSnapshot);
@@ -10352,7 +10397,7 @@
10352
10397
  buffer[offset++] = CharCodes$1.Newline;
10353
10398
  for (let idx = 0, len = indirectObjects.length; idx < len; idx++) {
10354
10399
  const [ref, object] = indirectObjects[idx];
10355
- if (!this.snapshot.shouldSave(ref.objectNumber)) {
10400
+ if (!this.shouldSave(incremental, ref.objectNumber, indirectObjects)) {
10356
10401
  continue;
10357
10402
  }
10358
10403
  const objectNumber = String(ref.objectNumber);
@@ -10398,8 +10443,16 @@
10398
10443
  return refSize + objectSize;
10399
10444
  }
10400
10445
  createTrailerDict(prevStartXRef) {
10446
+ /**
10447
+ * if last object (XRef Stream) is not in the output, then size is one less.
10448
+ * An XRef Stream object should always be the largest object number in PDF
10449
+ */
10450
+ const size = this.context.largestObjectNumber +
10451
+ (this._largestSkippedObjectNum === this.context.largestObjectNumber
10452
+ ? 0
10453
+ : 1);
10401
10454
  return this.context.obj({
10402
- Size: this.context.largestObjectNumber + 1,
10455
+ Size: size,
10403
10456
  Root: this.context.trailerInfo.Root,
10404
10457
  Encrypt: this.context.trailerInfo.Encrypt,
10405
10458
  Info: this.context.trailerInfo.Info,
@@ -10409,6 +10462,8 @@
10409
10462
  }
10410
10463
  computeBufferSize(incremental) {
10411
10464
  return __awaiter(this, void 0, void 0, function* () {
10465
+ this._largestSkippedObjectNum = 0;
10466
+ this._lastXRefObjectNumber = 0;
10412
10467
  const header = PDFHeader.forVersion(1, 7);
10413
10468
  let size = this.snapshot.pdfSize;
10414
10469
  if (!incremental) {
@@ -10421,7 +10476,7 @@
10421
10476
  for (let idx = 0, len = indirectObjects.length; idx < len; idx++) {
10422
10477
  const indirectObject = indirectObjects[idx];
10423
10478
  const [ref, object] = indirectObject;
10424
- if (!this.snapshot.shouldSave(ref.objectNumber))
10479
+ if (!this.shouldSave(incremental, ref.objectNumber, indirectObjects))
10425
10480
  continue;
10426
10481
  if (security)
10427
10482
  this.encrypt(ref, object, security);
@@ -15883,7 +15938,8 @@ end\
15883
15938
  let actualValue = value.substring(1, value.length - 1);
15884
15939
  if (this.cryptoFactory && ref) {
15885
15940
  const transformer = this.cryptoFactory.createCipherTransform(ref.objectNumber, ref.generationNumber);
15886
- actualValue = transformer.decryptString(actualValue);
15941
+ const decrypted = transformer.decryptBytes(PDFString.of(actualValue).asBytes());
15942
+ actualValue = arrayAsString(decrypted);
15887
15943
  }
15888
15944
  // Remove the outer parens so they aren't part of the contents
15889
15945
  return PDFString.of(actualValue);
@@ -37275,7 +37331,7 @@ end\
37275
37331
  return undefined;
37276
37332
  }
37277
37333
  takeSnapshot() {
37278
- const indirectObjects = [];
37334
+ const indirectObjects = new Set();
37279
37335
  const snapshot = new IncrementalDocumentSnapshot(this.context.largestObjectNumber, indirectObjects, this.context.pdfFileDetails.pdfSize, this.context.pdfFileDetails.prevStartXRef, this.context);
37280
37336
  if (!this.context.snapshot && this.context.pdfFileDetails.originalBytes) {
37281
37337
  this.context.snapshot = snapshot;