@embedpdf/engines 1.0.11 → 1.0.13
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.
- package/dist/engine-B0ZTENqz.js +5441 -0
- package/dist/engine-B0ZTENqz.js.map +1 -0
- package/dist/engine-BB5n-Wej.cjs +2 -0
- package/dist/engine-BB5n-Wej.cjs.map +1 -0
- package/dist/index.cjs +1 -5846
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -1613
- package/dist/index.js +339 -5824
- package/dist/index.js.map +1 -1
- package/dist/lib/converters/index.cjs +2 -0
- package/dist/lib/converters/index.cjs.map +1 -0
- package/dist/{converters.d.ts → lib/converters/index.d.ts} +7 -10
- package/dist/lib/converters/index.js +80 -0
- package/dist/{converters.cjs.map → lib/converters/index.js.map} +1 -1
- package/dist/lib/index.d.ts +7 -0
- package/dist/lib/mock/index.d.ts +25 -0
- package/dist/lib/pdfium/cache.d.ts +62 -0
- package/dist/{pdfium.d.ts → lib/pdfium/engine.d.ts} +124 -178
- package/dist/lib/pdfium/helper.d.ts +21 -0
- package/dist/lib/pdfium/index.cjs +2 -0
- package/dist/lib/pdfium/index.cjs.map +1 -0
- package/dist/lib/pdfium/index.d.ts +3 -0
- package/dist/lib/pdfium/index.js +13 -0
- package/dist/lib/pdfium/index.js.map +1 -0
- package/dist/lib/pdfium/runner.d.ts +16 -0
- package/dist/lib/pdfium/web/direct-engine.cjs +2 -0
- package/dist/lib/pdfium/web/direct-engine.cjs.map +1 -0
- package/dist/lib/pdfium/web/direct-engine.d.ts +3 -0
- package/dist/lib/pdfium/web/direct-engine.js +12 -0
- package/dist/lib/pdfium/web/direct-engine.js.map +1 -0
- package/dist/lib/pdfium/web/worker-engine.cjs +2 -0
- package/dist/lib/pdfium/web/worker-engine.cjs.map +1 -0
- package/dist/lib/pdfium/web/worker-engine.d.ts +7 -0
- package/dist/lib/pdfium/web/worker-engine.js +15 -0
- package/dist/lib/pdfium/web/worker-engine.js.map +1 -0
- package/dist/lib/pdfium/worker.d.ts +1 -0
- package/dist/lib/pdfium-engine.d.ts +1 -0
- package/dist/lib/webworker/engine.cjs +2 -0
- package/dist/lib/webworker/engine.cjs.map +1 -0
- package/dist/{worker.d.ts → lib/webworker/engine.d.ts} +4 -34
- package/dist/lib/webworker/engine.js +887 -0
- package/dist/lib/webworker/engine.js.map +1 -0
- package/dist/lib/webworker/index.d.ts +2 -0
- package/dist/lib/webworker/runner.d.ts +128 -0
- package/dist/lib/webworker-engine.d.ts +1 -0
- package/dist/preact/adapter.d.ts +1 -0
- package/dist/preact/index.cjs +2 -0
- package/dist/preact/index.cjs.map +1 -0
- package/dist/preact/index.d.ts +1 -0
- package/dist/preact/index.js +37 -0
- package/dist/preact/index.js.map +1 -0
- package/dist/react/adapter.d.ts +1 -0
- package/dist/react/index.cjs +2 -0
- package/dist/react/index.cjs.map +1 -0
- package/dist/react/index.d.ts +1 -0
- package/dist/react/index.js +37 -0
- package/dist/react/index.js.map +1 -0
- package/dist/runner-B-s0R7NN.js +269 -0
- package/dist/runner-B-s0R7NN.js.map +1 -0
- package/dist/runner-BBBtCHic.cjs +2 -0
- package/dist/runner-BBBtCHic.cjs.map +1 -0
- package/dist/shared-preact/hooks/index.d.ts +1 -0
- package/dist/{preact.d.ts → shared-preact/hooks/use-pdfium-engine.d.ts} +3 -5
- package/dist/shared-preact/index.d.ts +1 -0
- package/dist/shared-react/hooks/index.d.ts +1 -0
- package/dist/{react.d.ts → shared-react/hooks/use-pdfium-engine.d.ts} +3 -5
- package/dist/shared-react/index.d.ts +1 -0
- package/dist/vue/hooks/index.d.ts +1 -0
- package/dist/vue/hooks/use-pdfium-engine.d.ts +18 -0
- package/dist/vue/index.cjs +2 -0
- package/dist/vue/index.cjs.map +1 -0
- package/dist/vue/index.d.ts +1 -0
- package/dist/vue/index.js +39 -0
- package/dist/vue/index.js.map +1 -0
- package/package.json +41 -41
- package/dist/converters.cjs +0 -139
- package/dist/converters.js +0 -133
- package/dist/converters.js.map +0 -1
- package/dist/pdfium-direct-engine.cjs +0 -4400
- package/dist/pdfium-direct-engine.cjs.map +0 -1
- package/dist/pdfium-direct-engine.d.ts +0 -1091
- package/dist/pdfium-direct-engine.js +0 -4398
- package/dist/pdfium-direct-engine.js.map +0 -1
- package/dist/pdfium-worker-engine.cjs +0 -851
- package/dist/pdfium-worker-engine.cjs.map +0 -1
- package/dist/pdfium-worker-engine.d.ts +0 -314
- package/dist/pdfium-worker-engine.js +0 -849
- package/dist/pdfium-worker-engine.js.map +0 -1
- package/dist/pdfium.cjs +0 -4667
- package/dist/pdfium.cjs.map +0 -1
- package/dist/pdfium.js +0 -4661
- package/dist/pdfium.js.map +0 -1
- package/dist/preact.cjs +0 -41
- package/dist/preact.cjs.map +0 -1
- package/dist/preact.js +0 -39
- package/dist/preact.js.map +0 -1
- package/dist/react.cjs +0 -41
- package/dist/react.cjs.map +0 -1
- package/dist/react.js +0 -39
- package/dist/react.js.map +0 -1
- package/dist/worker.cjs +0 -839
- package/dist/worker.cjs.map +0 -1
- package/dist/worker.js +0 -836
- package/dist/worker.js.map +0 -1
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});exports.browserImageDataToBlobConverter=(e,t="image/webp")=>{if("undefined"==typeof OffscreenCanvas)throw new Error("OffscreenCanvas is not available in this environment. This converter is intended for browser use only. Please use createNodeImageDataToBlobConverter() or createNodeCanvasImageDataToBlobConverter() for Node.js.");const a=new OffscreenCanvas(e.width,e.height);return a.getContext("2d").putImageData(e,0,0),a.convertToBlob({type:t})},exports.createCustomImageDataToBlobConverter=function(e){return async(t,a="image/webp")=>{const r=await e(t);return new Blob([r],{type:a})}},exports.createCustomImageDataToBufferConverter=function(e){return async(t,a="image/webp")=>await e(t,a)},exports.createNodeCanvasImageDataToBlobConverter=function(e){return async(t,a="image/webp")=>{const{width:r,height:o}=t,n=e(r,o);let i;switch(n.getContext("2d").putImageData(t,0,0),a){case"image/webp":i=n.toBuffer("image/webp");break;case"image/png":i=n.toBuffer("image/png");break;case"image/jpeg":i=n.toBuffer("image/jpeg");break;default:throw new Error(`Unsupported image type: ${a}`)}return i}},exports.createNodeImageDataToBufferConverter=function(e){return async(t,a="image/webp")=>{const{width:r,height:o,data:n}=t;let i,s=e(Buffer.from(n),{raw:{width:r,height:o,channels:4}});switch(a){case"image/webp":i=await s.webp().toBuffer();break;case"image/png":i=await s.png().toBuffer();break;case"image/jpeg":i=await s.flatten({background:{r:255,g:255,b:255}}).jpeg().toBuffer();break;default:throw new Error(`Unsupported image type: ${a}`)}return i}};
|
|
2
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../../../src/lib/converters/index.ts"],"sourcesContent":["/**\n * Function type for converting ImageData to Blob\n * In browser: uses OffscreenCanvas\n * In Node.js: can use Sharp or other image processing libraries\n */\nexport type ImageDataConverter<T = Blob> = (\n imageData: ImageData,\n imageType?: ImageConversionTypes,\n) => Promise<T>;\n\nexport type ImageConversionTypes = 'image/webp' | 'image/png' | 'image/jpeg';\n/**\n * Browser implementation using OffscreenCanvas\n * This is the default implementation used in browser environments\n */\nexport const browserImageDataToBlobConverter: ImageDataConverter = (\n imageData: ImageData,\n imageType: ImageConversionTypes = 'image/webp',\n): Promise<Blob> => {\n // Check if we're in a browser environment\n if (typeof OffscreenCanvas === 'undefined') {\n throw new Error(\n 'OffscreenCanvas is not available in this environment. ' +\n 'This converter is intended for browser use only. ' +\n 'Please use createNodeImageDataToBlobConverter() or createNodeCanvasImageDataToBlobConverter() for Node.js.',\n );\n }\n\n const off = new OffscreenCanvas(imageData.width, imageData.height);\n off.getContext('2d')!.putImageData(imageData, 0, 0);\n return off.convertToBlob({ type: imageType });\n};\n\n/**\n * Node.js implementation using Sharp\n * This requires the 'sharp' package to be installed\n *\n * @example\n * ```typescript\n * import sharp from 'sharp';\n * import { createNodeImageDataToBufferConverter } from '@embedpdf/engines/pdfium/image-converters';\n *\n * const converter = createNodeImageDataToBufferConverter(sharp);\n * const engine = new PdfiumEngine(pdfiumModule, logger, converter);\n * ```\n */\nexport function createNodeImageDataToBufferConverter(\n sharp: any, // Using 'any' to avoid requiring sharp as a dependency\n): ImageDataConverter<Buffer> {\n return async (\n imageData: ImageData,\n imageType: ImageConversionTypes = 'image/webp',\n ): Promise<Buffer> => {\n const { width, height, data } = imageData;\n\n // Convert ImageData to Sharp format\n // ImageData uses RGBA format, Sharp expects the same\n let sharpInstance = sharp(Buffer.from(data), {\n raw: {\n width,\n height,\n channels: 4, // RGBA\n },\n });\n\n // Apply the appropriate format conversion based on imageType\n let buffer: Buffer;\n switch (imageType) {\n case 'image/webp':\n buffer = await sharpInstance.webp().toBuffer();\n break;\n case 'image/png':\n buffer = await sharpInstance.png().toBuffer();\n break;\n case 'image/jpeg':\n // JPEG doesn't support transparency, so we need to composite onto a white background\n buffer = await sharpInstance\n .flatten({ background: { r: 255, g: 255, b: 255 } }) // Remove alpha channel with white background\n .jpeg()\n .toBuffer();\n break;\n default:\n throw new Error(`Unsupported image type: ${imageType}`);\n }\n\n return buffer;\n };\n}\n\n/**\n * Alternative Node.js implementation using canvas (node-canvas)\n * This requires the 'canvas' package to be installed\n *\n * @example\n * ```typescript\n * import { createCanvas } from 'canvas';\n * import { createNodeCanvasImageDataToBlobConverter } from '@embedpdf/engines/pdfium/image-converters';\n *\n * const converter = createNodeCanvasImageDataToBlobConverter(createCanvas, 'image/png');\n * const engine = new PdfiumEngine(pdfiumModule, logger, converter);\n * ```\n */\nexport function createNodeCanvasImageDataToBlobConverter(\n createCanvas: any, // Using 'any' to avoid requiring canvas as a dependency\n): ImageDataConverter<Buffer> {\n return async (\n imageData: ImageData,\n imageType: ImageConversionTypes = 'image/webp',\n ): Promise<Buffer> => {\n const { width, height } = imageData;\n\n // Create a canvas and put the image data\n const canvas = createCanvas(width, height);\n const ctx = canvas.getContext('2d');\n ctx.putImageData(imageData, 0, 0);\n\n // Convert to buffer and create blob based on the requested type\n let buffer: Buffer;\n switch (imageType) {\n case 'image/webp':\n buffer = canvas.toBuffer('image/webp');\n break;\n case 'image/png':\n buffer = canvas.toBuffer('image/png');\n break;\n case 'image/jpeg':\n buffer = canvas.toBuffer('image/jpeg');\n break;\n default:\n throw new Error(`Unsupported image type: ${imageType}`);\n }\n\n return buffer;\n };\n}\n\n/**\n * Generic Node.js implementation that works with any image processing library\n * that can handle raw RGBA data\n *\n * @example\n * ```typescript\n * const converter = createCustomImageDataToBlobConverter(async (imageData) => {\n * // Your custom image processing logic here\n * // Return a Buffer that will be wrapped in a Blob\n * return processImageWithYourLibrary(imageData);\n * });\n * ```\n */\nexport function createCustomImageDataToBlobConverter(\n processor: (imageData: ImageData) => Promise<Buffer>,\n): ImageDataConverter {\n return async (\n imageData: ImageData,\n imageType: ImageConversionTypes = 'image/webp',\n ): Promise<Blob> => {\n const buffer = await processor(imageData);\n return new Blob([buffer], { type: imageType });\n };\n}\n\n/**\n * Create a custom converter that returns a Buffer\n * @param processor - function to process the image data\n * @param imageType - image type\n * @returns ImageDataToBlobConverter<Buffer>\n */\nexport function createCustomImageDataToBufferConverter(\n processor: (imageData: ImageData, imageType: ImageConversionTypes) => Promise<Buffer>,\n): ImageDataConverter<Buffer> {\n return async (\n imageData: ImageData,\n imageType: ImageConversionTypes = 'image/webp',\n ): Promise<Buffer> => {\n return await processor(imageData, imageType);\n };\n}\n"],"names":["imageData","imageType","OffscreenCanvas","Error","off","width","height","getContext","putImageData","convertToBlob","type","processor","async","buffer","Blob","createCanvas","canvas","toBuffer","sharp","data","sharpInstance","Buffer","from","raw","channels","webp","png","flatten","background","r","g","b","jpeg"],"mappings":"wHAemE,CACjEA,EACAC,EAAkC,gBAG9B,GAA2B,oBAApBC,gBACT,MAAM,IAAIC,MACR,qNAMJ,MAAMC,EAAM,IAAIF,gBAAgBF,EAAUK,MAAOL,EAAUM,QAE3D,OADAF,EAAIG,WAAW,MAAOC,aAAaR,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js: can use Sharp or other image processing libraries
|
|
5
5
|
*/
|
|
6
|
-
type ImageDataConverter<T = Blob> = (imageData: ImageData, imageType?: ImageConversionTypes) => Promise<T>;
|
|
7
|
-
type ImageConversionTypes = 'image/webp' | 'image/png' | 'image/jpeg';
|
|
6
|
+
export type ImageDataConverter<T = Blob> = (imageData: ImageData, imageType?: ImageConversionTypes) => Promise<T>;
|
|
7
|
+
export type ImageConversionTypes = 'image/webp' | 'image/png' | 'image/jpeg';
|
|
8
8
|
/**
|
|
9
9
|
* Browser implementation using OffscreenCanvas
|
|
10
10
|
* This is the default implementation used in browser environments
|
|
11
11
|
*/
|
|
12
|
-
declare const browserImageDataToBlobConverter: ImageDataConverter;
|
|
12
|
+
export declare const browserImageDataToBlobConverter: ImageDataConverter;
|
|
13
13
|
/**
|
|
14
14
|
* Node.js implementation using Sharp
|
|
15
15
|
* This requires the 'sharp' package to be installed
|
|
@@ -23,7 +23,7 @@ declare const browserImageDataToBlobConverter: ImageDataConverter;
|
|
|
23
23
|
* const engine = new PdfiumEngine(pdfiumModule, logger, converter);
|
|
24
24
|
* ```
|
|
25
25
|
*/
|
|
26
|
-
declare function createNodeImageDataToBufferConverter(sharp: any): ImageDataConverter<Buffer>;
|
|
26
|
+
export declare function createNodeImageDataToBufferConverter(sharp: any): ImageDataConverter<Buffer>;
|
|
27
27
|
/**
|
|
28
28
|
* Alternative Node.js implementation using canvas (node-canvas)
|
|
29
29
|
* This requires the 'canvas' package to be installed
|
|
@@ -37,7 +37,7 @@ declare function createNodeImageDataToBufferConverter(sharp: any): ImageDataConv
|
|
|
37
37
|
* const engine = new PdfiumEngine(pdfiumModule, logger, converter);
|
|
38
38
|
* ```
|
|
39
39
|
*/
|
|
40
|
-
declare function createNodeCanvasImageDataToBlobConverter(createCanvas: any): ImageDataConverter<Buffer>;
|
|
40
|
+
export declare function createNodeCanvasImageDataToBlobConverter(createCanvas: any): ImageDataConverter<Buffer>;
|
|
41
41
|
/**
|
|
42
42
|
* Generic Node.js implementation that works with any image processing library
|
|
43
43
|
* that can handle raw RGBA data
|
|
@@ -51,14 +51,11 @@ declare function createNodeCanvasImageDataToBlobConverter(createCanvas: any): Im
|
|
|
51
51
|
* });
|
|
52
52
|
* ```
|
|
53
53
|
*/
|
|
54
|
-
declare function createCustomImageDataToBlobConverter(processor: (imageData: ImageData) => Promise<Buffer>): ImageDataConverter;
|
|
54
|
+
export declare function createCustomImageDataToBlobConverter(processor: (imageData: ImageData) => Promise<Buffer>): ImageDataConverter;
|
|
55
55
|
/**
|
|
56
56
|
* Create a custom converter that returns a Buffer
|
|
57
57
|
* @param processor - function to process the image data
|
|
58
58
|
* @param imageType - image type
|
|
59
59
|
* @returns ImageDataToBlobConverter<Buffer>
|
|
60
60
|
*/
|
|
61
|
-
declare function createCustomImageDataToBufferConverter(processor: (imageData: ImageData, imageType: ImageConversionTypes) => Promise<Buffer>): ImageDataConverter<Buffer>;
|
|
62
|
-
|
|
63
|
-
export { browserImageDataToBlobConverter, createCustomImageDataToBlobConverter, createCustomImageDataToBufferConverter, createNodeCanvasImageDataToBlobConverter, createNodeImageDataToBufferConverter };
|
|
64
|
-
export type { ImageConversionTypes, ImageDataConverter };
|
|
61
|
+
export declare function createCustomImageDataToBufferConverter(processor: (imageData: ImageData, imageType: ImageConversionTypes) => Promise<Buffer>): ImageDataConverter<Buffer>;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
const browserImageDataToBlobConverter = (imageData, imageType = "image/webp") => {
|
|
2
|
+
if (typeof OffscreenCanvas === "undefined") {
|
|
3
|
+
throw new Error(
|
|
4
|
+
"OffscreenCanvas is not available in this environment. This converter is intended for browser use only. Please use createNodeImageDataToBlobConverter() or createNodeCanvasImageDataToBlobConverter() for Node.js."
|
|
5
|
+
);
|
|
6
|
+
}
|
|
7
|
+
const off = new OffscreenCanvas(imageData.width, imageData.height);
|
|
8
|
+
off.getContext("2d").putImageData(imageData, 0, 0);
|
|
9
|
+
return off.convertToBlob({ type: imageType });
|
|
10
|
+
};
|
|
11
|
+
function createNodeImageDataToBufferConverter(sharp) {
|
|
12
|
+
return async (imageData, imageType = "image/webp") => {
|
|
13
|
+
const { width, height, data } = imageData;
|
|
14
|
+
let sharpInstance = sharp(Buffer.from(data), {
|
|
15
|
+
raw: {
|
|
16
|
+
width,
|
|
17
|
+
height,
|
|
18
|
+
channels: 4
|
|
19
|
+
// RGBA
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
let buffer;
|
|
23
|
+
switch (imageType) {
|
|
24
|
+
case "image/webp":
|
|
25
|
+
buffer = await sharpInstance.webp().toBuffer();
|
|
26
|
+
break;
|
|
27
|
+
case "image/png":
|
|
28
|
+
buffer = await sharpInstance.png().toBuffer();
|
|
29
|
+
break;
|
|
30
|
+
case "image/jpeg":
|
|
31
|
+
buffer = await sharpInstance.flatten({ background: { r: 255, g: 255, b: 255 } }).jpeg().toBuffer();
|
|
32
|
+
break;
|
|
33
|
+
default:
|
|
34
|
+
throw new Error(`Unsupported image type: ${imageType}`);
|
|
35
|
+
}
|
|
36
|
+
return buffer;
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
function createNodeCanvasImageDataToBlobConverter(createCanvas) {
|
|
40
|
+
return async (imageData, imageType = "image/webp") => {
|
|
41
|
+
const { width, height } = imageData;
|
|
42
|
+
const canvas = createCanvas(width, height);
|
|
43
|
+
const ctx = canvas.getContext("2d");
|
|
44
|
+
ctx.putImageData(imageData, 0, 0);
|
|
45
|
+
let buffer;
|
|
46
|
+
switch (imageType) {
|
|
47
|
+
case "image/webp":
|
|
48
|
+
buffer = canvas.toBuffer("image/webp");
|
|
49
|
+
break;
|
|
50
|
+
case "image/png":
|
|
51
|
+
buffer = canvas.toBuffer("image/png");
|
|
52
|
+
break;
|
|
53
|
+
case "image/jpeg":
|
|
54
|
+
buffer = canvas.toBuffer("image/jpeg");
|
|
55
|
+
break;
|
|
56
|
+
default:
|
|
57
|
+
throw new Error(`Unsupported image type: ${imageType}`);
|
|
58
|
+
}
|
|
59
|
+
return buffer;
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
function createCustomImageDataToBlobConverter(processor) {
|
|
63
|
+
return async (imageData, imageType = "image/webp") => {
|
|
64
|
+
const buffer = await processor(imageData);
|
|
65
|
+
return new Blob([buffer], { type: imageType });
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
function createCustomImageDataToBufferConverter(processor) {
|
|
69
|
+
return async (imageData, imageType = "image/webp") => {
|
|
70
|
+
return await processor(imageData, imageType);
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
export {
|
|
74
|
+
browserImageDataToBlobConverter,
|
|
75
|
+
createCustomImageDataToBlobConverter,
|
|
76
|
+
createCustomImageDataToBufferConverter,
|
|
77
|
+
createNodeCanvasImageDataToBlobConverter,
|
|
78
|
+
createNodeImageDataToBufferConverter
|
|
79
|
+
};
|
|
80
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../../src/lib/converters/index.ts"],"sourcesContent":["/**\n * Function type for converting ImageData to Blob\n * In browser: uses OffscreenCanvas\n * In Node.js: can use Sharp or other image processing libraries\n */\nexport type ImageDataConverter<T = Blob> = (\n imageData: ImageData,\n imageType?: ImageConversionTypes,\n) => Promise<T>;\n\nexport type ImageConversionTypes = 'image/webp' | 'image/png' | 'image/jpeg';\n/**\n * Browser implementation using OffscreenCanvas\n * This is the default implementation used in browser environments\n */\nexport const browserImageDataToBlobConverter: ImageDataConverter = (\n imageData: ImageData,\n imageType: ImageConversionTypes = 'image/webp',\n): Promise<Blob> => {\n // Check if we're in a browser environment\n if (typeof OffscreenCanvas === 'undefined') {\n throw new Error(\n 'OffscreenCanvas is not available in this environment. ' +\n 'This converter is intended for browser use only. ' +\n 'Please use createNodeImageDataToBlobConverter() or createNodeCanvasImageDataToBlobConverter() for Node.js.',\n );\n }\n\n const off = new OffscreenCanvas(imageData.width, imageData.height);\n off.getContext('2d')!.putImageData(imageData, 0, 0);\n return off.convertToBlob({ type: imageType });\n};\n\n/**\n * Node.js implementation using Sharp\n * This requires the 'sharp' package to be installed\n *\n * @example\n * ```typescript\n * import sharp from 'sharp';\n * import { createNodeImageDataToBufferConverter } from '@embedpdf/engines/pdfium/image-converters';\n *\n * const converter = createNodeImageDataToBufferConverter(sharp);\n * const engine = new PdfiumEngine(pdfiumModule, logger, converter);\n * ```\n */\nexport function createNodeImageDataToBufferConverter(\n sharp: any, // Using 'any' to avoid requiring sharp as a dependency\n): ImageDataConverter<Buffer> {\n return async (\n imageData: ImageData,\n imageType: ImageConversionTypes = 'image/webp',\n ): Promise<Buffer> => {\n const { width, height, data } = imageData;\n\n // Convert ImageData to Sharp format\n // ImageData uses RGBA format, Sharp expects the same\n let sharpInstance = sharp(Buffer.from(data), {\n raw: {\n width,\n height,\n channels: 4, // RGBA\n },\n });\n\n // Apply the appropriate format conversion based on imageType\n let buffer: Buffer;\n switch (imageType) {\n case 'image/webp':\n buffer = await sharpInstance.webp().toBuffer();\n break;\n case 'image/png':\n buffer = await sharpInstance.png().toBuffer();\n break;\n case 'image/jpeg':\n // JPEG doesn't support transparency, so we need to composite onto a white background\n buffer = await sharpInstance\n .flatten({ background: { r: 255, g: 255, b: 255 } }) // Remove alpha channel with white background\n .jpeg()\n .toBuffer();\n break;\n default:\n throw new Error(`Unsupported image type: ${imageType}`);\n }\n\n return buffer;\n };\n}\n\n/**\n * Alternative Node.js implementation using canvas (node-canvas)\n * This requires the 'canvas' package to be installed\n *\n * @example\n * ```typescript\n * import { createCanvas } from 'canvas';\n * import { createNodeCanvasImageDataToBlobConverter } from '@embedpdf/engines/pdfium/image-converters';\n *\n * const converter = createNodeCanvasImageDataToBlobConverter(createCanvas, 'image/png');\n * const engine = new PdfiumEngine(pdfiumModule, logger, converter);\n * ```\n */\nexport function createNodeCanvasImageDataToBlobConverter(\n createCanvas: any, // Using 'any' to avoid requiring canvas as a dependency\n): ImageDataConverter<Buffer> {\n return async (\n imageData: ImageData,\n imageType: ImageConversionTypes = 'image/webp',\n ): Promise<Buffer> => {\n const { width, height } = imageData;\n\n // Create a canvas and put the image data\n const canvas = createCanvas(width, height);\n const ctx = canvas.getContext('2d');\n ctx.putImageData(imageData, 0, 0);\n\n // Convert to buffer and create blob based on the requested type\n let buffer: Buffer;\n switch (imageType) {\n case 'image/webp':\n buffer = canvas.toBuffer('image/webp');\n break;\n case 'image/png':\n buffer = canvas.toBuffer('image/png');\n break;\n case 'image/jpeg':\n buffer = canvas.toBuffer('image/jpeg');\n break;\n default:\n throw new Error(`Unsupported image type: ${imageType}`);\n }\n\n return buffer;\n };\n}\n\n/**\n * Generic Node.js implementation that works with any image processing library\n * that can handle raw RGBA data\n *\n * @example\n * ```typescript\n * const converter = createCustomImageDataToBlobConverter(async (imageData) => {\n * // Your custom image processing logic here\n * // Return a Buffer that will be wrapped in a Blob\n * return processImageWithYourLibrary(imageData);\n * });\n * ```\n */\nexport function createCustomImageDataToBlobConverter(\n processor: (imageData: ImageData) => Promise<Buffer>,\n): ImageDataConverter {\n return async (\n imageData: ImageData,\n imageType: ImageConversionTypes = 'image/webp',\n ): Promise<Blob> => {\n const buffer = await processor(imageData);\n return new Blob([buffer], { type: imageType });\n };\n}\n\n/**\n * Create a custom converter that returns a Buffer\n * @param processor - function to process the image data\n * @param imageType - image type\n * @returns ImageDataToBlobConverter<Buffer>\n */\nexport function createCustomImageDataToBufferConverter(\n processor: (imageData: ImageData, imageType: ImageConversionTypes) => Promise<Buffer>,\n): ImageDataConverter<Buffer> {\n return async (\n imageData: ImageData,\n imageType: ImageConversionTypes = 'image/webp',\n ): Promise<Buffer> => {\n return await processor(imageData, imageType);\n };\n}\n"],"names":[],"mappings":"AAeO,MAAM,kCAAsD,CACjE,WACA,YAAkC,iBAChB;AAEd,MAAA,OAAO,oBAAoB,aAAa;AAC1C,UAAM,IAAI;AAAA,MACR;AAAA,IAGF;AAAA,EAAA;AAGF,QAAM,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B,SAAS,EAAE;AAAA,IAAA;AAGnD,WAAA;AAAA,EACT;AACF;AAeO,SAAS,qCACd,WACoB;AACb,SAAA,OACL,WACA,YAAkC,iBAChB;AACZ,UAAA,SAAS,MAAM,UAAU,SAAS;AACjC,WAAA,IAAI,KAAK,CAAC,MAAM,GAAG,EAAE,MAAM,WAAW;AAAA,EAC/C;AACF;AAQO,SAAS,uCACd,WAC4B;AACrB,SAAA,OACL,WACA,YAAkC,iBACd;AACb,WAAA,MAAM,UAAU,WAAW,SAAS;AAAA,EAC7C;AACF;"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { PdfDocumentObject, PdfEngine, PdfFile } from '@embedpdf/models';
|
|
2
|
+
/**
|
|
3
|
+
* Create mock of pdf engine
|
|
4
|
+
* @param partialEngine - partial configuration of engine
|
|
5
|
+
* @returns - mock of pdf engine
|
|
6
|
+
*
|
|
7
|
+
* @public
|
|
8
|
+
*/
|
|
9
|
+
export declare function createMockPdfEngine(partialEngine?: Partial<PdfEngine>): PdfEngine;
|
|
10
|
+
/**
|
|
11
|
+
* Create mock of pdf document
|
|
12
|
+
* @param doc - partial configuration of document
|
|
13
|
+
* @returns mock of pdf document
|
|
14
|
+
*
|
|
15
|
+
* @public
|
|
16
|
+
*/
|
|
17
|
+
export declare function createMockPdfDocument(doc?: Partial<PdfDocumentObject>): PdfDocumentObject;
|
|
18
|
+
/**
|
|
19
|
+
* Create mock of pdf file
|
|
20
|
+
* @param file - partial configuration of file
|
|
21
|
+
* @returns mock of pdf file
|
|
22
|
+
*
|
|
23
|
+
* @public
|
|
24
|
+
*/
|
|
25
|
+
export declare function createMockPdfFile(file?: Partial<PdfFile>): PdfFile;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { WrappedPdfiumModule } from '@embedpdf/pdfium';
|
|
2
|
+
export declare class PdfCache {
|
|
3
|
+
private readonly pdfium;
|
|
4
|
+
private readonly docs;
|
|
5
|
+
constructor(pdfium: WrappedPdfiumModule);
|
|
6
|
+
/** Open (or re-use) a document */
|
|
7
|
+
setDocument(id: string, filePtr: number, docPtr: number): void;
|
|
8
|
+
/** Retrieve the DocumentContext for a given PdfDocumentObject */
|
|
9
|
+
getContext(docId: string): DocumentContext | undefined;
|
|
10
|
+
/** Close & fully release a document and all its pages */
|
|
11
|
+
closeDocument(docId: string): boolean;
|
|
12
|
+
}
|
|
13
|
+
export declare class DocumentContext {
|
|
14
|
+
readonly filePtr: number;
|
|
15
|
+
readonly docPtr: number;
|
|
16
|
+
private readonly pageCache;
|
|
17
|
+
constructor(filePtr: number, docPtr: number, pdfium: WrappedPdfiumModule);
|
|
18
|
+
/** Main accessor for pages */
|
|
19
|
+
acquirePage(pageIdx: number): PageContext;
|
|
20
|
+
/** Tear down all pages + this document */
|
|
21
|
+
dispose(): void;
|
|
22
|
+
}
|
|
23
|
+
export declare class PageCache {
|
|
24
|
+
readonly pdf: WrappedPdfiumModule;
|
|
25
|
+
private readonly docPtr;
|
|
26
|
+
private readonly cache;
|
|
27
|
+
constructor(pdf: WrappedPdfiumModule, docPtr: number);
|
|
28
|
+
acquire(pageIdx: number): PageContext;
|
|
29
|
+
forceReleaseAll(): void;
|
|
30
|
+
}
|
|
31
|
+
export declare class PageContext {
|
|
32
|
+
private readonly pdf;
|
|
33
|
+
readonly docPtr: number;
|
|
34
|
+
readonly pageIdx: number;
|
|
35
|
+
readonly pagePtr: number;
|
|
36
|
+
private readonly onFinalDispose;
|
|
37
|
+
private refCount;
|
|
38
|
+
private expiryTimer?;
|
|
39
|
+
private disposed;
|
|
40
|
+
private textPagePtr?;
|
|
41
|
+
private formInfoPtr?;
|
|
42
|
+
private formHandle?;
|
|
43
|
+
constructor(pdf: WrappedPdfiumModule, docPtr: number, pageIdx: number, pagePtr: number, onFinalDispose: () => void);
|
|
44
|
+
/** Called by PageCache.acquire() */
|
|
45
|
+
bumpRefCount(): void;
|
|
46
|
+
/** Called by PageCache.acquire() */
|
|
47
|
+
clearExpiryTimer(): void;
|
|
48
|
+
/** Called by PageCache.release() internally */
|
|
49
|
+
release(): void;
|
|
50
|
+
/** Tear down _all_ sub-pointers & the page. */
|
|
51
|
+
disposeImmediate(): void;
|
|
52
|
+
/** Always safe: opens (once) and returns the text-page ptr. */
|
|
53
|
+
getTextPage(): number;
|
|
54
|
+
/** Always safe: opens (once) and returns the form-fill handle. */
|
|
55
|
+
getFormHandle(): number;
|
|
56
|
+
/**
|
|
57
|
+
* Safely execute `fn` with an annotation pointer.
|
|
58
|
+
* Pointer is ALWAYS closed afterwards.
|
|
59
|
+
*/
|
|
60
|
+
withAnnotation<T>(annotIdx: number, fn: (annotPtr: number) => T): T;
|
|
61
|
+
private ensureAlive;
|
|
62
|
+
}
|