|
1 | | -import { PdfImage } from '@embedpdf/models'; |
2 | | -import { toArrayBuffer } from '../utils'; |
3 | | -import { ImageDataConverter, ImageConversionTypes, LazyImageData } from './types'; |
| 1 | +// Types |
| 2 | +export * from './types'; |
4 | 3 |
|
5 | | -/** |
6 | | - * Node.js implementation using Sharp |
7 | | - * This requires the 'sharp' package to be installed |
8 | | - * |
9 | | - * @example |
10 | | - * ```typescript |
11 | | - * import sharp from 'sharp'; |
12 | | - * import { createNodeImageDataToBufferConverter } from '@embedpdf/engines/pdfium/image-converters'; |
13 | | - * |
14 | | - * const imageDataConverter = createNodeImageDataToBufferConverter(sharp); |
15 | | - * const engine = new PdfiumEngine(pdfiumModule, { logger, imageDataConverter }); |
16 | | - * ``` |
17 | | - */ |
18 | | -export function createNodeImageDataToBufferConverter( |
19 | | - sharp: any, // Using 'any' to avoid requiring sharp as a dependency |
20 | | -): ImageDataConverter<Buffer> { |
21 | | - return async ( |
22 | | - getImageData: LazyImageData, |
23 | | - imageType: ImageConversionTypes = 'image/webp', |
24 | | - imageQuality?: number, |
25 | | - ): Promise<Buffer> => { |
26 | | - const imageData = getImageData(); |
27 | | - const { width, height, data } = imageData; |
| 4 | +// Browser converters (safe for all environments) |
| 5 | +export * from './browser'; |
28 | 6 |
|
29 | | - // Convert ImageData to Sharp format |
30 | | - // ImageData uses RGBA format, Sharp expects the same |
31 | | - let sharpInstance = sharp(Buffer.from(data), { |
32 | | - raw: { |
33 | | - width, |
34 | | - height, |
35 | | - channels: 4, // RGBA |
36 | | - }, |
37 | | - }); |
38 | | - |
39 | | - // Apply the appropriate format conversion based on imageType |
40 | | - let buffer: Buffer; |
41 | | - switch (imageType) { |
42 | | - case 'image/webp': |
43 | | - buffer = await sharpInstance |
44 | | - .webp({ |
45 | | - quality: imageQuality, |
46 | | - }) |
47 | | - .toBuffer(); |
48 | | - break; |
49 | | - case 'image/png': |
50 | | - buffer = await sharpInstance.png().toBuffer(); |
51 | | - break; |
52 | | - case 'image/jpeg': |
53 | | - // JPEG doesn't support transparency, so we need to composite onto a white background |
54 | | - buffer = await sharpInstance |
55 | | - .flatten({ background: { r: 255, g: 255, b: 255 } }) // Remove alpha channel with white background |
56 | | - .jpeg({ |
57 | | - quality: imageQuality, |
58 | | - }) |
59 | | - .toBuffer(); |
60 | | - break; |
61 | | - default: |
62 | | - throw new Error(`Unsupported image type: ${imageType}`); |
63 | | - } |
64 | | - |
65 | | - return buffer; |
66 | | - }; |
67 | | -} |
68 | | - |
69 | | -/** |
70 | | - * Alternative Node.js implementation using canvas (node-canvas) |
71 | | - * This requires the 'canvas' package to be installed |
72 | | - * |
73 | | - * @example |
74 | | - * ```typescript |
75 | | - * import { createCanvas } from 'canvas'; |
76 | | - * import { createNodeCanvasImageDataToBlobConverter } from '@embedpdf/engines/pdfium/image-converters'; |
77 | | - * |
78 | | - * const imageDataConverter = createNodeCanvasImageDataToBlobConverter(createCanvas); |
79 | | - * const engine = new PdfiumEngine(pdfiumModule, { logger, imageDataConverter }); |
80 | | - * ``` |
81 | | - */ |
82 | | -export function createNodeCanvasImageDataToBlobConverter( |
83 | | - createCanvas: any, // Using 'any' to avoid requiring canvas as a dependency |
84 | | -): ImageDataConverter<Buffer> { |
85 | | - return async ( |
86 | | - getImageData: LazyImageData, |
87 | | - imageType: ImageConversionTypes = 'image/webp', |
88 | | - _imageQuality?: number, |
89 | | - ): Promise<Buffer> => { |
90 | | - const imageData = getImageData(); |
91 | | - const { width, height } = imageData; |
92 | | - |
93 | | - // Create a canvas and put the image data |
94 | | - const canvas = createCanvas(width, height); |
95 | | - const ctx = canvas.getContext('2d'); |
96 | | - ctx.putImageData(imageData, 0, 0); |
97 | | - |
98 | | - // Convert to buffer and create blob based on the requested type |
99 | | - let buffer: Buffer; |
100 | | - switch (imageType) { |
101 | | - case 'image/webp': |
102 | | - buffer = canvas.toBuffer('image/webp'); |
103 | | - break; |
104 | | - case 'image/png': |
105 | | - buffer = canvas.toBuffer('image/png'); |
106 | | - break; |
107 | | - case 'image/jpeg': |
108 | | - buffer = canvas.toBuffer('image/jpeg'); |
109 | | - break; |
110 | | - default: |
111 | | - throw new Error(`Unsupported image type: ${imageType}`); |
112 | | - } |
113 | | - |
114 | | - return buffer; |
115 | | - }; |
116 | | -} |
117 | | - |
118 | | -/** |
119 | | - * Generic Node.js implementation that works with any image processing library |
120 | | - * that can handle raw RGBA data |
121 | | - * |
122 | | - * @example |
123 | | - * ```typescript |
124 | | - * const converter = createCustomImageDataToBlobConverter(async (imageData) => { |
125 | | - * // Your custom image processing logic here |
126 | | - * // Return a Buffer that will be wrapped in a Blob |
127 | | - * return processImageWithYourLibrary(imageData); |
128 | | - * }); |
129 | | - * ``` |
130 | | - */ |
131 | | -export function createCustomImageDataToBlobConverter( |
132 | | - processor: ( |
133 | | - imageData: PdfImage, |
134 | | - imageType?: ImageConversionTypes, |
135 | | - imageQuality?: number, |
136 | | - ) => Promise<Buffer>, |
137 | | -): ImageDataConverter { |
138 | | - return async ( |
139 | | - getImageData: LazyImageData, |
140 | | - imageType: ImageConversionTypes = 'image/webp', |
141 | | - imageQuality?: number, |
142 | | - ) => { |
143 | | - const imageData = getImageData(); |
144 | | - const bytes = await processor(imageData, imageType, imageQuality); |
145 | | - return new Blob([toArrayBuffer(bytes)], { type: imageType }); |
146 | | - }; |
147 | | -} |
148 | | - |
149 | | -/** |
150 | | - * Create a custom converter that returns a Buffer |
151 | | - * @param processor - function to process the image data |
152 | | - * @param imageType - image type |
153 | | - * @returns ImageDataToBlobConverter<Buffer> |
154 | | - */ |
155 | | -export function createCustomImageDataToBufferConverter( |
156 | | - processor: ( |
157 | | - imageData: PdfImage, |
158 | | - imageType: ImageConversionTypes, |
159 | | - imageQuality?: number, |
160 | | - ) => Promise<Buffer>, |
161 | | -): ImageDataConverter<Buffer> { |
162 | | - return async ( |
163 | | - getImageData: LazyImageData, |
164 | | - imageType: ImageConversionTypes = 'image/webp', |
165 | | - imageQuality?: number, |
166 | | - ): Promise<Buffer> => { |
167 | | - const imageData = getImageData(); |
168 | | - return await processor(imageData, imageType, imageQuality); |
169 | | - }; |
170 | | -} |
| 7 | +// Node.js converters (only for Node.js environments) |
| 8 | +export * from './node'; |
0 commit comments