@embedpdf/engines 1.0.4 → 1.0.6
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/converters.cjs +10 -29
- package/dist/converters.cjs.map +1 -1
- package/dist/converters.d.ts +6 -12
- package/dist/converters.js +11 -29
- package/dist/converters.js.map +1 -1
- package/dist/index.cjs +113 -19
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +18 -6
- package/dist/index.js +114 -20
- package/dist/index.js.map +1 -1
- package/dist/pdfium-direct-engine.cjs +86 -18
- package/dist/pdfium-direct-engine.cjs.map +1 -1
- package/dist/pdfium-direct-engine.d.ts +11 -5
- package/dist/pdfium-direct-engine.js +86 -18
- package/dist/pdfium-direct-engine.js.map +1 -1
- package/dist/pdfium-worker-engine.cjs +28 -7
- package/dist/pdfium-worker-engine.cjs.map +1 -1
- package/dist/pdfium-worker-engine.d.ts +10 -4
- package/dist/pdfium-worker-engine.js +28 -7
- package/dist/pdfium-worker-engine.js.map +1 -1
- package/dist/pdfium.cjs +86 -15
- package/dist/pdfium.cjs.map +1 -1
- package/dist/pdfium.d.ts +10 -4
- package/dist/pdfium.js +87 -16
- package/dist/pdfium.js.map +1 -1
- package/dist/worker.cjs +24 -4
- package/dist/worker.cjs.map +1 -1
- package/dist/worker.d.ts +9 -3
- package/dist/worker.js +24 -4
- package/dist/worker.js.map +1 -1
- package/package.json +3 -3
package/dist/converters.cjs
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* Browser implementation using OffscreenCanvas
|
|
5
5
|
* This is the default implementation used in browser environments
|
|
6
6
|
*/
|
|
7
|
-
const browserImageDataToBlobConverter = (imageData) => {
|
|
7
|
+
const browserImageDataToBlobConverter = (imageData, imageType = 'image/webp') => {
|
|
8
8
|
// Check if we're in a browser environment
|
|
9
9
|
if (typeof OffscreenCanvas === 'undefined') {
|
|
10
10
|
throw new Error('OffscreenCanvas is not available in this environment. ' +
|
|
@@ -13,24 +13,8 @@ const browserImageDataToBlobConverter = (imageData) => {
|
|
|
13
13
|
}
|
|
14
14
|
const off = new OffscreenCanvas(imageData.width, imageData.height);
|
|
15
15
|
off.getContext('2d').putImageData(imageData, 0, 0);
|
|
16
|
-
return off.convertToBlob({ type:
|
|
16
|
+
return off.convertToBlob({ type: imageType });
|
|
17
17
|
};
|
|
18
|
-
/**
|
|
19
|
-
* Create a browser implementation with custom image type
|
|
20
|
-
*/
|
|
21
|
-
function createBrowserImageDataToBlobConverter(imageType = 'image/webp') {
|
|
22
|
-
return (imageData) => {
|
|
23
|
-
// Check if we're in a browser environment
|
|
24
|
-
if (typeof OffscreenCanvas === 'undefined') {
|
|
25
|
-
throw new Error('OffscreenCanvas is not available in this environment. ' +
|
|
26
|
-
'This converter is intended for browser use only. ' +
|
|
27
|
-
'Please use createNodeImageDataToBlobConverter() or createNodeCanvasImageDataToBlobConverter() for Node.js.');
|
|
28
|
-
}
|
|
29
|
-
const off = new OffscreenCanvas(imageData.width, imageData.height);
|
|
30
|
-
off.getContext('2d').putImageData(imageData, 0, 0);
|
|
31
|
-
return off.convertToBlob({ type: imageType });
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
18
|
/**
|
|
35
19
|
* Node.js implementation using Sharp
|
|
36
20
|
* This requires the 'sharp' package to be installed
|
|
@@ -44,9 +28,8 @@ function createBrowserImageDataToBlobConverter(imageType = 'image/webp') {
|
|
|
44
28
|
* const engine = new PdfiumEngine(pdfiumModule, logger, converter);
|
|
45
29
|
* ```
|
|
46
30
|
*/
|
|
47
|
-
function createNodeImageDataToBufferConverter(sharp
|
|
48
|
-
imageType = 'image/webp') {
|
|
49
|
-
return async (imageData) => {
|
|
31
|
+
function createNodeImageDataToBufferConverter(sharp) {
|
|
32
|
+
return async (imageData, imageType = 'image/webp') => {
|
|
50
33
|
const { width, height, data } = imageData;
|
|
51
34
|
// Convert ImageData to Sharp format
|
|
52
35
|
// ImageData uses RGBA format, Sharp expects the same
|
|
@@ -92,9 +75,8 @@ imageType = 'image/webp') {
|
|
|
92
75
|
* const engine = new PdfiumEngine(pdfiumModule, logger, converter);
|
|
93
76
|
* ```
|
|
94
77
|
*/
|
|
95
|
-
function createNodeCanvasImageDataToBlobConverter(createCanvas
|
|
96
|
-
imageType = 'image/webp') {
|
|
97
|
-
return async (imageData) => {
|
|
78
|
+
function createNodeCanvasImageDataToBlobConverter(createCanvas) {
|
|
79
|
+
return async (imageData, imageType = 'image/webp') => {
|
|
98
80
|
const { width, height } = imageData;
|
|
99
81
|
// Create a canvas and put the image data
|
|
100
82
|
const canvas = createCanvas(width, height);
|
|
@@ -131,8 +113,8 @@ imageType = 'image/webp') {
|
|
|
131
113
|
* });
|
|
132
114
|
* ```
|
|
133
115
|
*/
|
|
134
|
-
function createCustomImageDataToBlobConverter(processor
|
|
135
|
-
return async (imageData) => {
|
|
116
|
+
function createCustomImageDataToBlobConverter(processor) {
|
|
117
|
+
return async (imageData, imageType = 'image/webp') => {
|
|
136
118
|
const buffer = await processor(imageData);
|
|
137
119
|
return new Blob([buffer], { type: imageType });
|
|
138
120
|
};
|
|
@@ -144,13 +126,12 @@ function createCustomImageDataToBlobConverter(processor, imageType = 'image/webp
|
|
|
144
126
|
* @returns ImageDataToBlobConverter<Buffer>
|
|
145
127
|
*/
|
|
146
128
|
function createCustomImageDataToBufferConverter(processor) {
|
|
147
|
-
return async (imageData) => {
|
|
148
|
-
return await processor(imageData);
|
|
129
|
+
return async (imageData, imageType = 'image/webp') => {
|
|
130
|
+
return await processor(imageData, imageType);
|
|
149
131
|
};
|
|
150
132
|
}
|
|
151
133
|
|
|
152
134
|
exports.browserImageDataToBlobConverter = browserImageDataToBlobConverter;
|
|
153
|
-
exports.createBrowserImageDataToBlobConverter = createBrowserImageDataToBlobConverter;
|
|
154
135
|
exports.createCustomImageDataToBlobConverter = createCustomImageDataToBlobConverter;
|
|
155
136
|
exports.createCustomImageDataToBufferConverter = createCustomImageDataToBufferConverter;
|
|
156
137
|
exports.createNodeCanvasImageDataToBlobConverter = createNodeCanvasImageDataToBlobConverter;
|
package/dist/converters.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"converters.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> = (imageData: ImageData) => 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
|
|
1
|
+
{"version":3,"file":"converters.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":[],"mappings":";;AAWA;;;AAGG;AACU,MAAA,+BAA+B,GAAuB,CACjE,SAAoB,EACpB,SAAA,GAAkC,YAAY,KAC7B;;AAEjB,IAAA,IAAI,OAAO,eAAe,KAAK,WAAW,EAAE;QAC1C,MAAM,IAAI,KAAK,CACb,wDAAwD;YACtD,mDAAmD;AACnD,YAAA,4GAA4G,CAC/G,CAAC;KACH;AAED,IAAA,MAAM,GAAG,GAAG,IAAI,eAAe,CAAC,SAAS,CAAC,KAAK,EAAE,SAAS,CAAC,MAAM,CAAC,CAAC;AACnE,IAAA,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D,KAAC,CAAC;AACJ,CAAC;AAED;;;;;AAKG;AACG,SAAU,sCAAsC,CACpD,SAAqF,EAAA;AAErF,IAAA,OAAO,OACL,SAAoB,EACpB,SAAkC,GAAA,YAAY,KAC3B;AACnB,QAAA,OAAO,MAAM,SAAS,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC;AAC/C,KAAC,CAAC;AACJ;;;;;;;;"}
|
package/dist/converters.d.ts
CHANGED
|
@@ -3,17 +3,13 @@
|
|
|
3
3
|
* In browser: uses OffscreenCanvas
|
|
4
4
|
* In Node.js: can use Sharp or other image processing libraries
|
|
5
5
|
*/
|
|
6
|
-
type ImageDataConverter<T = Blob> = (imageData: ImageData) => Promise<T>;
|
|
6
|
+
type ImageDataConverter<T = Blob> = (imageData: ImageData, imageType?: ImageConversionTypes) => Promise<T>;
|
|
7
7
|
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
12
|
declare const browserImageDataToBlobConverter: ImageDataConverter;
|
|
13
|
-
/**
|
|
14
|
-
* Create a browser implementation with custom image type
|
|
15
|
-
*/
|
|
16
|
-
declare function createBrowserImageDataToBlobConverter(imageType?: ImageConversionTypes): ImageDataConverter;
|
|
17
13
|
/**
|
|
18
14
|
* Node.js implementation using Sharp
|
|
19
15
|
* This requires the 'sharp' package to be installed
|
|
@@ -27,8 +23,7 @@ declare function createBrowserImageDataToBlobConverter(imageType?: ImageConversi
|
|
|
27
23
|
* const engine = new PdfiumEngine(pdfiumModule, logger, converter);
|
|
28
24
|
* ```
|
|
29
25
|
*/
|
|
30
|
-
declare function createNodeImageDataToBufferConverter(sharp: any
|
|
31
|
-
imageType?: ImageConversionTypes): ImageDataConverter<Buffer>;
|
|
26
|
+
declare function createNodeImageDataToBufferConverter(sharp: any): ImageDataConverter<Buffer>;
|
|
32
27
|
/**
|
|
33
28
|
* Alternative Node.js implementation using canvas (node-canvas)
|
|
34
29
|
* This requires the 'canvas' package to be installed
|
|
@@ -42,8 +37,7 @@ imageType?: ImageConversionTypes): ImageDataConverter<Buffer>;
|
|
|
42
37
|
* const engine = new PdfiumEngine(pdfiumModule, logger, converter);
|
|
43
38
|
* ```
|
|
44
39
|
*/
|
|
45
|
-
declare function createNodeCanvasImageDataToBlobConverter(createCanvas: any
|
|
46
|
-
imageType?: ImageConversionTypes): ImageDataConverter<Buffer>;
|
|
40
|
+
declare function createNodeCanvasImageDataToBlobConverter(createCanvas: any): ImageDataConverter<Buffer>;
|
|
47
41
|
/**
|
|
48
42
|
* Generic Node.js implementation that works with any image processing library
|
|
49
43
|
* that can handle raw RGBA data
|
|
@@ -57,14 +51,14 @@ imageType?: ImageConversionTypes): ImageDataConverter<Buffer>;
|
|
|
57
51
|
* });
|
|
58
52
|
* ```
|
|
59
53
|
*/
|
|
60
|
-
declare function createCustomImageDataToBlobConverter(processor: (imageData: ImageData) => Promise<Buffer
|
|
54
|
+
declare function createCustomImageDataToBlobConverter(processor: (imageData: ImageData) => Promise<Buffer>): ImageDataConverter;
|
|
61
55
|
/**
|
|
62
56
|
* Create a custom converter that returns a Buffer
|
|
63
57
|
* @param processor - function to process the image data
|
|
64
58
|
* @param imageType - image type
|
|
65
59
|
* @returns ImageDataToBlobConverter<Buffer>
|
|
66
60
|
*/
|
|
67
|
-
declare function createCustomImageDataToBufferConverter(processor: (imageData: ImageData) => Promise<Buffer>): ImageDataConverter<Buffer>;
|
|
61
|
+
declare function createCustomImageDataToBufferConverter(processor: (imageData: ImageData, imageType: ImageConversionTypes) => Promise<Buffer>): ImageDataConverter<Buffer>;
|
|
68
62
|
|
|
69
|
-
export { browserImageDataToBlobConverter,
|
|
63
|
+
export { browserImageDataToBlobConverter, createCustomImageDataToBlobConverter, createCustomImageDataToBufferConverter, createNodeCanvasImageDataToBlobConverter, createNodeImageDataToBufferConverter };
|
|
70
64
|
export type { ImageConversionTypes, ImageDataConverter };
|
package/dist/converters.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Browser implementation using OffscreenCanvas
|
|
3
3
|
* This is the default implementation used in browser environments
|
|
4
4
|
*/
|
|
5
|
-
const browserImageDataToBlobConverter = (imageData) => {
|
|
5
|
+
const browserImageDataToBlobConverter = (imageData, imageType = 'image/webp') => {
|
|
6
6
|
// Check if we're in a browser environment
|
|
7
7
|
if (typeof OffscreenCanvas === 'undefined') {
|
|
8
8
|
throw new Error('OffscreenCanvas is not available in this environment. ' +
|
|
@@ -11,24 +11,8 @@ const browserImageDataToBlobConverter = (imageData) => {
|
|
|
11
11
|
}
|
|
12
12
|
const off = new OffscreenCanvas(imageData.width, imageData.height);
|
|
13
13
|
off.getContext('2d').putImageData(imageData, 0, 0);
|
|
14
|
-
return off.convertToBlob({ type:
|
|
14
|
+
return off.convertToBlob({ type: imageType });
|
|
15
15
|
};
|
|
16
|
-
/**
|
|
17
|
-
* Create a browser implementation with custom image type
|
|
18
|
-
*/
|
|
19
|
-
function createBrowserImageDataToBlobConverter(imageType = 'image/webp') {
|
|
20
|
-
return (imageData) => {
|
|
21
|
-
// Check if we're in a browser environment
|
|
22
|
-
if (typeof OffscreenCanvas === 'undefined') {
|
|
23
|
-
throw new Error('OffscreenCanvas is not available in this environment. ' +
|
|
24
|
-
'This converter is intended for browser use only. ' +
|
|
25
|
-
'Please use createNodeImageDataToBlobConverter() or createNodeCanvasImageDataToBlobConverter() for Node.js.');
|
|
26
|
-
}
|
|
27
|
-
const off = new OffscreenCanvas(imageData.width, imageData.height);
|
|
28
|
-
off.getContext('2d').putImageData(imageData, 0, 0);
|
|
29
|
-
return off.convertToBlob({ type: imageType });
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
16
|
/**
|
|
33
17
|
* Node.js implementation using Sharp
|
|
34
18
|
* This requires the 'sharp' package to be installed
|
|
@@ -42,9 +26,8 @@ function createBrowserImageDataToBlobConverter(imageType = 'image/webp') {
|
|
|
42
26
|
* const engine = new PdfiumEngine(pdfiumModule, logger, converter);
|
|
43
27
|
* ```
|
|
44
28
|
*/
|
|
45
|
-
function createNodeImageDataToBufferConverter(sharp
|
|
46
|
-
imageType = 'image/webp') {
|
|
47
|
-
return async (imageData) => {
|
|
29
|
+
function createNodeImageDataToBufferConverter(sharp) {
|
|
30
|
+
return async (imageData, imageType = 'image/webp') => {
|
|
48
31
|
const { width, height, data } = imageData;
|
|
49
32
|
// Convert ImageData to Sharp format
|
|
50
33
|
// ImageData uses RGBA format, Sharp expects the same
|
|
@@ -90,9 +73,8 @@ imageType = 'image/webp') {
|
|
|
90
73
|
* const engine = new PdfiumEngine(pdfiumModule, logger, converter);
|
|
91
74
|
* ```
|
|
92
75
|
*/
|
|
93
|
-
function createNodeCanvasImageDataToBlobConverter(createCanvas
|
|
94
|
-
imageType = 'image/webp') {
|
|
95
|
-
return async (imageData) => {
|
|
76
|
+
function createNodeCanvasImageDataToBlobConverter(createCanvas) {
|
|
77
|
+
return async (imageData, imageType = 'image/webp') => {
|
|
96
78
|
const { width, height } = imageData;
|
|
97
79
|
// Create a canvas and put the image data
|
|
98
80
|
const canvas = createCanvas(width, height);
|
|
@@ -129,8 +111,8 @@ imageType = 'image/webp') {
|
|
|
129
111
|
* });
|
|
130
112
|
* ```
|
|
131
113
|
*/
|
|
132
|
-
function createCustomImageDataToBlobConverter(processor
|
|
133
|
-
return async (imageData) => {
|
|
114
|
+
function createCustomImageDataToBlobConverter(processor) {
|
|
115
|
+
return async (imageData, imageType = 'image/webp') => {
|
|
134
116
|
const buffer = await processor(imageData);
|
|
135
117
|
return new Blob([buffer], { type: imageType });
|
|
136
118
|
};
|
|
@@ -142,10 +124,10 @@ function createCustomImageDataToBlobConverter(processor, imageType = 'image/webp
|
|
|
142
124
|
* @returns ImageDataToBlobConverter<Buffer>
|
|
143
125
|
*/
|
|
144
126
|
function createCustomImageDataToBufferConverter(processor) {
|
|
145
|
-
return async (imageData) => {
|
|
146
|
-
return await processor(imageData);
|
|
127
|
+
return async (imageData, imageType = 'image/webp') => {
|
|
128
|
+
return await processor(imageData, imageType);
|
|
147
129
|
};
|
|
148
130
|
}
|
|
149
131
|
|
|
150
|
-
export { browserImageDataToBlobConverter,
|
|
132
|
+
export { browserImageDataToBlobConverter, createCustomImageDataToBlobConverter, createCustomImageDataToBufferConverter, createNodeCanvasImageDataToBlobConverter, createNodeImageDataToBufferConverter };
|
|
151
133
|
//# sourceMappingURL=converters.js.map
|
package/dist/converters.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"converters.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> = (imageData: ImageData) => 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
|
|
1
|
+
{"version":3,"file":"converters.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":"AAWA;;;AAGG;AACU,MAAA,+BAA+B,GAAuB,CACjE,SAAoB,EACpB,SAAA,GAAkC,YAAY,KAC7B;;AAEjB,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oD,EAAA;AAEpD,IAAA,OAAO,OACL,SAAoB,EACpB,SAAkC,GAAA,YAAY,KAC7B;AACjB,QAAA,MAAM,MAAM,GAAG,MAAM,SAAS,CAAC,SAAS,CAAC,CAAC;AAC1C,QAAA,OAAO,IAAI,IAAI,CAAC,CAAC,MAAM,CAAC,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC,CAAC;AACjD,KAAC,CAAC;AACJ,CAAC;AAED;;;;;AAKG;AACG,SAAU,sCAAsC,CACpD,SAAqF,EAAA;AAErF,IAAA,OAAO,OACL,SAAoB,EACpB,SAAkC,GAAA,YAAY,KAC3B;AACnB,QAAA,OAAO,MAAM,SAAS,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC;AAC/C,KAAC,CAAC;AACJ;;;;"}
|
package/dist/index.cjs
CHANGED
|
@@ -267,7 +267,7 @@ exports.PdfiumErrorCode = void 0;
|
|
|
267
267
|
PdfiumErrorCode[PdfiumErrorCode["XFALoad"] = 7] = "XFALoad";
|
|
268
268
|
PdfiumErrorCode[PdfiumErrorCode["XFALayout"] = 8] = "XFALayout";
|
|
269
269
|
})(exports.PdfiumErrorCode || (exports.PdfiumErrorCode = {}));
|
|
270
|
-
const browserImageDataToBlobConverter = (pdfImageData) => {
|
|
270
|
+
const browserImageDataToBlobConverter = (pdfImageData, imageType = 'image/webp') => {
|
|
271
271
|
// Check if we're in a browser environment
|
|
272
272
|
if (typeof OffscreenCanvas === 'undefined') {
|
|
273
273
|
throw new Error('OffscreenCanvas is not available in this environment. ' +
|
|
@@ -277,7 +277,7 @@ const browserImageDataToBlobConverter = (pdfImageData) => {
|
|
|
277
277
|
const imageData = new ImageData(pdfImageData.data, pdfImageData.width, pdfImageData.height);
|
|
278
278
|
const off = new OffscreenCanvas(imageData.width, imageData.height);
|
|
279
279
|
off.getContext('2d').putImageData(imageData, 0, 0);
|
|
280
|
-
return off.convertToBlob({ type:
|
|
280
|
+
return off.convertToBlob({ type: imageType });
|
|
281
281
|
};
|
|
282
282
|
/**
|
|
283
283
|
* Pdf engine that based on pdfium wasm
|
|
@@ -783,7 +783,7 @@ class PdfiumEngine {
|
|
|
783
783
|
*
|
|
784
784
|
* @public
|
|
785
785
|
*/
|
|
786
|
-
renderPage(doc, page, scaleFactor = 1, rotation = models.Rotation.Degree0, dpr = 1, options = { withAnnotations: false }) {
|
|
786
|
+
renderPage(doc, page, scaleFactor = 1, rotation = models.Rotation.Degree0, dpr = 1, options = { withAnnotations: false }, imageType = 'image/webp') {
|
|
787
787
|
const task = new models.Task();
|
|
788
788
|
this.logger.debug(LOG_SOURCE$2, LOG_CATEGORY$2, 'renderPage', doc, page, scaleFactor, rotation, dpr, options);
|
|
789
789
|
this.logger.perf(LOG_SOURCE$2, LOG_CATEGORY$2, `RenderPage`, 'Begin', `${doc.id}-${page.index}`);
|
|
@@ -800,7 +800,7 @@ class PdfiumEngine {
|
|
|
800
800
|
size: page.size,
|
|
801
801
|
}, scaleFactor, rotation, dpr, options);
|
|
802
802
|
this.logger.perf(LOG_SOURCE$2, LOG_CATEGORY$2, `RenderPage`, 'End', `${doc.id}-${page.index}`);
|
|
803
|
-
this.imageDataConverter(imageData).then((blob) => task.resolve(blob));
|
|
803
|
+
this.imageDataConverter(imageData, imageType).then((blob) => task.resolve(blob));
|
|
804
804
|
return task;
|
|
805
805
|
}
|
|
806
806
|
/**
|
|
@@ -808,7 +808,7 @@ class PdfiumEngine {
|
|
|
808
808
|
*
|
|
809
809
|
* @public
|
|
810
810
|
*/
|
|
811
|
-
renderPageRect(doc, page, scaleFactor, rotation, dpr, rect, options) {
|
|
811
|
+
renderPageRect(doc, page, scaleFactor, rotation, dpr, rect, options, imageType = 'image/webp') {
|
|
812
812
|
const task = new models.Task();
|
|
813
813
|
this.logger.debug(LOG_SOURCE$2, LOG_CATEGORY$2, 'renderPageRect', doc, page, scaleFactor, rotation, dpr, rect, options);
|
|
814
814
|
this.logger.perf(LOG_SOURCE$2, LOG_CATEGORY$2, `RenderPageRect`, 'Begin', `${doc.id}-${page.index}`);
|
|
@@ -822,7 +822,7 @@ class PdfiumEngine {
|
|
|
822
822
|
}
|
|
823
823
|
const imageData = this.renderPageRectToImageData(ctx, page, rect, scaleFactor, rotation, dpr, options);
|
|
824
824
|
this.logger.perf(LOG_SOURCE$2, LOG_CATEGORY$2, `RenderPageRect`, 'End', `${doc.id}-${page.index}`);
|
|
825
|
-
this.imageDataConverter(imageData).then((blob) => task.resolve(blob));
|
|
825
|
+
this.imageDataConverter(imageData, imageType).then((blob) => task.resolve(blob));
|
|
826
826
|
return task;
|
|
827
827
|
}
|
|
828
828
|
/**
|
|
@@ -1364,6 +1364,62 @@ class PdfiumEngine {
|
|
|
1364
1364
|
this.logger.perf(LOG_SOURCE$2, LOG_CATEGORY$2, `ExtractText`, 'End', doc.id);
|
|
1365
1365
|
return models.PdfTaskHelper.resolve(text);
|
|
1366
1366
|
}
|
|
1367
|
+
/**
|
|
1368
|
+
* {@inheritDoc @embedpdf/models!PdfEngine.getTextSlices}
|
|
1369
|
+
*
|
|
1370
|
+
* @public
|
|
1371
|
+
*/
|
|
1372
|
+
getTextSlices(doc, slices) {
|
|
1373
|
+
this.logger.debug(LOG_SOURCE$2, LOG_CATEGORY$2, 'getTextSlices', doc, slices);
|
|
1374
|
+
this.logger.perf(LOG_SOURCE$2, LOG_CATEGORY$2, 'GetTextSlices', 'Begin', doc.id);
|
|
1375
|
+
/* ⚠︎ 1 — trivial case */
|
|
1376
|
+
if (slices.length === 0) {
|
|
1377
|
+
this.logger.perf(LOG_SOURCE$2, LOG_CATEGORY$2, 'GetTextSlices', 'End', doc.id);
|
|
1378
|
+
return models.PdfTaskHelper.resolve([]);
|
|
1379
|
+
}
|
|
1380
|
+
/* ⚠︎ 2 — document must be open */
|
|
1381
|
+
const ctx = this.cache.getContext(doc.id);
|
|
1382
|
+
if (!ctx) {
|
|
1383
|
+
this.logger.perf(LOG_SOURCE$2, LOG_CATEGORY$2, 'GetTextSlices', 'End', doc.id);
|
|
1384
|
+
return models.PdfTaskHelper.reject({
|
|
1385
|
+
code: models.PdfErrorCode.DocNotOpen,
|
|
1386
|
+
message: 'document does not open',
|
|
1387
|
+
});
|
|
1388
|
+
}
|
|
1389
|
+
try {
|
|
1390
|
+
/* keep caller order */
|
|
1391
|
+
const out = new Array(slices.length);
|
|
1392
|
+
/* group → open each page once */
|
|
1393
|
+
const byPage = new Map();
|
|
1394
|
+
slices.forEach((s, i) => {
|
|
1395
|
+
(byPage.get(s.pageIndex) ?? byPage.set(s.pageIndex, []).get(s.pageIndex)).push({
|
|
1396
|
+
slice: s,
|
|
1397
|
+
pos: i,
|
|
1398
|
+
});
|
|
1399
|
+
});
|
|
1400
|
+
for (const [pageIdx, list] of byPage) {
|
|
1401
|
+
const pageCtx = ctx.acquirePage(pageIdx);
|
|
1402
|
+
const textPagePtr = pageCtx.getTextPage();
|
|
1403
|
+
for (const { slice, pos } of list) {
|
|
1404
|
+
const bufPtr = this.malloc(2 * (slice.charCount + 1)); // UTF-16 + NIL
|
|
1405
|
+
this.pdfiumModule.FPDFText_GetText(textPagePtr, slice.charIndex, slice.charCount, bufPtr);
|
|
1406
|
+
out[pos] = models.stripPdfUnwantedMarkers(this.pdfiumModule.pdfium.UTF16ToString(bufPtr));
|
|
1407
|
+
this.free(bufPtr);
|
|
1408
|
+
}
|
|
1409
|
+
pageCtx.release();
|
|
1410
|
+
}
|
|
1411
|
+
this.logger.perf(LOG_SOURCE$2, LOG_CATEGORY$2, 'GetTextSlices', 'End', doc.id);
|
|
1412
|
+
return models.PdfTaskHelper.resolve(out);
|
|
1413
|
+
}
|
|
1414
|
+
catch (e) {
|
|
1415
|
+
this.logger.error(LOG_SOURCE$2, LOG_CATEGORY$2, 'getTextSlices error', e);
|
|
1416
|
+
this.logger.perf(LOG_SOURCE$2, LOG_CATEGORY$2, 'GetTextSlices', 'End', doc.id);
|
|
1417
|
+
return models.PdfTaskHelper.reject({
|
|
1418
|
+
code: models.PdfErrorCode.Unknown,
|
|
1419
|
+
message: String(e),
|
|
1420
|
+
});
|
|
1421
|
+
}
|
|
1422
|
+
}
|
|
1367
1423
|
/**
|
|
1368
1424
|
* {@inheritDoc @embedpdf/models!PdfEngine.merge}
|
|
1369
1425
|
*
|
|
@@ -1893,14 +1949,12 @@ class PdfiumEngine {
|
|
|
1893
1949
|
const runs = [];
|
|
1894
1950
|
let current = null;
|
|
1895
1951
|
let curObjPtr = null;
|
|
1952
|
+
let bounds = null;
|
|
1896
1953
|
/** ── main loop ──────────────────────────────────────────── */
|
|
1897
1954
|
for (let i = 0; i < glyphs.length; i++) {
|
|
1898
1955
|
const g = glyphs[i];
|
|
1899
1956
|
/* 1 — find the CPDF_TextObject this glyph belongs to */
|
|
1900
1957
|
const objPtr = this.pdfiumModule.FPDFText_GetTextObject(textPagePtr, i);
|
|
1901
|
-
if (g.isEmpty) {
|
|
1902
|
-
continue;
|
|
1903
|
-
}
|
|
1904
1958
|
/* 2 — start a new run when the text object changes */
|
|
1905
1959
|
if (objPtr !== curObjPtr) {
|
|
1906
1960
|
curObjPtr = objPtr;
|
|
@@ -1914,6 +1968,12 @@ class PdfiumEngine {
|
|
|
1914
1968
|
charStart: i,
|
|
1915
1969
|
glyphs: [],
|
|
1916
1970
|
};
|
|
1971
|
+
bounds = {
|
|
1972
|
+
minX: g.origin.x,
|
|
1973
|
+
minY: g.origin.y,
|
|
1974
|
+
maxX: g.origin.x + g.size.width,
|
|
1975
|
+
maxY: g.origin.y + g.size.height,
|
|
1976
|
+
};
|
|
1917
1977
|
runs.push(current);
|
|
1918
1978
|
}
|
|
1919
1979
|
/* 3 — append the slim glyph record */
|
|
@@ -1922,16 +1982,24 @@ class PdfiumEngine {
|
|
|
1922
1982
|
y: g.origin.y,
|
|
1923
1983
|
width: g.size.width,
|
|
1924
1984
|
height: g.size.height,
|
|
1925
|
-
flags: g.isSpace ? 1 : 0,
|
|
1985
|
+
flags: g.isEmpty ? 2 : g.isSpace ? 1 : 0,
|
|
1926
1986
|
});
|
|
1927
1987
|
/* 4 — expand the run's bounding rect */
|
|
1988
|
+
if (g.isEmpty) {
|
|
1989
|
+
continue;
|
|
1990
|
+
}
|
|
1928
1991
|
const right = g.origin.x + g.size.width;
|
|
1929
1992
|
const bottom = g.origin.y + g.size.height;
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
1993
|
+
// Update bounds
|
|
1994
|
+
bounds.minX = Math.min(bounds.minX, g.origin.x);
|
|
1995
|
+
bounds.minY = Math.min(bounds.minY, g.origin.y);
|
|
1996
|
+
bounds.maxX = Math.max(bounds.maxX, right);
|
|
1997
|
+
bounds.maxY = Math.max(bounds.maxY, bottom);
|
|
1998
|
+
// Calculate final rect from bounds
|
|
1999
|
+
current.rect.x = bounds.minX;
|
|
2000
|
+
current.rect.y = bounds.minY;
|
|
2001
|
+
current.rect.width = bounds.maxX - bounds.minX;
|
|
2002
|
+
current.rect.height = bounds.maxY - bounds.minY;
|
|
1935
2003
|
}
|
|
1936
2004
|
return runs;
|
|
1937
2005
|
}
|
|
@@ -4177,6 +4245,9 @@ class EngineRunner {
|
|
|
4177
4245
|
case 'extractText':
|
|
4178
4246
|
task = this.engine[name](...args);
|
|
4179
4247
|
break;
|
|
4248
|
+
case 'getTextSlices':
|
|
4249
|
+
task = this.engine[name](...args);
|
|
4250
|
+
break;
|
|
4180
4251
|
case 'getPageGlyphs':
|
|
4181
4252
|
task = this.engine[name](...args);
|
|
4182
4253
|
break;
|
|
@@ -4602,7 +4673,7 @@ class WebWorkerEngine {
|
|
|
4602
4673
|
*
|
|
4603
4674
|
* @public
|
|
4604
4675
|
*/
|
|
4605
|
-
renderPage(doc, page, scaleFactor, rotation, dpr, options) {
|
|
4676
|
+
renderPage(doc, page, scaleFactor, rotation, dpr, options, imageType = 'image/webp') {
|
|
4606
4677
|
this.logger.debug(LOG_SOURCE, LOG_CATEGORY, 'renderPage', doc, page, scaleFactor, rotation, dpr, options);
|
|
4607
4678
|
const requestId = this.generateRequestId(doc.id);
|
|
4608
4679
|
const task = new WorkerTask(this.worker, requestId);
|
|
@@ -4611,7 +4682,7 @@ class WebWorkerEngine {
|
|
|
4611
4682
|
type: 'ExecuteRequest',
|
|
4612
4683
|
data: {
|
|
4613
4684
|
name: 'renderPage',
|
|
4614
|
-
args: [doc, page, scaleFactor, rotation, dpr, options],
|
|
4685
|
+
args: [doc, page, scaleFactor, rotation, dpr, options, imageType],
|
|
4615
4686
|
},
|
|
4616
4687
|
};
|
|
4617
4688
|
this.proxy(task, request);
|
|
@@ -4622,7 +4693,7 @@ class WebWorkerEngine {
|
|
|
4622
4693
|
*
|
|
4623
4694
|
* @public
|
|
4624
4695
|
*/
|
|
4625
|
-
renderPageRect(doc, page, scaleFactor, rotation, dpr, rect, options) {
|
|
4696
|
+
renderPageRect(doc, page, scaleFactor, rotation, dpr, rect, options, imageType = 'image/webp') {
|
|
4626
4697
|
this.logger.debug(LOG_SOURCE, LOG_CATEGORY, 'renderPageRect', doc, page, scaleFactor, rotation, dpr, rect, options);
|
|
4627
4698
|
const requestId = this.generateRequestId(doc.id);
|
|
4628
4699
|
const task = new WorkerTask(this.worker, requestId);
|
|
@@ -4631,7 +4702,7 @@ class WebWorkerEngine {
|
|
|
4631
4702
|
type: 'ExecuteRequest',
|
|
4632
4703
|
data: {
|
|
4633
4704
|
name: 'renderPageRect',
|
|
4634
|
-
args: [doc, page, scaleFactor, rotation, dpr, rect, options],
|
|
4705
|
+
args: [doc, page, scaleFactor, rotation, dpr, rect, options, imageType],
|
|
4635
4706
|
},
|
|
4636
4707
|
};
|
|
4637
4708
|
this.proxy(task, request);
|
|
@@ -4937,6 +5008,26 @@ class WebWorkerEngine {
|
|
|
4937
5008
|
this.proxy(task, request);
|
|
4938
5009
|
return task;
|
|
4939
5010
|
}
|
|
5011
|
+
/**
|
|
5012
|
+
* {@inheritDoc @embedpdf/models!PdfEngine.getTextSlices}
|
|
5013
|
+
*
|
|
5014
|
+
* @public
|
|
5015
|
+
*/
|
|
5016
|
+
getTextSlices(doc, slices) {
|
|
5017
|
+
this.logger.debug(LOG_SOURCE, LOG_CATEGORY, 'getTextSlices', doc, slices);
|
|
5018
|
+
const requestId = this.generateRequestId(doc.id);
|
|
5019
|
+
const task = new WorkerTask(this.worker, requestId);
|
|
5020
|
+
const request = {
|
|
5021
|
+
id: requestId,
|
|
5022
|
+
type: 'ExecuteRequest',
|
|
5023
|
+
data: {
|
|
5024
|
+
name: 'getTextSlices',
|
|
5025
|
+
args: [doc, slices],
|
|
5026
|
+
},
|
|
5027
|
+
};
|
|
5028
|
+
this.proxy(task, request);
|
|
5029
|
+
return task;
|
|
5030
|
+
}
|
|
4940
5031
|
/**
|
|
4941
5032
|
* {@inheritDoc @embedpdf/models!PdfEngine.getPageGlyphs}
|
|
4942
5033
|
*
|
|
@@ -5301,6 +5392,9 @@ function createMockPdfEngine(partialEngine) {
|
|
|
5301
5392
|
extractText: (pdf, pageIndexes) => {
|
|
5302
5393
|
return models.PdfTaskHelper.resolve('');
|
|
5303
5394
|
},
|
|
5395
|
+
getTextSlices: (doc, slices) => {
|
|
5396
|
+
return models.PdfTaskHelper.resolve([]);
|
|
5397
|
+
},
|
|
5304
5398
|
getPageGlyphs: (doc, page) => {
|
|
5305
5399
|
return models.PdfTaskHelper.resolve([]);
|
|
5306
5400
|
},
|