@embedpdf/engines 1.0.24 → 1.0.26

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.
@@ -1,4 +1,4 @@
1
- import { NoopLogger, PdfTaskHelper, PdfErrorCode, pdfDateToDate, ignore, isUuidV4, uuidV4, PdfAnnotationSubtype, Rotation, PdfPageFlattenFlag, stripPdfUnwantedMarkers, PdfAnnotationIcon, PdfAnnotationBorderStyle, PdfAnnotationColorType, PdfAnnotationLineEnding, pdfColorToWebColor, webColorToPdfColor, pdfAlphaToWebOpacity, webOpacityToPdfAlpha, dateToPdfDate, quadToRect, rectToQuad, PdfStandardFont, PdfPageObjectType, flagsToNames, namesToFlags, PDF_FORM_FIELD_TYPE, AppearanceMode, Task, toIntRect, transformRect, buildUserToDeviceMatrix, PdfActionType, PdfZoomMode, MatchFlag } from "@embedpdf/models";
1
+ import { NoopLogger, PdfTaskHelper, PdfErrorCode, pdfDateToDate, ignore, isUuidV4, uuidV4, PdfAnnotationSubtype, PdfPageFlattenFlag, stripPdfUnwantedMarkers, PdfAnnotationIcon, PdfAnnotationBorderStyle, PdfAnnotationColorType, PdfAnnotationLineEnding, PdfTrappedStatus, pdfColorToWebColor, webColorToPdfColor, pdfAlphaToWebOpacity, webOpacityToPdfAlpha, dateToPdfDate, quadToRect, rectToQuad, PdfStandardFont, PdfPageObjectType, flagsToNames, namesToFlags, PDF_FORM_FIELD_TYPE, Rotation, AppearanceMode, Task, toIntRect, transformRect, buildUserToDeviceMatrix, PdfActionType, PdfZoomMode, MatchFlag } from "@embedpdf/models";
2
2
  function readString(wasmModule, readChars, parseChars, defaultLength = 100) {
3
3
  let buffer = wasmModule.wasmExports.malloc(defaultLength);
4
4
  for (let i = 0; i < defaultLength; i++) {
@@ -32,6 +32,27 @@ function readArrayBuffer(wasmModule, readChars) {
32
32
  wasmModule.wasmExports.free(bufferPtr);
33
33
  return arrayBuffer;
34
34
  }
35
+ const RESERVED_INFO_KEYS = /* @__PURE__ */ new Set([
36
+ "Title",
37
+ "Author",
38
+ "Subject",
39
+ "Keywords",
40
+ "Producer",
41
+ "Creator",
42
+ "CreationDate",
43
+ "ModDate",
44
+ "Trapped"
45
+ ]);
46
+ function isValidCustomKey(key) {
47
+ if (!key || key.length > 127) return false;
48
+ if (RESERVED_INFO_KEYS.has(key)) return false;
49
+ if (key[0] === "/") return false;
50
+ for (let i = 0; i < key.length; i++) {
51
+ const c = key.charCodeAt(i);
52
+ if (c < 32 || c > 126) return false;
53
+ }
54
+ return true;
55
+ }
35
56
  class PdfCache {
36
57
  constructor(pdfium) {
37
58
  this.pdfium = pdfium;
@@ -693,7 +714,9 @@ class PdfiumEngine {
693
714
  producer: this.readMetaText(ctx.docPtr, "Producer"),
694
715
  creator: this.readMetaText(ctx.docPtr, "Creator"),
695
716
  creationDate: creationRaw ? pdfDateToDate(creationRaw) ?? null : null,
696
- modificationDate: modRaw ? pdfDateToDate(modRaw) ?? null : null
717
+ modificationDate: modRaw ? pdfDateToDate(modRaw) ?? null : null,
718
+ trapped: this.getMetaTrapped(ctx.docPtr),
719
+ custom: this.readAllMeta(ctx.docPtr, true)
697
720
  };
698
721
  this.logger.perf(LOG_SOURCE, LOG_CATEGORY, `GetMetadata`, "End", doc.id);
699
722
  return PdfTaskHelper.resolve(metadata);
@@ -742,6 +765,18 @@ class PdfiumEngine {
742
765
  };
743
766
  writeDate("creationDate", "CreationDate");
744
767
  writeDate("modificationDate", "ModDate");
768
+ if (meta.trapped !== void 0) {
769
+ if (!this.setMetaTrapped(ctx.docPtr, meta.trapped ?? null)) ok = false;
770
+ }
771
+ if (meta.custom !== void 0) {
772
+ for (const [key, value] of Object.entries(meta.custom)) {
773
+ if (!isValidCustomKey(key)) {
774
+ this.logger.warn(LOG_SOURCE, LOG_CATEGORY, "Invalid custom metadata key skipped", key);
775
+ continue;
776
+ }
777
+ if (!this.setMetaText(ctx.docPtr, key, value ?? null)) ok = false;
778
+ }
779
+ }
745
780
  this.logger.perf(LOG_SOURCE, LOG_CATEGORY, "SetMetadata", "End", doc.id);
746
781
  return ok ? PdfTaskHelper.resolve(true) : PdfTaskHelper.reject({
747
782
  code: PdfErrorCode.Unknown,
@@ -1375,7 +1410,7 @@ class PdfiumEngine {
1375
1410
  * @public
1376
1411
  */
1377
1412
  renderThumbnail(doc, page, options) {
1378
- const { scaleFactor = 1, rotation = Rotation.Degree0, dpr = 1 } = options ?? {};
1413
+ const { scaleFactor = 1, ...rest } = options ?? {};
1379
1414
  this.logger.debug(LOG_SOURCE, LOG_CATEGORY, "renderThumbnail", doc, page, options);
1380
1415
  this.logger.perf(
1381
1416
  LOG_SOURCE,
@@ -1400,9 +1435,7 @@ class PdfiumEngine {
1400
1435
  }
1401
1436
  const result = this.renderPage(doc, page, {
1402
1437
  scaleFactor: Math.max(scaleFactor, 0.5),
1403
- rotation,
1404
- dpr,
1405
- withAnnotations: true
1438
+ ...rest
1406
1439
  });
1407
1440
  this.logger.perf(LOG_SOURCE, LOG_CATEGORY, `RenderThumbnail`, "End", `${doc.id}-${page.index}`);
1408
1441
  return result;
@@ -2589,6 +2622,84 @@ class PdfiumEngine {
2589
2622
  this.memoryManager.free(ptr);
2590
2623
  }
2591
2624
  }
2625
+ /**
2626
+ * Read the document's trapped status via PDFium.
2627
+ * Falls back to `Unknown` on unexpected values.
2628
+ *
2629
+ * @private
2630
+ */
2631
+ getMetaTrapped(docPtr) {
2632
+ const raw = Number(this.pdfiumModule.EPDF_GetMetaTrapped(docPtr));
2633
+ switch (raw) {
2634
+ case PdfTrappedStatus.NotSet:
2635
+ case PdfTrappedStatus.True:
2636
+ case PdfTrappedStatus.False:
2637
+ case PdfTrappedStatus.Unknown:
2638
+ return raw;
2639
+ default:
2640
+ return PdfTrappedStatus.Unknown;
2641
+ }
2642
+ }
2643
+ /**
2644
+ * Write (or clear) the document's trapped status via PDFium.
2645
+ * Pass `null`/`undefined` to remove the `/Trapped` key.
2646
+ *
2647
+ * @private
2648
+ */
2649
+ setMetaTrapped(docPtr, status) {
2650
+ const toSet = status == null || status === void 0 ? PdfTrappedStatus.NotSet : status;
2651
+ const valid = toSet === PdfTrappedStatus.NotSet || toSet === PdfTrappedStatus.True || toSet === PdfTrappedStatus.False || toSet === PdfTrappedStatus.Unknown;
2652
+ if (!valid) return false;
2653
+ return !!this.pdfiumModule.EPDF_SetMetaTrapped(docPtr, toSet);
2654
+ }
2655
+ /**
2656
+ * Get the number of keys in the document's Info dictionary.
2657
+ * @param docPtr - pointer to pdf document
2658
+ * @param customOnly - if true, only count non-reserved (custom) keys; if false, count all keys.
2659
+ * @returns the number of keys (possibly 0). On error, returns 0.
2660
+ *
2661
+ * @private
2662
+ */
2663
+ getMetaKeyCount(docPtr, customOnly) {
2664
+ return Number(this.pdfiumModule.EPDF_GetMetaKeyCount(docPtr, customOnly)) | 0;
2665
+ }
2666
+ /**
2667
+ * Get the name of the Info dictionary key at |index|.
2668
+ * @param docPtr - pointer to pdf document
2669
+ * @param index - 0-based key index in the order returned by PDFium.
2670
+ * @param customOnly - if true, indexes only over non-reserved (custom) keys; if false, indexes over all keys.
2671
+ * @returns the name of the key, or null if the key is not found.
2672
+ *
2673
+ * @private
2674
+ */
2675
+ getMetaKeyName(docPtr, index, customOnly) {
2676
+ const len = this.pdfiumModule.EPDF_GetMetaKeyName(docPtr, index, customOnly, 0, 0);
2677
+ if (!len) return null;
2678
+ return readString(
2679
+ this.pdfiumModule.pdfium,
2680
+ (buffer, buflen) => this.pdfiumModule.EPDF_GetMetaKeyName(docPtr, index, customOnly, buffer, buflen),
2681
+ this.pdfiumModule.pdfium.UTF8ToString,
2682
+ len
2683
+ );
2684
+ }
2685
+ /**
2686
+ * Read all metadata from the document's Info dictionary.
2687
+ * @param docPtr - pointer to pdf document
2688
+ * @param customOnly - if true, only read non-reserved (custom) keys; if false, read all keys.
2689
+ * @returns all metadata
2690
+ *
2691
+ * @private
2692
+ */
2693
+ readAllMeta(docPtr, customOnly = true) {
2694
+ const n = this.getMetaKeyCount(docPtr, customOnly);
2695
+ const out = {};
2696
+ for (let i = 0; i < n; i++) {
2697
+ const key = this.getMetaKeyName(docPtr, i, customOnly);
2698
+ if (!key) continue;
2699
+ out[key] = this.readMetaText(docPtr, key);
2700
+ }
2701
+ return out;
2702
+ }
2592
2703
  /**
2593
2704
  * Read bookmarks in the pdf document
2594
2705
  * @param docPtr - pointer to pdf document
@@ -6388,6 +6499,7 @@ export {
6388
6499
  PdfiumEngine as a,
6389
6500
  browserImageDataToBlobConverter as b,
6390
6501
  readArrayBuffer as c,
6502
+ isValidCustomKey as i,
6391
6503
  readString as r
6392
6504
  };
6393
- //# sourceMappingURL=engine-BKTSw4UQ.js.map
6505
+ //# sourceMappingURL=engine-BH98VpS7.js.map