@embedpdf/engines 1.0.22 → 1.0.23

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 (34) hide show
  1. package/dist/{engine-D0zKoYug.js → engine-DSdvr8ah.js} +233 -166
  2. package/dist/engine-DSdvr8ah.js.map +1 -0
  3. package/dist/engine-RdqkJxAO.cjs +2 -0
  4. package/dist/engine-RdqkJxAO.cjs.map +1 -0
  5. package/dist/index.cjs +1 -1
  6. package/dist/index.cjs.map +1 -1
  7. package/dist/index.js +3 -2
  8. package/dist/lib/converters/index.cjs +1 -1
  9. package/dist/lib/converters/index.cjs.map +1 -1
  10. package/dist/lib/converters/index.d.ts +4 -14
  11. package/dist/lib/converters/index.js +16 -19
  12. package/dist/lib/converters/index.js.map +1 -1
  13. package/dist/lib/converters/types.d.ts +9 -0
  14. package/dist/lib/pdfium/engine.d.ts +8 -21
  15. package/dist/lib/pdfium/index.cjs +1 -1
  16. package/dist/lib/pdfium/index.js +3 -2
  17. package/dist/lib/pdfium/web/direct-engine.cjs +1 -1
  18. package/dist/lib/pdfium/web/direct-engine.js +1 -1
  19. package/dist/lib/pdfium/web/worker-engine.cjs +1 -1
  20. package/dist/lib/pdfium/web/worker-engine.js +1 -1
  21. package/dist/preact/index.cjs +1 -1
  22. package/dist/preact/index.js +1 -1
  23. package/dist/react/index.cjs +1 -1
  24. package/dist/react/index.js +1 -1
  25. package/dist/{runner-CGyvdr9V.js → runner-CjYO-wR5.js} +2 -2
  26. package/dist/{runner-CGyvdr9V.js.map → runner-CjYO-wR5.js.map} +1 -1
  27. package/dist/{runner-CHOggH0O.cjs → runner-DoKDIKUP.cjs} +2 -2
  28. package/dist/{runner-CHOggH0O.cjs.map → runner-DoKDIKUP.cjs.map} +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-D0zKoYug.js.map +0 -1
  33. package/dist/engine-Dcn6oo4j.cjs +0 -2
  34. package/dist/engine-Dcn6oo4j.cjs.map +0 -1
@@ -1,4 +1,4 @@
1
- import { NoopLogger, PdfTaskHelper, PdfErrorCode, pdfDateToDate, Task, 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, toIntRect, transformRect, makeMatrix, toIntSize, transformSize, PdfActionType, PdfZoomMode, MatchFlag } from "@embedpdf/models";
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";
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++) {
@@ -236,17 +236,25 @@ var PdfiumErrorCode = /* @__PURE__ */ ((PdfiumErrorCode2) => {
236
236
  PdfiumErrorCode2[PdfiumErrorCode2["XFALayout"] = 8] = "XFALayout";
237
237
  return PdfiumErrorCode2;
238
238
  })(PdfiumErrorCode || {});
239
- const browserImageDataToBlobConverter = (pdfImageData, imageType = "image/webp") => {
239
+ class OffscreenCanvasError extends Error {
240
+ constructor(message) {
241
+ super(message);
242
+ this.name = "OffscreenCanvasError";
243
+ }
244
+ }
245
+ const browserImageDataToBlobConverter = (getImageData, imageType = "image/webp", quality) => {
240
246
  if (typeof OffscreenCanvas === "undefined") {
241
- throw new Error(
242
- "OffscreenCanvas is not available in this environment. This converter is intended for browser use only. Please use createNodeImageDataToBlobConverter() or createNodeCanvasImageDataToBlobConverter() for Node.js."
247
+ return Promise.reject(
248
+ new OffscreenCanvasError(
249
+ "OffscreenCanvas is not available in this environment. This converter is intended for browser use only. Falling back to WASM-based image encoding."
250
+ )
243
251
  );
244
252
  }
245
- const rgba = new Uint8ClampedArray(pdfImageData.data);
246
- const imageData = new ImageData(rgba, pdfImageData.width, pdfImageData.height);
253
+ const pdfImage = getImageData();
254
+ const imageData = new ImageData(pdfImage.data, pdfImage.width, pdfImage.height);
247
255
  const off = new OffscreenCanvas(imageData.width, imageData.height);
248
256
  off.getContext("2d").putImageData(imageData, 0, 0);
249
- return off.convertToBlob({ type: imageType });
257
+ return off.convertToBlob({ type: imageType, quality });
250
258
  };
251
259
  class PdfiumEngine {
252
260
  /**
@@ -781,29 +789,11 @@ class PdfiumEngine {
781
789
  * @public
782
790
  */
783
791
  renderPage(doc, page, options) {
784
- const { imageType = "image/webp" } = options ?? {};
785
- const task = new Task();
786
792
  this.logger.debug(LOG_SOURCE, LOG_CATEGORY, "renderPage", doc, page, options);
787
793
  this.logger.perf(LOG_SOURCE, LOG_CATEGORY, `RenderPage`, "Begin", `${doc.id}-${page.index}`);
788
- const ctx = this.cache.getContext(doc.id);
789
- if (!ctx) {
790
- this.logger.perf(LOG_SOURCE, LOG_CATEGORY, `RenderPage`, "End", `${doc.id}-${page.index}`);
791
- return PdfTaskHelper.reject({
792
- code: PdfErrorCode.DocNotOpen,
793
- message: "document does not open"
794
- });
795
- }
796
- const imageData = this.renderPageRectToImageData(
797
- ctx,
798
- page,
799
- {
800
- origin: { x: 0, y: 0 },
801
- size: page.size
802
- },
803
- options
804
- );
794
+ const rect = { origin: { x: 0, y: 0 }, size: page.size };
795
+ const task = this.renderRectEncoded(doc, page, rect, options);
805
796
  this.logger.perf(LOG_SOURCE, LOG_CATEGORY, `RenderPage`, "End", `${doc.id}-${page.index}`);
806
- this.imageDataConverter(imageData, imageType).then((blob) => task.resolve(blob));
807
797
  return task;
808
798
  }
809
799
  /**
@@ -812,8 +802,6 @@ class PdfiumEngine {
812
802
  * @public
813
803
  */
814
804
  renderPageRect(doc, page, rect, options) {
815
- const { imageType = "image/webp" } = options ?? {};
816
- const task = new Task();
817
805
  this.logger.debug(LOG_SOURCE, LOG_CATEGORY, "renderPageRect", doc, page, rect, options);
818
806
  this.logger.perf(
819
807
  LOG_SOURCE,
@@ -822,23 +810,8 @@ class PdfiumEngine {
822
810
  "Begin",
823
811
  `${doc.id}-${page.index}`
824
812
  );
825
- const ctx = this.cache.getContext(doc.id);
826
- if (!ctx) {
827
- this.logger.perf(
828
- LOG_SOURCE,
829
- LOG_CATEGORY,
830
- `RenderPageRect`,
831
- "End",
832
- `${doc.id}-${page.index}`
833
- );
834
- return PdfTaskHelper.reject({
835
- code: PdfErrorCode.DocNotOpen,
836
- message: "document does not open"
837
- });
838
- }
839
- const imageData = this.renderPageRectToImageData(ctx, page, rect, options);
813
+ const task = this.renderRectEncoded(doc, page, rect, options);
840
814
  this.logger.perf(LOG_SOURCE, LOG_CATEGORY, `RenderPageRect`, "End", `${doc.id}-${page.index}`);
841
- this.imageDataConverter(imageData, imageType).then((blob) => task.resolve(blob));
842
815
  return task;
843
816
  }
844
817
  /**
@@ -5172,7 +5145,8 @@ class PdfiumEngine {
5172
5145
  rotation = Rotation.Degree0,
5173
5146
  dpr = 1,
5174
5147
  mode = AppearanceMode.Normal,
5175
- imageType = "image/webp"
5148
+ imageType = "image/webp",
5149
+ imageQuality
5176
5150
  } = options ?? {};
5177
5151
  this.logger.debug(
5178
5152
  LOG_SOURCE,
@@ -5208,7 +5182,6 @@ class PdfiumEngine {
5208
5182
  const pageCtx = ctx.acquirePage(page.index);
5209
5183
  const annotPtr = this.getAnnotationByName(pageCtx.pagePtr, annotation.id);
5210
5184
  if (!annotPtr) {
5211
- pageCtx.release();
5212
5185
  this.logger.perf(
5213
5186
  LOG_SOURCE,
5214
5187
  LOG_CATEGORY,
@@ -5216,53 +5189,53 @@ class PdfiumEngine {
5216
5189
  "End",
5217
5190
  `${doc.id}-${page.index}-${annotation.id}`
5218
5191
  );
5219
- return PdfTaskHelper.reject({
5220
- code: PdfErrorCode.NotFound,
5221
- message: "annotation not found"
5222
- });
5192
+ pageCtx.release();
5193
+ return PdfTaskHelper.reject({ code: PdfErrorCode.NotFound, message: "annotation not found" });
5223
5194
  }
5224
- const finalScale = scaleFactor * dpr;
5225
- const annotRect = annotation.rect;
5226
- const bitmapRect = toIntRect(transformRect(page.size, annotRect, rotation, finalScale));
5227
- const format = 4;
5228
- const bytesPerPixel = 4;
5229
- const bitmapHeapLength = bitmapRect.size.width * bitmapRect.size.height * bytesPerPixel;
5230
- const bitmapHeapPtr = this.malloc(bitmapHeapLength);
5195
+ const finalScale = Math.max(0.01, scaleFactor * dpr);
5196
+ const devRect = toIntRect(transformRect(page.size, annotation.rect, rotation, finalScale));
5197
+ const wDev = Math.max(1, devRect.size.width);
5198
+ const hDev = Math.max(1, devRect.size.height);
5199
+ const stride = wDev * 4;
5200
+ const bytes = stride * hDev;
5201
+ const heapPtr = this.malloc(bytes);
5231
5202
  const bitmapPtr = this.pdfiumModule.FPDFBitmap_CreateEx(
5232
- bitmapRect.size.width,
5233
- bitmapRect.size.height,
5234
- format,
5235
- bitmapHeapPtr,
5236
- bitmapRect.size.width * bytesPerPixel
5203
+ wDev,
5204
+ hDev,
5205
+ 4,
5206
+ heapPtr,
5207
+ stride
5237
5208
  );
5238
- this.pdfiumModule.FPDFBitmap_FillRect(
5239
- bitmapPtr,
5240
- 0,
5241
- 0,
5242
- bitmapRect.size.width,
5243
- bitmapRect.size.height,
5244
- 0
5209
+ this.pdfiumModule.FPDFBitmap_FillRect(bitmapPtr, 0, 0, wDev, hDev, 0);
5210
+ const M = buildUserToDeviceMatrix(
5211
+ annotation.rect,
5212
+ // {origin:{L,B}, size:{W,H}}
5213
+ rotation,
5214
+ wDev,
5215
+ hDev
5245
5216
  );
5246
- const matrix = makeMatrix(annotation.rect, rotation, finalScale);
5247
- const matrixSize = 6 * 4;
5248
- const matrixPtr = this.malloc(matrixSize);
5249
- const matrixView = new Float32Array(this.pdfiumModule.pdfium.HEAPF32.buffer, matrixPtr, 6);
5250
- matrixView.set([matrix.a, matrix.b, matrix.c, matrix.d, matrix.e, matrix.f]);
5217
+ const mPtr = this.malloc(6 * 4);
5218
+ const mView = new Float32Array(this.pdfiumModule.pdfium.HEAPF32.buffer, mPtr, 6);
5219
+ mView.set([M.a, M.b, M.c, M.d, M.e, M.f]);
5251
5220
  const FLAGS = 16;
5252
- const ok = !!this.pdfiumModule.EPDF_RenderAnnotBitmap(
5253
- bitmapPtr,
5254
- pageCtx.pagePtr,
5255
- annotPtr,
5256
- mode,
5257
- matrixPtr,
5258
- FLAGS
5259
- );
5260
- this.free(matrixPtr);
5261
- this.pdfiumModule.FPDFBitmap_Destroy(bitmapPtr);
5262
- this.pdfiumModule.FPDFPage_CloseAnnot(annotPtr);
5263
- pageCtx.release();
5221
+ let ok = false;
5222
+ try {
5223
+ ok = !!this.pdfiumModule.EPDF_RenderAnnotBitmap(
5224
+ bitmapPtr,
5225
+ pageCtx.pagePtr,
5226
+ annotPtr,
5227
+ mode,
5228
+ mPtr,
5229
+ FLAGS
5230
+ );
5231
+ } finally {
5232
+ this.free(mPtr);
5233
+ this.pdfiumModule.FPDFBitmap_Destroy(bitmapPtr);
5234
+ this.pdfiumModule.FPDFPage_CloseAnnot(annotPtr);
5235
+ pageCtx.release();
5236
+ }
5264
5237
  if (!ok) {
5265
- this.free(bitmapHeapPtr);
5238
+ this.free(heapPtr);
5266
5239
  this.logger.perf(
5267
5240
  LOG_SOURCE,
5268
5241
  LOG_CATEGORY,
@@ -5275,89 +5248,182 @@ class PdfiumEngine {
5275
5248
  message: "EPDF_RenderAnnotBitmap failed"
5276
5249
  });
5277
5250
  }
5278
- const data = this.pdfiumModule.pdfium.HEAPU8.subarray(
5279
- bitmapHeapPtr,
5280
- bitmapHeapPtr + bitmapHeapLength
5281
- );
5282
- const imageData = {
5283
- data: new Uint8ClampedArray(data),
5284
- width: bitmapRect.size.width,
5285
- height: bitmapRect.size.height
5286
- };
5287
- this.free(bitmapHeapPtr);
5288
- this.logger.perf(
5289
- LOG_SOURCE,
5290
- LOG_CATEGORY,
5291
- `RenderPageAnnotation`,
5292
- "End",
5293
- `${doc.id}-${page.index}-${annotation.id}`
5294
- );
5295
- this.imageDataConverter(imageData, imageType).then((blob) => task.resolve(blob)).catch((err) => task.reject({ code: PdfErrorCode.Unknown, message: String(err) }));
5251
+ const dispose = () => this.free(heapPtr);
5252
+ this.imageDataConverter(
5253
+ () => {
5254
+ const rgba = new Uint8ClampedArray(
5255
+ this.pdfiumModule.pdfium.HEAPU8.subarray(heapPtr, heapPtr + bytes)
5256
+ );
5257
+ return {
5258
+ width: wDev,
5259
+ height: hDev,
5260
+ data: rgba
5261
+ };
5262
+ },
5263
+ imageType,
5264
+ imageQuality
5265
+ ).then((out) => task.resolve(out)).catch((e) => {
5266
+ if (e instanceof OffscreenCanvasError) {
5267
+ try {
5268
+ const blob = this.encodeViaWasm(
5269
+ { ptr: heapPtr, width: wDev, height: hDev, stride },
5270
+ { type: imageType, quality: imageQuality }
5271
+ );
5272
+ task.resolve(blob);
5273
+ } catch (wasmError) {
5274
+ task.reject({ code: PdfErrorCode.Unknown, message: String(wasmError) });
5275
+ }
5276
+ } else {
5277
+ task.reject({ code: PdfErrorCode.Unknown, message: String(e) });
5278
+ }
5279
+ }).finally(dispose);
5296
5280
  return task;
5297
5281
  }
5298
- /**
5299
- * render rectangle of pdf page to image
5300
- * @param docPtr - pointer to pdf document object
5301
- * @param page - pdf page infor
5302
- * @param rect - rectangle info
5303
- * @param scaleFactor - factor of scalling
5304
- * @param rotation - rotation angle
5305
- * @param options - render options
5306
- * @returns image data
5307
- *
5308
- * @private
5309
- */
5310
- renderPageRectToImageData(ctx, page, rect, options) {
5311
- const { scaleFactor = 1, rotation = Rotation.Degree0, dpr = 1 } = options ?? {};
5312
- const format = 4;
5313
- const bytesPerPixel = 4;
5314
- const rectSize = toIntRect(transformRect(page.size, rect, rotation, scaleFactor * dpr));
5315
- const pageSize = toIntSize(transformSize(page.size, rotation, scaleFactor * dpr));
5316
- const bitmapHeapLength = rectSize.size.width * rectSize.size.height * bytesPerPixel;
5317
- const bitmapHeapPtr = this.malloc(bitmapHeapLength);
5282
+ encodeViaWasm(buf, opts) {
5283
+ const pdf = this.pdfiumModule.pdfium;
5284
+ const blobFrom = (outPtr, size, mime) => {
5285
+ const view = pdf.HEAPU8.subarray(outPtr, outPtr + size);
5286
+ const copy = new Uint8Array(view);
5287
+ this.free(outPtr);
5288
+ return new Blob([copy], { type: mime });
5289
+ };
5290
+ const pngLevel = 6;
5291
+ const outPtrPtr = this.malloc(4);
5292
+ try {
5293
+ switch (opts.type) {
5294
+ /*
5295
+ case 'image/webp': {
5296
+ const size = this.pdfiumModule.EPDF_WebP_EncodeRGBA(
5297
+ buf.ptr,
5298
+ buf.width,
5299
+ buf.height,
5300
+ buf.stride,
5301
+ webpQ,
5302
+ outPtrPtr,
5303
+ );
5304
+ const outPtr = pdf.getValue(outPtrPtr, 'i32');
5305
+ return blobFrom(outPtr, size, 'image/webp');
5306
+ }
5307
+ case 'image/jpeg': {
5308
+ const size = this.pdfiumModule.EPDF_JPEG_EncodeRGBA(
5309
+ buf.ptr,
5310
+ buf.width,
5311
+ buf.height,
5312
+ buf.stride,
5313
+ jpegQ,
5314
+ outPtrPtr,
5315
+ );
5316
+ const outPtr = pdf.getValue(outPtrPtr, 'i32');
5317
+ return blobFrom(outPtr, size, 'image/jpeg');
5318
+ }
5319
+ */
5320
+ case "image/png":
5321
+ default: {
5322
+ const size = this.pdfiumModule.EPDF_PNG_EncodeRGBA(
5323
+ buf.ptr,
5324
+ buf.width,
5325
+ buf.height,
5326
+ buf.stride,
5327
+ pngLevel,
5328
+ outPtrPtr
5329
+ );
5330
+ const outPtr = pdf.getValue(outPtrPtr, "i32");
5331
+ return blobFrom(outPtr, size, "image/png");
5332
+ }
5333
+ }
5334
+ } finally {
5335
+ this.free(outPtrPtr);
5336
+ }
5337
+ }
5338
+ renderRectEncoded(doc, page, rect, options) {
5339
+ const task = new Task();
5340
+ const imageType = (options == null ? void 0 : options.imageType) ?? "image/webp";
5341
+ const quality = options == null ? void 0 : options.imageQuality;
5342
+ const rotation = (options == null ? void 0 : options.rotation) ?? Rotation.Degree0;
5343
+ const ctx = this.cache.getContext(doc.id);
5344
+ if (!ctx) {
5345
+ return PdfTaskHelper.reject({
5346
+ code: PdfErrorCode.DocNotOpen,
5347
+ message: "document does not open"
5348
+ });
5349
+ }
5350
+ const scale = Math.max(0.01, (options == null ? void 0 : options.scaleFactor) ?? 1);
5351
+ const dpr = Math.max(1, (options == null ? void 0 : options.dpr) ?? 1);
5352
+ const finalScale = scale * dpr;
5353
+ const baseW = rect.size.width;
5354
+ const baseH = rect.size.height;
5355
+ const swap = (rotation & 1) === 1;
5356
+ const wDev = Math.max(1, Math.round((swap ? baseH : baseW) * finalScale));
5357
+ const hDev = Math.max(1, Math.round((swap ? baseW : baseH) * finalScale));
5358
+ const stride = wDev * 4;
5359
+ const bytes = stride * hDev;
5360
+ const heapPtr = this.malloc(bytes);
5318
5361
  const bitmapPtr = this.pdfiumModule.FPDFBitmap_CreateEx(
5319
- rectSize.size.width,
5320
- rectSize.size.height,
5321
- format,
5322
- bitmapHeapPtr,
5323
- rectSize.size.width * bytesPerPixel
5362
+ wDev,
5363
+ hDev,
5364
+ 4,
5365
+ heapPtr,
5366
+ stride
5324
5367
  );
5325
- this.pdfiumModule.FPDFBitmap_FillRect(
5326
- bitmapPtr,
5327
- 0,
5328
- 0,
5329
- rectSize.size.width,
5330
- rectSize.size.height,
5331
- 4294967295
5332
- );
5333
- let flags = 16;
5334
- if (options == null ? void 0 : options.withAnnotations) {
5335
- flags = flags | 1;
5336
- }
5368
+ this.pdfiumModule.FPDFBitmap_FillRect(bitmapPtr, 0, 0, wDev, hDev, 4294967295);
5369
+ const M = buildUserToDeviceMatrix(rect, rotation, wDev, hDev);
5370
+ const mPtr = this.malloc(6 * 4);
5371
+ const mView = new Float32Array(this.pdfiumModule.pdfium.HEAPF32.buffer, mPtr, 6);
5372
+ mView.set([M.a, M.b, M.c, M.d, M.e, M.f]);
5373
+ const clipPtr = this.malloc(4 * 4);
5374
+ const clipView = new Float32Array(this.pdfiumModule.pdfium.HEAPF32.buffer, clipPtr, 4);
5375
+ clipView.set([0, 0, wDev, hDev]);
5376
+ let flags = 2 | 16;
5377
+ if ((options == null ? void 0 : options.withAnnotations) ?? false) flags |= 1;
5337
5378
  const pageCtx = ctx.acquirePage(page.index);
5338
- this.pdfiumModule.FPDF_RenderPageBitmap(
5339
- bitmapPtr,
5340
- pageCtx.pagePtr,
5341
- -rectSize.origin.x,
5342
- -rectSize.origin.y,
5343
- pageSize.width,
5344
- pageSize.height,
5345
- rotation,
5346
- flags
5347
- );
5348
- this.pdfiumModule.FPDFBitmap_Destroy(bitmapPtr);
5349
- pageCtx.release();
5350
- const data = this.pdfiumModule.pdfium.HEAPU8.subarray(
5351
- bitmapHeapPtr,
5352
- bitmapHeapPtr + bitmapHeapLength
5353
- );
5354
- const imageData = {
5355
- data: new Uint8ClampedArray(data),
5356
- width: rectSize.size.width,
5357
- height: rectSize.size.height
5379
+ try {
5380
+ this.pdfiumModule.FPDF_RenderPageBitmapWithMatrix(
5381
+ bitmapPtr,
5382
+ pageCtx.pagePtr,
5383
+ mPtr,
5384
+ clipPtr,
5385
+ flags
5386
+ );
5387
+ } finally {
5388
+ pageCtx.release();
5389
+ this.free(mPtr);
5390
+ this.free(clipPtr);
5391
+ }
5392
+ const dispose = () => {
5393
+ this.pdfiumModule.FPDFBitmap_Destroy(bitmapPtr);
5394
+ this.free(heapPtr);
5358
5395
  };
5359
- this.free(bitmapHeapPtr);
5360
- return imageData;
5396
+ this.imageDataConverter(
5397
+ () => {
5398
+ const rgba = new Uint8ClampedArray(
5399
+ this.pdfiumModule.pdfium.HEAPU8.subarray(heapPtr, heapPtr + bytes)
5400
+ );
5401
+ return {
5402
+ width: wDev,
5403
+ height: hDev,
5404
+ data: rgba
5405
+ };
5406
+ },
5407
+ imageType,
5408
+ quality
5409
+ ).then((out) => task.resolve(out)).catch((e) => {
5410
+ this.logger.error(LOG_SOURCE, LOG_CATEGORY, "Error", e);
5411
+ if (e instanceof OffscreenCanvasError) {
5412
+ this.logger.info(LOG_SOURCE, LOG_CATEGORY, "Fallback to WASM encoding");
5413
+ try {
5414
+ const blob = this.encodeViaWasm(
5415
+ { ptr: heapPtr, width: wDev, height: hDev, stride },
5416
+ { type: imageType, quality }
5417
+ );
5418
+ task.resolve(blob);
5419
+ } catch (wasmError) {
5420
+ task.reject({ code: PdfErrorCode.Unknown, message: String(wasmError) });
5421
+ }
5422
+ } else {
5423
+ task.reject({ code: PdfErrorCode.Unknown, message: String(e) });
5424
+ }
5425
+ }).finally(dispose);
5426
+ return task;
5361
5427
  }
5362
5428
  /**
5363
5429
  * Read the target of pdf link annotation
@@ -6253,6 +6319,7 @@ class PdfiumEngine {
6253
6319
  }
6254
6320
  export {
6255
6321
  BitmapFormat as B,
6322
+ OffscreenCanvasError as O,
6256
6323
  PdfiumErrorCode as P,
6257
6324
  RenderFlag as R,
6258
6325
  PdfiumEngine as a,
@@ -6260,4 +6327,4 @@ export {
6260
6327
  readArrayBuffer as c,
6261
6328
  readString as r
6262
6329
  };
6263
- //# sourceMappingURL=engine-D0zKoYug.js.map
6330
+ //# sourceMappingURL=engine-DSdvr8ah.js.map