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