@embedpdf/engines 1.2.0 → 1.3.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 (37) hide show
  1. package/dist/{engine-4h8XG4y4.js → engine-BSP2yN_k.js} +197 -2
  2. package/dist/engine-BSP2yN_k.js.map +1 -0
  3. package/dist/engine-D6NzurHS.cjs +2 -0
  4. package/dist/engine-D6NzurHS.cjs.map +1 -0
  5. package/dist/{index-DbHWMufd.js → index-CEXo42Dc.js} +9 -3
  6. package/dist/index-CEXo42Dc.js.map +1 -0
  7. package/dist/index-idmT6csD.cjs +2 -0
  8. package/dist/index-idmT6csD.cjs.map +1 -0
  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 +18 -0
  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 +4 -4
  32. package/dist/engine-4h8XG4y4.js.map +0 -1
  33. package/dist/engine-CJwS0wio.cjs +0 -2
  34. package/dist/engine-CJwS0wio.cjs.map +0 -1
  35. package/dist/index--54o4qnU.cjs +0 -2
  36. package/dist/index--54o4qnU.cjs.map +0 -1
  37. package/dist/index-DbHWMufd.js.map +0 -1
@@ -1,4 +1,4 @@
1
- import { NoopLogger, PdfTaskHelper, PdfErrorCode, pdfDateToDate, ignore, isUuidV4, uuidV4, PdfAnnotationSubtype, PdfPageFlattenFlag, stripPdfUnwantedMarkers, PdfAnnotationIcon, PdfAnnotationBorderStyle, PdfAnnotationColorType, PdfAnnotationLineEnding, PdfStampFit, 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";
1
+ import { NoopLogger, PdfTaskHelper, PdfErrorCode, pdfDateToDate, ignore, isUuidV4, uuidV4, PdfAnnotationSubtype, PdfPageFlattenFlag, stripPdfUnwantedMarkers, PdfAnnotationIcon, PdfAnnotationBorderStyle, PdfAnnotationColorType, PdfAnnotationLineEnding, PdfStampFit, PdfTrappedStatus, pdfColorToWebColor, webColorToPdfColor, pdfAlphaToWebOpacity, webOpacityToPdfAlpha, dateToPdfDate, quadToRect, rectToQuad, PdfStandardFont, PdfPageObjectType, flagsToNames, namesToFlags, PDF_FORM_FIELD_TYPE, Rotation, AppearanceMode, Task, toIntRect, transformRect, buildUserToDeviceMatrix, PdfZoomMode, PdfActionType, 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++) {
@@ -514,6 +514,33 @@ class PdfiumEngine {
514
514
  this.logger.perf(LOG_SOURCE, LOG_CATEGORY, `Destroy`, "End", "General");
515
515
  return PdfTaskHelper.resolve(true);
516
516
  }
517
+ /** Write a UTF-16LE (WIDESTRING) to wasm, call `fn(ptr)`, then free. */
518
+ withWString(value, fn) {
519
+ const length = (value.length + 1) * 2;
520
+ const ptr = this.memoryManager.malloc(length);
521
+ try {
522
+ this.pdfiumModule.pdfium.stringToUTF16(value, ptr, length);
523
+ return fn(ptr);
524
+ } finally {
525
+ this.memoryManager.free(ptr);
526
+ }
527
+ }
528
+ /** Write a float[] to wasm, call `fn(ptr, count)`, then free. */
529
+ withFloatArray(values, fn) {
530
+ const arr = values ?? [];
531
+ const bytes = arr.length * 4;
532
+ const ptr = bytes ? this.memoryManager.malloc(bytes) : WasmPointer(0);
533
+ try {
534
+ if (bytes) {
535
+ for (let i = 0; i < arr.length; i++) {
536
+ this.pdfiumModule.pdfium.setValue(ptr + i * 4, arr[i], "float");
537
+ }
538
+ }
539
+ return fn(ptr, arr.length);
540
+ } finally {
541
+ if (bytes) this.memoryManager.free(ptr);
542
+ }
543
+ }
517
544
  /**
518
545
  * {@inheritDoc @embedpdf/models!PdfEngine.openDocumentUrl}
519
546
  *
@@ -1014,6 +1041,85 @@ class PdfiumEngine {
1014
1041
  bookmarks
1015
1042
  });
1016
1043
  }
1044
+ /**
1045
+ * {@inheritDoc @embedpdf/models!PdfEngine.setBookmarks}
1046
+ *
1047
+ * @public
1048
+ */
1049
+ setBookmarks(doc, list) {
1050
+ this.logger.debug(LOG_SOURCE, LOG_CATEGORY, "setBookmarks", doc, list);
1051
+ this.logger.perf(LOG_SOURCE, LOG_CATEGORY, `SetBookmarks`, "Begin", doc.id);
1052
+ const ctx = this.cache.getContext(doc.id);
1053
+ if (!ctx) {
1054
+ this.logger.perf(LOG_SOURCE, LOG_CATEGORY, `SetBookmarks`, "End", doc.id);
1055
+ return PdfTaskHelper.reject({
1056
+ code: PdfErrorCode.DocNotOpen,
1057
+ message: "document does not open"
1058
+ });
1059
+ }
1060
+ if (!this.pdfiumModule.EPDFBookmark_Clear(ctx.docPtr)) {
1061
+ this.logger.perf(LOG_SOURCE, LOG_CATEGORY, `SetBookmarks`, "End", doc.id);
1062
+ return PdfTaskHelper.reject({
1063
+ code: PdfErrorCode.Unknown,
1064
+ message: "failed to clear existing bookmarks"
1065
+ });
1066
+ }
1067
+ const build = (parentPtr, items) => {
1068
+ var _a;
1069
+ for (const item of items) {
1070
+ const bmPtr = this.withWString(
1071
+ item.title ?? "",
1072
+ (wptr) => this.pdfiumModule.EPDFBookmark_AppendChild(ctx.docPtr, parentPtr, wptr)
1073
+ );
1074
+ if (!bmPtr) return false;
1075
+ if (item.target) {
1076
+ const ok2 = this.applyBookmarkTarget(ctx.docPtr, bmPtr, item.target);
1077
+ if (!ok2) return false;
1078
+ }
1079
+ if ((_a = item.children) == null ? void 0 : _a.length) {
1080
+ const ok2 = build(bmPtr, item.children);
1081
+ if (!ok2) return false;
1082
+ }
1083
+ }
1084
+ return true;
1085
+ };
1086
+ const ok = build(
1087
+ /*top-level*/
1088
+ 0,
1089
+ list
1090
+ );
1091
+ this.logger.perf(LOG_SOURCE, LOG_CATEGORY, `SetBookmarks`, "End", doc.id);
1092
+ if (!ok) {
1093
+ return PdfTaskHelper.reject({
1094
+ code: PdfErrorCode.Unknown,
1095
+ message: "failed to build bookmark tree"
1096
+ });
1097
+ }
1098
+ return PdfTaskHelper.resolve(true);
1099
+ }
1100
+ /**
1101
+ * {@inheritDoc @embedpdf/models!PdfEngine.deleteBookmarks}
1102
+ *
1103
+ * @public
1104
+ */
1105
+ deleteBookmarks(doc) {
1106
+ this.logger.debug(LOG_SOURCE, LOG_CATEGORY, "deleteBookmarks", doc);
1107
+ this.logger.perf(LOG_SOURCE, LOG_CATEGORY, `DeleteBookmarks`, "Begin", doc.id);
1108
+ const ctx = this.cache.getContext(doc.id);
1109
+ if (!ctx) {
1110
+ this.logger.perf(LOG_SOURCE, LOG_CATEGORY, `DeleteBookmarks`, "End", doc.id);
1111
+ return PdfTaskHelper.reject({
1112
+ code: PdfErrorCode.DocNotOpen,
1113
+ message: "document does not open"
1114
+ });
1115
+ }
1116
+ const ok = this.pdfiumModule.EPDFBookmark_Clear(ctx.docPtr);
1117
+ this.logger.perf(LOG_SOURCE, LOG_CATEGORY, `DeleteBookmarks`, "End", doc.id);
1118
+ return ok ? PdfTaskHelper.resolve(true) : PdfTaskHelper.reject({
1119
+ code: PdfErrorCode.Unknown,
1120
+ message: "failed to clear bookmarks"
1121
+ });
1122
+ }
1017
1123
  /**
1018
1124
  * {@inheritDoc @embedpdf/models!PdfEngine.renderPage}
1019
1125
  *
@@ -5759,6 +5865,95 @@ class PdfiumEngine {
5759
5865
  }
5760
5866
  }
5761
5867
  }
5868
+ createLocalDestPtr(docPtr, dest) {
5869
+ var _a, _b;
5870
+ const pagePtr = this.pdfiumModule.FPDF_LoadPage(docPtr, dest.pageIndex);
5871
+ if (!pagePtr) return 0;
5872
+ try {
5873
+ if (dest.zoom.mode === PdfZoomMode.XYZ) {
5874
+ const { x, y, zoom } = dest.zoom.params;
5875
+ return this.pdfiumModule.EPDFDest_CreateXYZ(
5876
+ pagePtr,
5877
+ /*has_left*/
5878
+ true,
5879
+ x,
5880
+ /*has_top*/
5881
+ true,
5882
+ y,
5883
+ /*has_zoom*/
5884
+ true,
5885
+ zoom
5886
+ );
5887
+ }
5888
+ let viewEnum;
5889
+ let params = [];
5890
+ switch (dest.zoom.mode) {
5891
+ case PdfZoomMode.FitPage:
5892
+ viewEnum = PdfZoomMode.FitPage;
5893
+ break;
5894
+ case PdfZoomMode.FitHorizontal:
5895
+ viewEnum = PdfZoomMode.FitHorizontal;
5896
+ params = [((_a = dest.view) == null ? void 0 : _a[0]) ?? 0];
5897
+ break;
5898
+ case PdfZoomMode.FitVertical:
5899
+ viewEnum = PdfZoomMode.FitVertical;
5900
+ params = [((_b = dest.view) == null ? void 0 : _b[0]) ?? 0];
5901
+ break;
5902
+ case PdfZoomMode.FitRectangle:
5903
+ {
5904
+ const v = dest.view ?? [];
5905
+ params = [v[0] ?? 0, v[1] ?? 0, v[2] ?? 0, v[3] ?? 0];
5906
+ viewEnum = PdfZoomMode.FitRectangle;
5907
+ }
5908
+ break;
5909
+ case PdfZoomMode.Unknown:
5910
+ default:
5911
+ return 0;
5912
+ }
5913
+ return this.withFloatArray(
5914
+ params,
5915
+ (ptr, count) => this.pdfiumModule.EPDFDest_CreateView(pagePtr, viewEnum, ptr, count)
5916
+ );
5917
+ } finally {
5918
+ this.pdfiumModule.FPDF_ClosePage(pagePtr);
5919
+ }
5920
+ }
5921
+ applyBookmarkTarget(docPtr, bmPtr, target) {
5922
+ if (target.type === "destination") {
5923
+ const destPtr = this.createLocalDestPtr(docPtr, target.destination);
5924
+ if (!destPtr) return false;
5925
+ const ok = this.pdfiumModule.EPDFBookmark_SetDest(docPtr, bmPtr, destPtr);
5926
+ return !!ok;
5927
+ }
5928
+ const action = target.action;
5929
+ switch (action.type) {
5930
+ case PdfActionType.Goto: {
5931
+ const destPtr = this.createLocalDestPtr(docPtr, action.destination);
5932
+ if (!destPtr) return false;
5933
+ const actPtr = this.pdfiumModule.EPDFAction_CreateGoTo(docPtr, destPtr);
5934
+ if (!actPtr) return false;
5935
+ return !!this.pdfiumModule.EPDFBookmark_SetAction(docPtr, bmPtr, actPtr);
5936
+ }
5937
+ case PdfActionType.URI: {
5938
+ const actPtr = this.pdfiumModule.EPDFAction_CreateURI(docPtr, action.uri);
5939
+ if (!actPtr) return false;
5940
+ return !!this.pdfiumModule.EPDFBookmark_SetAction(docPtr, bmPtr, actPtr);
5941
+ }
5942
+ case PdfActionType.LaunchAppOrOpenFile: {
5943
+ const actPtr = this.withWString(
5944
+ action.path,
5945
+ (wptr) => this.pdfiumModule.EPDFAction_CreateLaunch(docPtr, wptr)
5946
+ );
5947
+ if (!actPtr) return false;
5948
+ return !!this.pdfiumModule.EPDFBookmark_SetAction(docPtr, bmPtr, actPtr);
5949
+ }
5950
+ case PdfActionType.RemoteGoto:
5951
+ return false;
5952
+ case PdfActionType.Unsupported:
5953
+ default:
5954
+ return false;
5955
+ }
5956
+ }
5762
5957
  /**
5763
5958
  * Read pdf action from pdf document
5764
5959
  * @param docPtr - pointer to pdf document object
@@ -6654,4 +6849,4 @@ export {
6654
6849
  isValidCustomKey as i,
6655
6850
  readString as r
6656
6851
  };
6657
- //# sourceMappingURL=engine-4h8XG4y4.js.map
6852
+ //# sourceMappingURL=engine-BSP2yN_k.js.map