@embedpdf/engines 1.3.0 → 1.3.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 (36) hide show
  1. package/dist/{engine-BSP2yN_k.js → engine-BVe88qdX.js} +203 -26
  2. package/dist/engine-BVe88qdX.js.map +1 -0
  3. package/dist/engine-Cg0lRKs3.cjs +2 -0
  4. package/dist/engine-Cg0lRKs3.cjs.map +1 -0
  5. package/dist/{index-CEXo42Dc.js → index-B4mOT1hM.js} +9 -3
  6. package/dist/index-B4mOT1hM.js.map +1 -0
  7. package/dist/index-dNJf04Wj.cjs +2 -0
  8. package/dist/{index-idmT6csD.cjs.map → index-dNJf04Wj.cjs.map} +1 -1
  9. package/dist/index.cjs +1 -1
  10. package/dist/index.cjs.map +1 -1
  11. package/dist/index.js +8 -2
  12. package/dist/index.js.map +1 -1
  13. package/dist/lib/pdfium/engine.d.ts +54 -1
  14. package/dist/lib/pdfium/index.cjs +1 -1
  15. package/dist/lib/pdfium/index.js +2 -2
  16. package/dist/lib/pdfium/web/direct-engine.cjs +1 -1
  17. package/dist/lib/pdfium/web/direct-engine.js +1 -1
  18. package/dist/lib/pdfium/web/worker-engine.cjs +1 -1
  19. package/dist/lib/pdfium/web/worker-engine.js +1 -1
  20. package/dist/lib/webworker/engine.cjs +1 -1
  21. package/dist/lib/webworker/engine.cjs.map +1 -1
  22. package/dist/lib/webworker/engine.d.ts +13 -1
  23. package/dist/lib/webworker/engine.js +26 -0
  24. package/dist/lib/webworker/engine.js.map +1 -1
  25. package/dist/preact/index.cjs +1 -1
  26. package/dist/preact/index.js +1 -1
  27. package/dist/react/index.cjs +1 -1
  28. package/dist/react/index.js +1 -1
  29. package/dist/vue/index.cjs +1 -1
  30. package/dist/vue/index.js +1 -1
  31. package/package.json +3 -3
  32. package/dist/engine-BSP2yN_k.js.map +0 -1
  33. package/dist/engine-D6NzurHS.cjs +0 -2
  34. package/dist/engine-D6NzurHS.cjs.map +0 -1
  35. package/dist/index-CEXo42Dc.js.map +0 -1
  36. package/dist/index-idmT6csD.cjs +0 -2
@@ -1682,6 +1682,111 @@ class PdfiumEngine {
1682
1682
  this.logger.perf(LOG_SOURCE, LOG_CATEGORY, `GetAttachments`, "End", doc.id);
1683
1683
  return PdfTaskHelper.resolve(attachments);
1684
1684
  }
1685
+ /**
1686
+ * {@inheritDoc @embedpdf/models!PdfEngine.addAttachment}
1687
+ *
1688
+ * @public
1689
+ */
1690
+ addAttachment(doc, params) {
1691
+ this.logger.debug(LOG_SOURCE, LOG_CATEGORY, "addAttachment", doc, params == null ? void 0 : params.name);
1692
+ this.logger.perf(LOG_SOURCE, LOG_CATEGORY, `AddAttachment`, "Begin", doc.id);
1693
+ const ctx = this.cache.getContext(doc.id);
1694
+ if (!ctx) {
1695
+ this.logger.perf(LOG_SOURCE, LOG_CATEGORY, `AddAttachment`, "End", doc.id);
1696
+ return PdfTaskHelper.reject({
1697
+ code: PdfErrorCode.DocNotOpen,
1698
+ message: "document does not open"
1699
+ });
1700
+ }
1701
+ const { name, description, mimeType, data } = params ?? {};
1702
+ if (!name) {
1703
+ this.logger.perf(LOG_SOURCE, LOG_CATEGORY, `AddAttachment`, "End", doc.id);
1704
+ return PdfTaskHelper.reject({
1705
+ code: PdfErrorCode.NotFound,
1706
+ message: "attachment name is required"
1707
+ });
1708
+ }
1709
+ if (!data || (data instanceof Uint8Array ? data.byteLength === 0 : data.byteLength === 0)) {
1710
+ this.logger.perf(LOG_SOURCE, LOG_CATEGORY, `AddAttachment`, "End", doc.id);
1711
+ return PdfTaskHelper.reject({
1712
+ code: PdfErrorCode.NotFound,
1713
+ message: "attachment data is empty"
1714
+ });
1715
+ }
1716
+ const attachmentPtr = this.withWString(
1717
+ name,
1718
+ (wNamePtr) => this.pdfiumModule.FPDFDoc_AddAttachment(ctx.docPtr, wNamePtr)
1719
+ );
1720
+ if (!attachmentPtr) {
1721
+ this.logger.perf(LOG_SOURCE, LOG_CATEGORY, `AddAttachment`, "End", doc.id);
1722
+ return PdfTaskHelper.reject({
1723
+ code: PdfErrorCode.Unknown,
1724
+ message: `An attachment named "${name}" already exists`
1725
+ });
1726
+ }
1727
+ this.withWString(
1728
+ description,
1729
+ (wDescriptionPtr) => this.pdfiumModule.EPDFAttachment_SetDescription(attachmentPtr, wDescriptionPtr)
1730
+ );
1731
+ this.pdfiumModule.EPDFAttachment_SetSubtype(attachmentPtr, mimeType);
1732
+ const u8 = data instanceof Uint8Array ? data : new Uint8Array(data);
1733
+ const len = u8.byteLength;
1734
+ const contentPtr = this.memoryManager.malloc(len);
1735
+ try {
1736
+ this.pdfiumModule.pdfium.HEAPU8.set(u8, contentPtr);
1737
+ const ok = this.pdfiumModule.FPDFAttachment_SetFile(
1738
+ attachmentPtr,
1739
+ ctx.docPtr,
1740
+ contentPtr,
1741
+ len
1742
+ );
1743
+ if (!ok) {
1744
+ this.logger.perf(LOG_SOURCE, LOG_CATEGORY, `AddAttachment`, "End", doc.id);
1745
+ return PdfTaskHelper.reject({
1746
+ code: PdfErrorCode.Unknown,
1747
+ message: "failed to write attachment bytes"
1748
+ });
1749
+ }
1750
+ } finally {
1751
+ this.memoryManager.free(contentPtr);
1752
+ }
1753
+ this.logger.perf(LOG_SOURCE, LOG_CATEGORY, `AddAttachment`, "End", doc.id);
1754
+ return PdfTaskHelper.resolve(true);
1755
+ }
1756
+ /**
1757
+ * {@inheritDoc @embedpdf/models!PdfEngine.removeAttachment}
1758
+ *
1759
+ * @public
1760
+ */
1761
+ removeAttachment(doc, attachment) {
1762
+ this.logger.debug(LOG_SOURCE, LOG_CATEGORY, "deleteAttachment", doc, attachment);
1763
+ this.logger.perf(LOG_SOURCE, LOG_CATEGORY, `DeleteAttachment`, "Begin", doc.id);
1764
+ const ctx = this.cache.getContext(doc.id);
1765
+ if (!ctx) {
1766
+ this.logger.perf(LOG_SOURCE, LOG_CATEGORY, `DeleteAttachment`, "End", doc.id);
1767
+ return PdfTaskHelper.reject({
1768
+ code: PdfErrorCode.DocNotOpen,
1769
+ message: "document does not open"
1770
+ });
1771
+ }
1772
+ const count = this.pdfiumModule.FPDFDoc_GetAttachmentCount(ctx.docPtr);
1773
+ if (attachment.index < 0 || attachment.index >= count) {
1774
+ this.logger.perf(LOG_SOURCE, LOG_CATEGORY, `DeleteAttachment`, "End", doc.id);
1775
+ return PdfTaskHelper.reject({
1776
+ code: PdfErrorCode.Unknown,
1777
+ message: `attachment index ${attachment.index} out of range`
1778
+ });
1779
+ }
1780
+ const ok = this.pdfiumModule.FPDFDoc_DeleteAttachment(ctx.docPtr, attachment.index);
1781
+ this.logger.perf(LOG_SOURCE, LOG_CATEGORY, `DeleteAttachment`, "End", doc.id);
1782
+ if (!ok) {
1783
+ return PdfTaskHelper.reject({
1784
+ code: PdfErrorCode.Unknown,
1785
+ message: "failed to delete attachment"
1786
+ });
1787
+ }
1788
+ return PdfTaskHelper.resolve(true);
1789
+ }
1685
1790
  /**
1686
1791
  * {@inheritDoc @embedpdf/models!PdfEngine.readAttachmentContent}
1687
1792
  *
@@ -3847,6 +3952,29 @@ class PdfiumEngine {
3847
3952
  const raw = dateToPdfDate(date);
3848
3953
  return this.setAnnotString(annotationPtr, key, raw);
3849
3954
  }
3955
+ /**
3956
+ * Get the date of the attachment
3957
+ *
3958
+ * @param attachmentPtr - pointer to an `FPDF_ATTACHMENT`
3959
+ * @param key - 'ModDate' for modified date, 'CreationDate' for creation date
3960
+ * @returns `Date` or `undefined` when PDFium can't read the date
3961
+ */
3962
+ getAttachmentDate(attachmentPtr, key) {
3963
+ const raw = this.getAttachmentString(attachmentPtr, key);
3964
+ return raw ? pdfDateToDate(raw) : void 0;
3965
+ }
3966
+ /**
3967
+ * Set the date of the attachment
3968
+ *
3969
+ * @param attachmentPtr - pointer to an `FPDF_ATTACHMENT`
3970
+ * @param key - 'ModDate' for modified date, 'CreationDate' for creation date
3971
+ * @param date - `Date` to set
3972
+ * @returns `true` on success
3973
+ */
3974
+ setAttachmentDate(attachmentPtr, key, date) {
3975
+ const raw = dateToPdfDate(date);
3976
+ return this.setAttachmentString(attachmentPtr, key, raw);
3977
+ }
3850
3978
  /**
3851
3979
  * Dash-pattern helper ( /BS → /D array, dashed borders only )
3852
3980
  *
@@ -5238,6 +5366,45 @@ class PdfiumEngine {
5238
5366
  this.memoryManager.free(ptr);
5239
5367
  return value || void 0;
5240
5368
  }
5369
+ /**
5370
+ * Get a string value (`/T`, `/M`, `/State`, …) from an attachment.
5371
+ *
5372
+ * @returns decoded UTF-8 string or `undefined` when the key is absent
5373
+ *
5374
+ * @private
5375
+ */
5376
+ getAttachmentString(attachmentPtr, key) {
5377
+ const len = this.pdfiumModule.FPDFAttachment_GetStringValue(attachmentPtr, key, 0, 0);
5378
+ if (len === 0) return;
5379
+ const bytes = (len + 1) * 2;
5380
+ const ptr = this.memoryManager.malloc(bytes);
5381
+ this.pdfiumModule.FPDFAttachment_GetStringValue(attachmentPtr, key, ptr, bytes);
5382
+ const value = this.pdfiumModule.pdfium.UTF16ToString(ptr);
5383
+ this.memoryManager.free(ptr);
5384
+ return value || void 0;
5385
+ }
5386
+ /**
5387
+ * Get a number value (`/Size`) from an attachment.
5388
+ *
5389
+ * @returns number or `null` when the key is absent
5390
+ *
5391
+ * @private
5392
+ */
5393
+ getAttachmentNumber(attachmentPtr, key) {
5394
+ const outPtr = this.memoryManager.malloc(4);
5395
+ try {
5396
+ const ok = this.pdfiumModule.EPDFAttachment_GetIntegerValue(
5397
+ attachmentPtr,
5398
+ key,
5399
+ // FPDF_BYTESTRING → ASCII JS string is fine in your glue
5400
+ outPtr
5401
+ );
5402
+ if (!ok) return void 0;
5403
+ return this.pdfiumModule.pdfium.getValue(outPtr, "i32") >>> 0;
5404
+ } finally {
5405
+ this.memoryManager.free(outPtr);
5406
+ }
5407
+ }
5241
5408
  /**
5242
5409
  * Get custom data of the annotation
5243
5410
  * @param annotationPtr - pointer to pdf annotation
@@ -5329,12 +5496,9 @@ class PdfiumEngine {
5329
5496
  * @private
5330
5497
  */
5331
5498
  getAnnotationByName(pagePtr, name) {
5332
- const bytes = 2 * (name.length + 1);
5333
- const ptr = this.memoryManager.malloc(bytes);
5334
- this.pdfiumModule.pdfium.stringToUTF16(name, ptr, bytes);
5335
- const ok = this.pdfiumModule.EPDFPage_GetAnnotByName(pagePtr, ptr);
5336
- this.memoryManager.free(ptr);
5337
- return ok;
5499
+ return this.withWString(name, (wNamePtr) => {
5500
+ return this.pdfiumModule.EPDFPage_GetAnnotByName(pagePtr, wNamePtr);
5501
+ });
5338
5502
  }
5339
5503
  /**
5340
5504
  * Remove annotation by name
@@ -5345,12 +5509,9 @@ class PdfiumEngine {
5345
5509
  * @private
5346
5510
  */
5347
5511
  removeAnnotationByName(pagePtr, name) {
5348
- const bytes = 2 * (name.length + 1);
5349
- const ptr = this.memoryManager.malloc(bytes);
5350
- this.pdfiumModule.pdfium.stringToUTF16(name, ptr, bytes);
5351
- const ok = this.pdfiumModule.EPDFPage_RemoveAnnotByName(pagePtr, ptr);
5352
- this.memoryManager.free(ptr);
5353
- return ok;
5512
+ return this.withWString(name, (wNamePtr) => {
5513
+ return this.pdfiumModule.EPDFPage_RemoveAnnotByName(pagePtr, wNamePtr);
5514
+ });
5354
5515
  }
5355
5516
  /**
5356
5517
  * Set a string value (`/T`, `/M`, `/State`, …) to an annotation.
@@ -5360,12 +5521,21 @@ class PdfiumEngine {
5360
5521
  * @private
5361
5522
  */
5362
5523
  setAnnotString(annotationPtr, key, value) {
5363
- const bytes = 2 * (value.length + 1);
5364
- const ptr = this.memoryManager.malloc(bytes);
5365
- this.pdfiumModule.pdfium.stringToUTF16(value, ptr, bytes);
5366
- const ok = this.pdfiumModule.FPDFAnnot_SetStringValue(annotationPtr, key, ptr);
5367
- this.memoryManager.free(ptr);
5368
- return ok;
5524
+ return this.withWString(value, (wValPtr) => {
5525
+ return this.pdfiumModule.FPDFAnnot_SetStringValue(annotationPtr, key, wValPtr);
5526
+ });
5527
+ }
5528
+ /**
5529
+ * Set a string value (`/T`, `/M`, `/State`, …) to an attachment.
5530
+ *
5531
+ * @returns `true` if the operation was successful
5532
+ *
5533
+ * @private
5534
+ */
5535
+ setAttachmentString(attachmentPtr, key, value) {
5536
+ return this.withWString(value, (wValPtr) => {
5537
+ return this.pdfiumModule.FPDFAttachment_SetStringValue(attachmentPtr, key, wValPtr);
5538
+ });
5369
5539
  }
5370
5540
  /**
5371
5541
  * Read vertices of pdf annotation
@@ -6144,18 +6314,21 @@ class PdfiumEngine {
6144
6314
  },
6145
6315
  this.pdfiumModule.pdfium.UTF16ToString
6146
6316
  );
6147
- const creationDate = readString(
6317
+ const description = readString(
6148
6318
  this.pdfiumModule.pdfium,
6149
6319
  (buffer, bufferLength) => {
6150
- return this.pdfiumModule.FPDFAttachment_GetStringValue(
6151
- attachmentPtr,
6152
- "CreationDate",
6153
- buffer,
6154
- bufferLength
6155
- );
6320
+ return this.pdfiumModule.EPDFAttachment_GetDescription(attachmentPtr, buffer, bufferLength);
6321
+ },
6322
+ this.pdfiumModule.pdfium.UTF16ToString
6323
+ );
6324
+ const mimeType = readString(
6325
+ this.pdfiumModule.pdfium,
6326
+ (buffer, bufferLength) => {
6327
+ return this.pdfiumModule.FPDFAttachment_GetSubtype(attachmentPtr, buffer, bufferLength);
6156
6328
  },
6157
6329
  this.pdfiumModule.pdfium.UTF16ToString
6158
6330
  );
6331
+ const creationDate = this.getAttachmentDate(attachmentPtr, "CreationDate");
6159
6332
  const checksum = readString(
6160
6333
  this.pdfiumModule.pdfium,
6161
6334
  (buffer, bufferLength) => {
@@ -6168,9 +6341,13 @@ class PdfiumEngine {
6168
6341
  },
6169
6342
  this.pdfiumModule.pdfium.UTF16ToString
6170
6343
  );
6344
+ const size = this.getAttachmentNumber(attachmentPtr, "Size");
6171
6345
  return {
6172
6346
  index,
6173
6347
  name,
6348
+ description,
6349
+ mimeType,
6350
+ size,
6174
6351
  creationDate,
6175
6352
  checksum
6176
6353
  };
@@ -6849,4 +7026,4 @@ export {
6849
7026
  isValidCustomKey as i,
6850
7027
  readString as r
6851
7028
  };
6852
- //# sourceMappingURL=engine-BSP2yN_k.js.map
7029
+ //# sourceMappingURL=engine-BVe88qdX.js.map