@adnsistemas/pdf-lib 2.7.3 → 2.8.1

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 (70) hide show
  1. package/README.md +1 -0
  2. package/cjs/api/PDFDocument.d.ts +16 -2
  3. package/cjs/api/PDFDocument.d.ts.map +1 -1
  4. package/cjs/api/PDFDocument.js +58 -4
  5. package/cjs/api/PDFDocument.js.map +1 -1
  6. package/cjs/api/PDFEmbeddedFile.d.ts +6 -0
  7. package/cjs/api/PDFEmbeddedFile.d.ts.map +1 -1
  8. package/cjs/api/PDFEmbeddedFile.js +10 -0
  9. package/cjs/api/PDFEmbeddedFile.js.map +1 -1
  10. package/cjs/api/PDFPage.js +1 -1
  11. package/cjs/api/operations.js +2 -2
  12. package/cjs/api/operations.js.map +1 -1
  13. package/cjs/core/embedders/JpegEmbedder.d.ts.map +1 -1
  14. package/cjs/core/embedders/JpegEmbedder.js +1 -1
  15. package/cjs/core/embedders/JpegEmbedder.js.map +1 -1
  16. package/cjs/core/structures/PDFCatalog.d.ts +3 -0
  17. package/cjs/core/structures/PDFCatalog.d.ts.map +1 -1
  18. package/cjs/core/structures/PDFCatalog.js +7 -0
  19. package/cjs/core/structures/PDFCatalog.js.map +1 -1
  20. package/cjs/core/writers/PDFStreamWriter.js +1 -1
  21. package/cjs/core/writers/PDFStreamWriter.js.map +1 -1
  22. package/cjs/core/writers/PDFWriter.d.ts.map +1 -1
  23. package/cjs/core/writers/PDFWriter.js +3 -2
  24. package/cjs/core/writers/PDFWriter.js.map +1 -1
  25. package/dist/pdf-lib.esm.js +82 -11
  26. package/dist/pdf-lib.esm.js.map +1 -1
  27. package/dist/pdf-lib.esm.min.js +2 -2
  28. package/dist/pdf-lib.esm.min.js.map +1 -1
  29. package/dist/pdf-lib.js +82 -11
  30. package/dist/pdf-lib.js.map +1 -1
  31. package/dist/pdf-lib.min.js +2 -2
  32. package/dist/pdf-lib.min.js.map +1 -1
  33. package/es/api/PDFDocument.d.ts +16 -2
  34. package/es/api/PDFDocument.d.ts.map +1 -1
  35. package/es/api/PDFDocument.js +58 -4
  36. package/es/api/PDFDocument.js.map +1 -1
  37. package/es/api/PDFEmbeddedFile.d.ts +6 -0
  38. package/es/api/PDFEmbeddedFile.d.ts.map +1 -1
  39. package/es/api/PDFEmbeddedFile.js +10 -0
  40. package/es/api/PDFEmbeddedFile.js.map +1 -1
  41. package/es/api/PDFPage.js +1 -1
  42. package/es/api/operations.js +2 -2
  43. package/es/api/operations.js.map +1 -1
  44. package/es/core/embedders/JpegEmbedder.d.ts.map +1 -1
  45. package/es/core/embedders/JpegEmbedder.js +1 -1
  46. package/es/core/embedders/JpegEmbedder.js.map +1 -1
  47. package/es/core/structures/PDFCatalog.d.ts +3 -0
  48. package/es/core/structures/PDFCatalog.d.ts.map +1 -1
  49. package/es/core/structures/PDFCatalog.js +7 -0
  50. package/es/core/structures/PDFCatalog.js.map +1 -1
  51. package/es/core/writers/PDFStreamWriter.js +1 -1
  52. package/es/core/writers/PDFStreamWriter.js.map +1 -1
  53. package/es/core/writers/PDFWriter.d.ts.map +1 -1
  54. package/es/core/writers/PDFWriter.js +3 -2
  55. package/es/core/writers/PDFWriter.js.map +1 -1
  56. package/package.json +4 -2
  57. package/src/api/PDFDocument.ts +74 -9
  58. package/src/api/PDFEmbeddedFile.ts +12 -0
  59. package/src/api/PDFPage.ts +1 -1
  60. package/src/api/operations.ts +2 -2
  61. package/src/core/embedders/JpegEmbedder.ts +5 -1
  62. package/src/core/structures/PDFCatalog.ts +9 -0
  63. package/src/core/writers/PDFStreamWriter.ts +1 -1
  64. package/src/core/writers/PDFWriter.ts +3 -2
  65. package/ts3.4/cjs/api/PDFDocument.d.ts +16 -2
  66. package/ts3.4/cjs/api/PDFEmbeddedFile.d.ts +6 -0
  67. package/ts3.4/cjs/core/structures/PDFCatalog.d.ts +3 -0
  68. package/ts3.4/es/api/PDFDocument.d.ts +16 -2
  69. package/ts3.4/es/api/PDFEmbeddedFile.d.ts +6 -0
  70. package/ts3.4/es/core/structures/PDFCatalog.d.ts +3 -0
package/dist/pdf-lib.js CHANGED
@@ -10363,8 +10363,9 @@
10363
10363
  // only the last XRef Stream will be regenerated on save
10364
10364
  if (!this._lastXRefObjectNumber) {
10365
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
10366
+ // 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
10367
+ this._lastXRefObjectNumber = this.context.largestObjectNumber + 10;
10368
+ const checkWatermark = this._lastXRefObjectNumber - 20; // max number of objects in the final part of the PDF to check
10368
10369
  // search the last XRef Stream, if there is one, objects are expected to be in object number order
10369
10370
  for (let idx = objects.length - 1; idx > 0; idx--) {
10370
10371
  // if not in last 'rangeToCheck' objects, there is none that should be skipped, most probably a linearized PDF, or without XRef Streams
@@ -12451,6 +12452,12 @@
12451
12452
  AcroForm() {
12452
12453
  return this.lookupMaybe(PDFName.of('AcroForm'), PDFDict);
12453
12454
  }
12455
+ Names() {
12456
+ return this.lookupMaybe(PDFName.of('Names'), PDFDict);
12457
+ }
12458
+ AttachedFiles() {
12459
+ return this.lookupMaybe(PDFName.of('AF'), PDFArray);
12460
+ }
12454
12461
  getAcroForm() {
12455
12462
  const dict = this.AcroForm();
12456
12463
  if (!dict)
@@ -12693,7 +12700,7 @@
12693
12700
  for (let idx = 0, len = indirectObjects.length; idx < len; idx++) {
12694
12701
  const indirectObject = indirectObjects[idx];
12695
12702
  const [ref, object] = indirectObject;
12696
- if (!this.snapshot.shouldSave(ref.objectNumber)) {
12703
+ if (!this.shouldSave(incremental, ref.objectNumber, indirectObjects)) {
12697
12704
  continue;
12698
12705
  }
12699
12706
  const shouldNotCompress = ref === this.context.trailerInfo.Encrypt ||
@@ -13281,7 +13288,7 @@ end\
13281
13288
  class JpegEmbedder {
13282
13289
  static for(imageData) {
13283
13290
  return __awaiter(this, void 0, void 0, function* () {
13284
- const dataView = new DataView(imageData.buffer);
13291
+ const dataView = new DataView(imageData.buffer, imageData.byteOffset, imageData.byteLength);
13285
13292
  const soi = dataView.getUint16(0);
13286
13293
  if (soi !== 0xffd8)
13287
13294
  throw new Error('SOI not found in JPEG');
@@ -29523,13 +29530,13 @@ end\
29523
29530
  ];
29524
29531
  };
29525
29532
  const getDrawingOperator = ({ color, borderWidth, borderColor, fillRule, }) => {
29526
- if (color && borderColor && borderWidth) {
29533
+ if (color && borderColor && borderWidth !== 0) {
29527
29534
  return fillAndStroke;
29528
29535
  }
29529
29536
  else if (color) {
29530
29537
  return fillRule === exports.FillRule.EvenOdd ? fillEvenOdd : fill;
29531
29538
  }
29532
- else if (borderColor && borderWidth) {
29539
+ else if (borderColor && borderWidth !== 0) {
29533
29540
  return stroke;
29534
29541
  }
29535
29542
  return undefined;
@@ -34234,6 +34241,16 @@ end\
34234
34241
  getEmbedder() {
34235
34242
  return this.embedder;
34236
34243
  }
34244
+ /**
34245
+ * Returns whether or not this file has already been embedded.
34246
+ * @returns true if the file has already been embedded, false otherwise.
34247
+ */
34248
+ getAlreadyEmbedded() {
34249
+ return this.alreadyEmbedded;
34250
+ }
34251
+ getRef() {
34252
+ return this.ref;
34253
+ }
34237
34254
  }
34238
34255
  /**
34239
34256
  * > **NOTE:** You probably don't want to call this method directly. Instead,
@@ -36796,13 +36813,17 @@ end\
36796
36813
  for (let idx = 0, len = EFNames.size(); idx < len; idx += 2) {
36797
36814
  const fileName = EFNames.lookup(idx);
36798
36815
  const fileSpec = EFNames.lookup(idx + 1, PDFDict);
36799
- rawAttachments.push({ fileName, fileSpec });
36816
+ rawAttachments.push({
36817
+ fileName,
36818
+ fileSpec,
36819
+ specRef: EFNames.get(idx + 1),
36820
+ });
36800
36821
  }
36801
36822
  return rawAttachments;
36802
36823
  }
36803
36824
  getSavedAttachments() {
36804
36825
  const rawAttachments = this.getRawAttachments();
36805
- return rawAttachments.flatMap(({ fileName, fileSpec }) => {
36826
+ return rawAttachments.flatMap(({ fileName, fileSpec, specRef }) => {
36806
36827
  const efDict = fileSpec.lookup(PDFName.of('EF'));
36807
36828
  if (!(efDict instanceof PDFDict))
36808
36829
  return [];
@@ -36835,7 +36856,11 @@ end\
36835
36856
  modificationDate = modDateRaw.decodeDate();
36836
36857
  }
36837
36858
  }
36838
- const description = fileSpec.lookup(PDFName.of('Desc')).decodeText();
36859
+ const descRaw = fileSpec.lookup(PDFName.of('Desc'));
36860
+ let description;
36861
+ if (descRaw instanceof PDFHexString) {
36862
+ description = descRaw.decodeText();
36863
+ }
36839
36864
  return [
36840
36865
  {
36841
36866
  name: fileName.decodeText(),
@@ -36845,12 +36870,16 @@ end\
36845
36870
  description,
36846
36871
  creationDate,
36847
36872
  modificationDate,
36873
+ embeddedFileDict: efDict,
36874
+ specRef,
36848
36875
  },
36849
36876
  ];
36850
36877
  });
36851
36878
  }
36852
36879
  getUnsavedAttachments() {
36853
- const attachments = this.embeddedFiles.map((file) => {
36880
+ const attachments = this.embeddedFiles.flatMap((file) => {
36881
+ if (file.getAlreadyEmbedded())
36882
+ return [];
36854
36883
  const embedder = file.getEmbedder();
36855
36884
  return {
36856
36885
  name: embedder.fileName,
@@ -36860,6 +36889,7 @@ end\
36860
36889
  afRelationship: embedder.options.afRelationship,
36861
36890
  creationDate: embedder.options.creationDate,
36862
36891
  modificationDate: embedder.options.modificationDate,
36892
+ pdfEmbeddedFile: file,
36863
36893
  };
36864
36894
  });
36865
36895
  return attachments;
@@ -36874,6 +36904,47 @@ end\
36874
36904
  const unsavedAttachments = this.getUnsavedAttachments();
36875
36905
  return [...savedAttachments, ...unsavedAttachments];
36876
36906
  }
36907
+ /**
36908
+ * Removes an attachment from PDF, based on name.
36909
+ * @param {string} name Name of the attachmet to remove.
36910
+ */
36911
+ detach(name) {
36912
+ const attachedFiles = this.getAttachments();
36913
+ attachedFiles.forEach((file) => {
36914
+ var _a, _b, _c;
36915
+ if (file.name !== name)
36916
+ return;
36917
+ // the file wasn't embedded into context yet
36918
+ if ('pdfEmbeddedFile' in file) {
36919
+ const i = this.embeddedFiles.findIndex((f) => file.pdfEmbeddedFile === f);
36920
+ if (i !== undefined)
36921
+ this.embeddedFiles.splice(i, 1);
36922
+ }
36923
+ else {
36924
+ // remove references from catalog
36925
+ const namesArr = (_a = this.catalog
36926
+ .Names()) === null || _a === void 0 ? void 0 : _a.lookup(PDFName.of('EmbeddedFiles'), PDFDict).lookup(PDFName.of('Names'), PDFArray);
36927
+ const iNames = namesArr === null || namesArr === void 0 ? void 0 : namesArr.indexOf(file.specRef);
36928
+ if (iNames !== undefined && iNames > 0) {
36929
+ // attachment spec ref
36930
+ namesArr === null || namesArr === void 0 ? void 0 : namesArr.remove(iNames);
36931
+ // attachment name
36932
+ namesArr === null || namesArr === void 0 ? void 0 : namesArr.remove(iNames - 1);
36933
+ }
36934
+ // AF-Tag for PDF-A3 compliance
36935
+ const AF = this.catalog.AttachedFiles();
36936
+ const afIndex = AF === null || AF === void 0 ? void 0 : AF.indexOf(file.specRef);
36937
+ if (afIndex !== undefined)
36938
+ AF === null || AF === void 0 ? void 0 : AF.remove(afIndex);
36939
+ // remove references from context
36940
+ const streamRef = (_c = (_b = this.context
36941
+ .lookupMaybe(file.specRef, PDFDict)) === null || _b === void 0 ? void 0 : _b.lookupMaybe(PDFName.of('EF'), PDFDict)) === null || _c === void 0 ? void 0 : _c.get(PDFName.of('F'));
36942
+ if (streamRef)
36943
+ this.context.delete(streamRef);
36944
+ this.context.delete(file.specRef);
36945
+ }
36946
+ });
36947
+ }
36877
36948
  /**
36878
36949
  * Embed a font into this document. The input data can be provided in multiple
36879
36950
  * formats:
@@ -38565,7 +38636,7 @@ end\
38565
38636
  rotate: (_c = options.rotate) !== null && _c !== void 0 ? _c : degrees(0),
38566
38637
  color: (_d = options.color) !== null && _d !== void 0 ? _d : undefined,
38567
38638
  borderColor: (_e = options.borderColor) !== null && _e !== void 0 ? _e : undefined,
38568
- borderWidth: (_f = options.borderWidth) !== null && _f !== void 0 ? _f : 0,
38639
+ borderWidth: (_f = options.borderWidth) !== null && _f !== void 0 ? _f : 1,
38569
38640
  borderDashArray: (_g = options.borderDashArray) !== null && _g !== void 0 ? _g : undefined,
38570
38641
  borderDashPhase: (_h = options.borderDashPhase) !== null && _h !== void 0 ? _h : undefined,
38571
38642
  borderLineCap: (_j = options.borderLineCap) !== null && _j !== void 0 ? _j : undefined,