@adnsistemas/pdf-lib 2.7.2 → 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 +8 -3
  13. package/cjs/core/writers/PDFWriter.d.ts.map +1 -1
  14. package/cjs/core/writers/PDFWriter.js +30 -7
  15. package/cjs/core/writers/PDFWriter.js.map +1 -1
  16. package/dist/pdf-lib.esm.js +37 -17
  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 +37 -17
  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 +8 -3
  35. package/es/core/writers/PDFWriter.d.ts.map +1 -1
  36. package/es/core/writers/PDFWriter.js +30 -7
  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 +32 -9
  44. package/ts3.4/cjs/api/snapshot/IncrementalDocumentSnapshot.d.ts +1 -1
  45. package/ts3.4/cjs/core/writers/PDFWriter.d.ts +11 -3
  46. package/ts3.4/es/api/snapshot/IncrementalDocumentSnapshot.d.ts +1 -1
  47. package/ts3.4/es/core/writers/PDFWriter.d.ts +11 -3
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) {
@@ -10337,6 +10333,11 @@
10337
10333
  * be corrected, to be accurate.
10338
10334
  */
10339
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;
10340
10341
  this.shouldWaitForTick = (n) => {
10341
10342
  this.parsedObjects += n;
10342
10343
  return this.parsedObjects % this.objectsPerTick === 0;
@@ -10347,20 +10348,37 @@
10347
10348
  }
10348
10349
  /**
10349
10350
  * For incremental saves, defers the decision to the snapshot.
10350
- * For full saves, checks that the object is not an XRef stream object.
10351
+ * For full saves, checks that the object is not the last XRef stream object.
10351
10352
  * @param {boolean} incremental If making an incremental save, or a full save of the PDF
10352
10353
  * @param {number} objNum Object number
10353
- * @param {PDFObject} object PDFObject used to check if it is an XRef stream, when not 'incremental' saving
10354
+ * @param {[PDFRef, PDFObject][]} objects List of objects that form the PDF
10354
10355
  * @returns {boolean} whether the object should be saved or not
10355
10356
  */
10356
- shouldSave(incremental, objNum, object) {
10357
+ shouldSave(incremental, objNum, objects) {
10357
10358
  let should = true;
10358
10359
  if (incremental) {
10359
10360
  should = this.snapshot.shouldSave(objNum);
10360
10361
  }
10361
10362
  else {
10362
- should = !(object instanceof PDFRawStream &&
10363
- object.dict.lookup(PDFName.of('Type')) === PDFName.of('XRef'));
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;
10364
10382
  }
10365
10383
  if (!should && this._largestSkippedObjectNum < objNum)
10366
10384
  this._largestSkippedObjectNum = objNum;
@@ -10379,7 +10397,7 @@
10379
10397
  buffer[offset++] = CharCodes$1.Newline;
10380
10398
  for (let idx = 0, len = indirectObjects.length; idx < len; idx++) {
10381
10399
  const [ref, object] = indirectObjects[idx];
10382
- if (!this.shouldSave(incremental, ref.objectNumber, object)) {
10400
+ if (!this.shouldSave(incremental, ref.objectNumber, indirectObjects)) {
10383
10401
  continue;
10384
10402
  }
10385
10403
  const objectNumber = String(ref.objectNumber);
@@ -10445,6 +10463,7 @@
10445
10463
  computeBufferSize(incremental) {
10446
10464
  return __awaiter(this, void 0, void 0, function* () {
10447
10465
  this._largestSkippedObjectNum = 0;
10466
+ this._lastXRefObjectNumber = 0;
10448
10467
  const header = PDFHeader.forVersion(1, 7);
10449
10468
  let size = this.snapshot.pdfSize;
10450
10469
  if (!incremental) {
@@ -10457,7 +10476,7 @@
10457
10476
  for (let idx = 0, len = indirectObjects.length; idx < len; idx++) {
10458
10477
  const indirectObject = indirectObjects[idx];
10459
10478
  const [ref, object] = indirectObject;
10460
- if (!this.shouldSave(incremental, ref.objectNumber, object))
10479
+ if (!this.shouldSave(incremental, ref.objectNumber, indirectObjects))
10461
10480
  continue;
10462
10481
  if (security)
10463
10482
  this.encrypt(ref, object, security);
@@ -15919,7 +15938,8 @@ end\
15919
15938
  let actualValue = value.substring(1, value.length - 1);
15920
15939
  if (this.cryptoFactory && ref) {
15921
15940
  const transformer = this.cryptoFactory.createCipherTransform(ref.objectNumber, ref.generationNumber);
15922
- actualValue = transformer.decryptString(actualValue);
15941
+ const decrypted = transformer.decryptBytes(PDFString.of(actualValue).asBytes());
15942
+ actualValue = arrayAsString(decrypted);
15923
15943
  }
15924
15944
  // Remove the outer parens so they aren't part of the contents
15925
15945
  return PDFString.of(actualValue);
@@ -37311,7 +37331,7 @@ end\
37311
37331
  return undefined;
37312
37332
  }
37313
37333
  takeSnapshot() {
37314
- const indirectObjects = [];
37334
+ const indirectObjects = new Set();
37315
37335
  const snapshot = new IncrementalDocumentSnapshot(this.context.largestObjectNumber, indirectObjects, this.context.pdfFileDetails.pdfSize, this.context.pdfFileDetails.prevStartXRef, this.context);
37316
37336
  if (!this.context.snapshot && this.context.pdfFileDetails.originalBytes) {
37317
37337
  this.context.snapshot = snapshot;