@adnsistemas/pdf-lib 2.4.5 → 2.5.0

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 (88) hide show
  1. package/cjs/api/PDFDocument.d.ts.map +1 -1
  2. package/cjs/api/PDFDocument.js +2 -0
  3. package/cjs/api/PDFDocument.js.map +1 -1
  4. package/cjs/api/snapshot/DefaultDocumentSnapshot.d.ts +4 -0
  5. package/cjs/api/snapshot/DefaultDocumentSnapshot.d.ts.map +1 -1
  6. package/cjs/api/snapshot/DefaultDocumentSnapshot.js +10 -0
  7. package/cjs/api/snapshot/DefaultDocumentSnapshot.js.map +1 -1
  8. package/cjs/api/snapshot/DocumentSnapshot.d.ts +4 -0
  9. package/cjs/api/snapshot/DocumentSnapshot.d.ts.map +1 -1
  10. package/cjs/api/snapshot/IncrementalDocumentSnapshot.d.ts +5 -0
  11. package/cjs/api/snapshot/IncrementalDocumentSnapshot.d.ts.map +1 -1
  12. package/cjs/api/snapshot/IncrementalDocumentSnapshot.js +17 -0
  13. package/cjs/api/snapshot/IncrementalDocumentSnapshot.js.map +1 -1
  14. package/cjs/core/PDFContext.d.ts +1 -0
  15. package/cjs/core/PDFContext.d.ts.map +1 -1
  16. package/cjs/core/PDFContext.js +2 -0
  17. package/cjs/core/PDFContext.js.map +1 -1
  18. package/cjs/core/document/PDFCrossRefSection.d.ts +1 -1
  19. package/cjs/core/document/PDFCrossRefSection.d.ts.map +1 -1
  20. package/cjs/core/document/PDFCrossRefSection.js +49 -2
  21. package/cjs/core/document/PDFCrossRefSection.js.map +1 -1
  22. package/cjs/core/parser/PDFParser.d.ts.map +1 -1
  23. package/cjs/core/parser/PDFParser.js +6 -0
  24. package/cjs/core/parser/PDFParser.js.map +1 -1
  25. package/cjs/core/parser/PDFXRefStreamParser.d.ts.map +1 -1
  26. package/cjs/core/parser/PDFXRefStreamParser.js +6 -0
  27. package/cjs/core/parser/PDFXRefStreamParser.js.map +1 -1
  28. package/cjs/core/writers/PDFWriter.d.ts.map +1 -1
  29. package/cjs/core/writers/PDFWriter.js +10 -0
  30. package/cjs/core/writers/PDFWriter.js.map +1 -1
  31. package/dist/pdf-lib.esm.js +100 -2
  32. package/dist/pdf-lib.esm.js.map +1 -1
  33. package/dist/pdf-lib.esm.min.js +3 -3
  34. package/dist/pdf-lib.esm.min.js.map +1 -1
  35. package/dist/pdf-lib.js +100 -2
  36. package/dist/pdf-lib.js.map +1 -1
  37. package/dist/pdf-lib.min.js +3 -3
  38. package/dist/pdf-lib.min.js.map +1 -1
  39. package/es/api/PDFDocument.d.ts.map +1 -1
  40. package/es/api/PDFDocument.js +2 -0
  41. package/es/api/PDFDocument.js.map +1 -1
  42. package/es/api/snapshot/DefaultDocumentSnapshot.d.ts +4 -0
  43. package/es/api/snapshot/DefaultDocumentSnapshot.d.ts.map +1 -1
  44. package/es/api/snapshot/DefaultDocumentSnapshot.js +10 -0
  45. package/es/api/snapshot/DefaultDocumentSnapshot.js.map +1 -1
  46. package/es/api/snapshot/DocumentSnapshot.d.ts +4 -0
  47. package/es/api/snapshot/DocumentSnapshot.d.ts.map +1 -1
  48. package/es/api/snapshot/IncrementalDocumentSnapshot.d.ts +5 -0
  49. package/es/api/snapshot/IncrementalDocumentSnapshot.d.ts.map +1 -1
  50. package/es/api/snapshot/IncrementalDocumentSnapshot.js +17 -0
  51. package/es/api/snapshot/IncrementalDocumentSnapshot.js.map +1 -1
  52. package/es/core/PDFContext.d.ts +1 -0
  53. package/es/core/PDFContext.d.ts.map +1 -1
  54. package/es/core/PDFContext.js +2 -0
  55. package/es/core/PDFContext.js.map +1 -1
  56. package/es/core/document/PDFCrossRefSection.d.ts +1 -1
  57. package/es/core/document/PDFCrossRefSection.d.ts.map +1 -1
  58. package/es/core/document/PDFCrossRefSection.js +49 -2
  59. package/es/core/document/PDFCrossRefSection.js.map +1 -1
  60. package/es/core/parser/PDFParser.d.ts.map +1 -1
  61. package/es/core/parser/PDFParser.js +6 -0
  62. package/es/core/parser/PDFParser.js.map +1 -1
  63. package/es/core/parser/PDFXRefStreamParser.d.ts.map +1 -1
  64. package/es/core/parser/PDFXRefStreamParser.js +6 -0
  65. package/es/core/parser/PDFXRefStreamParser.js.map +1 -1
  66. package/es/core/writers/PDFWriter.d.ts.map +1 -1
  67. package/es/core/writers/PDFWriter.js +10 -0
  68. package/es/core/writers/PDFWriter.js.map +1 -1
  69. package/package.json +2 -2
  70. package/src/api/PDFDocument.ts +2 -0
  71. package/src/api/snapshot/DefaultDocumentSnapshot.ts +13 -0
  72. package/src/api/snapshot/DocumentSnapshot.ts +6 -0
  73. package/src/api/snapshot/IncrementalDocumentSnapshot.ts +20 -0
  74. package/src/core/PDFContext.ts +2 -0
  75. package/src/core/document/PDFCrossRefSection.ts +46 -2
  76. package/src/core/parser/PDFParser.ts +7 -0
  77. package/src/core/parser/PDFXRefStreamParser.ts +8 -0
  78. package/src/core/writers/PDFWriter.ts +11 -0
  79. package/ts3.4/cjs/api/snapshot/DefaultDocumentSnapshot.d.ts +4 -0
  80. package/ts3.4/cjs/api/snapshot/DocumentSnapshot.d.ts +4 -0
  81. package/ts3.4/cjs/api/snapshot/IncrementalDocumentSnapshot.d.ts +5 -0
  82. package/ts3.4/cjs/core/PDFContext.d.ts +1 -0
  83. package/ts3.4/cjs/core/document/PDFCrossRefSection.d.ts +1 -1
  84. package/ts3.4/es/api/snapshot/DefaultDocumentSnapshot.d.ts +4 -0
  85. package/ts3.4/es/api/snapshot/DocumentSnapshot.d.ts +4 -0
  86. package/ts3.4/es/api/snapshot/IncrementalDocumentSnapshot.d.ts +5 -0
  87. package/ts3.4/es/core/PDFContext.d.ts +1 -0
  88. package/ts3.4/es/core/document/PDFCrossRefSection.d.ts +1 -1
package/dist/pdf-lib.js CHANGED
@@ -9313,6 +9313,8 @@
9313
9313
  return ref;
9314
9314
  }
9315
9315
  delete(ref) {
9316
+ if (this.snapshot)
9317
+ this.snapshot.markDeletedRef(ref);
9316
9318
  return this.indirectObjects.delete(ref);
9317
9319
  }
9318
9320
  lookupMaybe(ref, ...types) {
@@ -9846,6 +9848,7 @@
9846
9848
  constructor() {
9847
9849
  this.pdfSize = 0;
9848
9850
  this.prevStartXRef = 0;
9851
+ this.deletedCount = 0;
9849
9852
  }
9850
9853
  shouldSave(_objectNumber) {
9851
9854
  return true;
@@ -9862,11 +9865,22 @@
9862
9865
  markObjsForSave(_objs) {
9863
9866
  throw new Error('This method should not be called.');
9864
9867
  }
9868
+ markDeletedObj(_obj) {
9869
+ throw new Error('This method should not be called.');
9870
+ }
9871
+ markDeletedRef(_ref) {
9872
+ throw new Error('This method should not be called.');
9873
+ }
9874
+ deletedRef(_index) {
9875
+ throw new Error('This method should not be called.');
9876
+ }
9865
9877
  }
9866
9878
  const defaultDocumentSnapshot = new DefaultDocumentSnapshot();
9867
9879
 
9868
9880
  class IncrementalDocumentSnapshot {
9869
9881
  constructor(lastObjectNumber, indirectObjects, pdfSize, prevStartXRef, context) {
9882
+ this.deletedCount = 0;
9883
+ this.deleted = [];
9870
9884
  this.lastObjectNumber = lastObjectNumber;
9871
9885
  this.changedObjects = indirectObjects;
9872
9886
  this.pdfSize = pdfSize;
@@ -9899,11 +9913,26 @@
9899
9913
  .map((obj) => this.context.getRef(obj))
9900
9914
  .filter((ref) => ref !== undefined));
9901
9915
  }
9916
+ markDeletedRef(ref) {
9917
+ if (this.deleted.findIndex((dref) => dref.objectNumber === ref.objectNumber) <
9918
+ 0)
9919
+ this.deletedCount = this.deleted.push(ref);
9920
+ }
9921
+ markDeletedObj(obj) {
9922
+ const oref = this.context.getRef(obj);
9923
+ if (oref)
9924
+ this.markDeletedRef(oref);
9925
+ }
9926
+ deletedRef(index) {
9927
+ if (index < 0 || index >= this.deleted.length)
9928
+ return null;
9929
+ return this.deleted[index];
9930
+ }
9902
9931
  }
9903
9932
 
9904
9933
  /**
9905
9934
  * Entries should be added using the [[addEntry]] and [[addDeletedEntry]]
9906
- * methods **in order of ascending object number**.
9935
+ * methods.
9907
9936
  */
9908
9937
  class PDFCrossRefSection {
9909
9938
  constructor(firstEntry) {
@@ -9915,6 +9944,23 @@
9915
9944
  this.append({ ref, offset, deleted: false });
9916
9945
  }
9917
9946
  addDeletedEntry(ref, nextFreeObjectNumber) {
9947
+ // fix the first entry if required
9948
+ if (!this.subsections.length) {
9949
+ this.subsections = [
9950
+ [
9951
+ {
9952
+ ref: PDFRef.of(0, 65535),
9953
+ offset: ref.objectNumber,
9954
+ deleted: true,
9955
+ },
9956
+ ],
9957
+ ];
9958
+ this.chunkIdx = 0;
9959
+ this.chunkLength = 1;
9960
+ }
9961
+ else if (!this.subsections[0][0].offset) {
9962
+ this.subsections[0][0].offset = ref.objectNumber;
9963
+ }
9918
9964
  this.append({ ref, offset: nextFreeObjectNumber, deleted: true });
9919
9965
  }
9920
9966
  toString() {
@@ -9997,7 +10043,37 @@
9997
10043
  }
9998
10044
  const chunk = this.subsections[this.chunkIdx];
9999
10045
  const prevEntry = chunk[this.chunkLength - 1];
10000
- if (currEntry.ref.objectNumber - prevEntry.ref.objectNumber > 1) {
10046
+ if (currEntry.ref.objectNumber - prevEntry.ref.objectNumber !== 1) {
10047
+ // the current chunk is not the right chunk, find the right one, or create a new one
10048
+ for (let c = 0; c < this.subsections.length; c++) {
10049
+ const first = this.subsections[c][0];
10050
+ const last = this.subsections[c][this.subsections[c].length - 1];
10051
+ if (first.ref.objectNumber > currEntry.ref.objectNumber) {
10052
+ // goes before this subsection, or at the start of it
10053
+ if (first.ref.objectNumber - currEntry.ref.objectNumber === 1) {
10054
+ // first element of subsection
10055
+ this.subsections[c].unshift(currEntry);
10056
+ if (c === this.chunkIdx)
10057
+ this.chunkLength += 1;
10058
+ return;
10059
+ }
10060
+ else {
10061
+ // create subsection
10062
+ this.subsections.splice(c, 0, [currEntry]);
10063
+ this.chunkIdx++;
10064
+ return;
10065
+ }
10066
+ }
10067
+ else if (last.ref.objectNumber > currEntry.ref.objectNumber) {
10068
+ // goes in this subsection, find its place..
10069
+ const cep = this.subsections[c].findIndex((ee) => ee.ref.objectNumber > currEntry.ref.objectNumber);
10070
+ this.subsections[c].splice(cep, 0, currEntry);
10071
+ if (c === this.chunkIdx)
10072
+ this.chunkLength += 1;
10073
+ }
10074
+ // bigger, keep looking
10075
+ }
10076
+ // if got to here, then a new subsection is required
10001
10077
  this.subsections.push([currEntry]);
10002
10078
  this.chunkIdx += 1;
10003
10079
  this.chunkLength = 1;
@@ -10238,6 +10314,15 @@
10238
10314
  if (this.shouldWaitForTick(1))
10239
10315
  yield waitForTick();
10240
10316
  }
10317
+ // deleted objects
10318
+ for (let idx = 0; idx < this.snapshot.deletedCount; idx++) {
10319
+ const dref = this.snapshot.deletedRef(idx);
10320
+ if (!dref)
10321
+ break;
10322
+ const nextdref = this.snapshot.deletedRef(idx + 1);
10323
+ // add 1 to generation number for deleted ref
10324
+ xref.addDeletedEntry(PDFRef.of(dref.objectNumber, dref.generationNumber + 1), nextdref ? nextdref.objectNumber : 0);
10325
+ }
10241
10326
  const xrefOffset = size;
10242
10327
  size += xref.sizeInBytes() + 1; // '\n'
10243
10328
  const trailerDict = PDFTrailerDict.of(this.createTrailerDict(this.snapshot.prevStartXRef));
@@ -15770,11 +15855,17 @@ end\
15770
15855
  }
15771
15856
  this.alreadyParsed = true;
15772
15857
  this.context.trailerInfo = {
15858
+ Size: this.dict.lookup(PDFName.of('Size'), PDFNumber),
15773
15859
  Root: this.dict.get(PDFName.of('Root')),
15774
15860
  Encrypt: this.dict.get(PDFName.of('Encrypt')),
15775
15861
  Info: this.dict.get(PDFName.of('Info')),
15776
15862
  ID: this.dict.get(PDFName.of('ID')),
15777
15863
  };
15864
+ // if open for incremental update, make sure next object number doesn't overlap a deleted one
15865
+ if (this.context.trailerInfo.Size &&
15866
+ this.context.pdfFileDetails.originalBytes)
15867
+ this.context.largestObjectNumber =
15868
+ this.context.trailerInfo.Size.asNumber() - 1;
15778
15869
  const entries = this.parseEntries();
15779
15870
  // for (let idx = 0, len = entries.length; idx < len; idx++) {
15780
15871
  // const entry = entries[idx];
@@ -16027,11 +16118,16 @@ end\
16027
16118
  const dict = this.parseDict();
16028
16119
  const { context } = this;
16029
16120
  context.trailerInfo = {
16121
+ Size: dict.lookupMaybe(PDFName.of('Size'), PDFNumber) ||
16122
+ context.trailerInfo.Size,
16030
16123
  Root: dict.get(PDFName.of('Root')) || context.trailerInfo.Root,
16031
16124
  Encrypt: dict.get(PDFName.of('Encrypt')) || context.trailerInfo.Encrypt,
16032
16125
  Info: dict.get(PDFName.of('Info')) || context.trailerInfo.Info,
16033
16126
  ID: dict.get(PDFName.of('ID')) || context.trailerInfo.ID,
16034
16127
  };
16128
+ // if open for incremental update, then deleted objects need to be preserved, and largestObjectNumber has to be Size-1
16129
+ if (context.trailerInfo.Size && context.pdfFileDetails.originalBytes)
16130
+ context.largestObjectNumber = context.trailerInfo.Size.asNumber() - 1;
16035
16131
  }
16036
16132
  maybeParseTrailer() {
16037
16133
  this.skipWhitespaceAndComments();
@@ -36114,8 +36210,10 @@ end\
36114
36210
  if (this.pageCount === 0)
36115
36211
  throw new RemovePageFromEmptyDocumentError();
36116
36212
  assertRange(index, 'index', 0, pageCount - 1);
36213
+ const page = this.getPage(index);
36117
36214
  this.catalog.removeLeafNode(index);
36118
36215
  this.pageCount = pageCount - 1;
36216
+ this.context.delete(page.ref);
36119
36217
  }
36120
36218
  /**
36121
36219
  * Add a page to the end of this document. This method accepts three