@pdfme/converter 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -0
- package/dist/cjs/__tests__/index.test.js +189 -0
- package/dist/cjs/__tests__/index.test.js.map +1 -0
- package/dist/cjs/src/img2pdf.js +91 -0
- package/dist/cjs/src/img2pdf.js.map +1 -0
- package/dist/cjs/src/index.browser.js +80 -0
- package/dist/cjs/src/index.browser.js.map +1 -0
- package/dist/cjs/src/index.node.js +67 -0
- package/dist/cjs/src/index.node.js.map +1 -0
- package/dist/cjs/src/pdf2img.js +35 -0
- package/dist/cjs/src/pdf2img.js.map +1 -0
- package/dist/cjs/src/pdf2size.js +17 -0
- package/dist/cjs/src/pdf2size.js.map +1 -0
- package/dist/esm/__tests__/index.test.js +187 -0
- package/dist/esm/__tests__/index.test.js.map +1 -0
- package/dist/esm/src/img2pdf.js +88 -0
- package/dist/esm/src/img2pdf.js.map +1 -0
- package/dist/esm/src/index.browser.js +38 -0
- package/dist/esm/src/index.browser.js.map +1 -0
- package/dist/esm/src/index.node.js +25 -0
- package/dist/esm/src/index.node.js.map +1 -0
- package/dist/esm/src/pdf2img.js +32 -0
- package/dist/esm/src/pdf2img.js.map +1 -0
- package/dist/esm/src/pdf2size.js +14 -0
- package/dist/esm/src/pdf2size.js.map +1 -0
- package/dist/types/__tests__/index.test.d.ts +1 -0
- package/dist/types/src/img2pdf.d.ts +12 -0
- package/dist/types/src/index.browser.d.ts +8 -0
- package/dist/types/src/index.node.d.ts +8 -0
- package/dist/types/src/pdf2img.d.ts +17 -0
- package/dist/types/src/pdf2size.d.ts +10 -0
- package/eslint.config.mjs +22 -0
- package/package.json +80 -0
- package/src/img2pdf.ts +110 -0
- package/src/index.browser.ts +51 -0
- package/src/index.node.ts +33 -0
- package/src/pdf2img.ts +64 -0
- package/src/pdf2size.ts +33 -0
- package/src/types.d.ts +1 -0
- package/tsconfig.cjs.json +10 -0
- package/tsconfig.esm.json +11 -0
- package/tsconfig.json +6 -0
package/src/img2pdf.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { PDFDocument } from '@pdfme/pdf-lib';
|
|
2
|
+
import { mm2pt } from '@pdfme/common';
|
|
3
|
+
import type { ImageType } from './types.js';
|
|
4
|
+
|
|
5
|
+
interface Img2PdfOptions {
|
|
6
|
+
scale?: number;
|
|
7
|
+
imageType?: ImageType;
|
|
8
|
+
size?: { height: number; width: number }; // in millimeters
|
|
9
|
+
margin?: [number, number, number, number]; // in millimeters [top, right, bottom, left]
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function detectImageType(buffer: ArrayBuffer): 'jpeg' | 'png' | 'unknown' {
|
|
13
|
+
const bytes = new Uint8Array(buffer);
|
|
14
|
+
|
|
15
|
+
if (bytes.length >= 2 && bytes[0] === 0xff && bytes[1] === 0xd8) {
|
|
16
|
+
return 'jpeg';
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (
|
|
20
|
+
bytes.length >= 8 &&
|
|
21
|
+
bytes[0] === 0x89 &&
|
|
22
|
+
bytes[1] === 0x50 &&
|
|
23
|
+
bytes[2] === 0x4e &&
|
|
24
|
+
bytes[3] === 0x47 &&
|
|
25
|
+
bytes[4] === 0x0d &&
|
|
26
|
+
bytes[5] === 0x0a &&
|
|
27
|
+
bytes[6] === 0x1a &&
|
|
28
|
+
bytes[7] === 0x0a
|
|
29
|
+
) {
|
|
30
|
+
return 'png';
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return 'unknown';
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export async function img2pdf(
|
|
37
|
+
imgs: ArrayBuffer[],
|
|
38
|
+
options: Img2PdfOptions = {},
|
|
39
|
+
): Promise<ArrayBuffer> {
|
|
40
|
+
try {
|
|
41
|
+
const { scale = 1, size, margin = [0, 0, 0, 0] } = options;
|
|
42
|
+
|
|
43
|
+
if (!Array.isArray(imgs) || imgs.length === 0) {
|
|
44
|
+
throw new Error('Input must be a non-empty array of image buffers');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const doc = await PDFDocument.create();
|
|
48
|
+
for (const img of imgs) {
|
|
49
|
+
try {
|
|
50
|
+
let image;
|
|
51
|
+
const type = detectImageType(img);
|
|
52
|
+
|
|
53
|
+
if (type === 'jpeg') {
|
|
54
|
+
image = await doc.embedJpg(img);
|
|
55
|
+
} else if (type === 'png') {
|
|
56
|
+
image = await doc.embedPng(img);
|
|
57
|
+
} else {
|
|
58
|
+
try {
|
|
59
|
+
image = await doc.embedJpg(img);
|
|
60
|
+
} catch {
|
|
61
|
+
image = await doc.embedPng(img);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const page = doc.addPage();
|
|
66
|
+
const { width: imgWidth, height: imgHeight } = image.scale(scale);
|
|
67
|
+
|
|
68
|
+
// Set page size based on size option or image dimensions
|
|
69
|
+
const pageWidth = size ? mm2pt(size.width) : imgWidth;
|
|
70
|
+
const pageHeight = size ? mm2pt(size.height) : imgHeight;
|
|
71
|
+
page.setSize(pageWidth, pageHeight);
|
|
72
|
+
|
|
73
|
+
// Convert margins from mm to points
|
|
74
|
+
const [topMargin, rightMargin, bottomMargin, leftMargin] = margin.map(mm2pt);
|
|
75
|
+
|
|
76
|
+
// Calculate available space for the image after applying margins
|
|
77
|
+
const availableWidth = pageWidth - leftMargin - rightMargin;
|
|
78
|
+
const availableHeight = pageHeight - topMargin - bottomMargin;
|
|
79
|
+
|
|
80
|
+
// Calculate scaling to fit image within available space while maintaining aspect ratio
|
|
81
|
+
const widthRatio = availableWidth / imgWidth;
|
|
82
|
+
const heightRatio = availableHeight / imgHeight;
|
|
83
|
+
const ratio = Math.min(widthRatio, heightRatio, 1); // Don't upscale images
|
|
84
|
+
|
|
85
|
+
// Calculate final image dimensions and position
|
|
86
|
+
const finalWidth = imgWidth * ratio;
|
|
87
|
+
const finalHeight = imgHeight * ratio;
|
|
88
|
+
const x = leftMargin + (availableWidth - finalWidth) / 2; // Center horizontally
|
|
89
|
+
const y = bottomMargin + (availableHeight - finalHeight) / 2; // Center vertically
|
|
90
|
+
|
|
91
|
+
page.drawImage(image, {
|
|
92
|
+
x,
|
|
93
|
+
y,
|
|
94
|
+
width: finalWidth,
|
|
95
|
+
height: finalHeight,
|
|
96
|
+
});
|
|
97
|
+
} catch (error) {
|
|
98
|
+
throw new Error(`Failed to process image: ${(error as Error).message}`);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
const pdfUint8Array = await doc.save();
|
|
102
|
+
// Create a new ArrayBuffer from the Uint8Array to ensure we return only ArrayBuffer
|
|
103
|
+
const buffer = new ArrayBuffer(pdfUint8Array.byteLength);
|
|
104
|
+
const view = new Uint8Array(buffer);
|
|
105
|
+
view.set(pdfUint8Array);
|
|
106
|
+
return buffer;
|
|
107
|
+
} catch (error) {
|
|
108
|
+
throw new Error(`[@pdfme/converter] img2pdf failed: ${(error as Error).message}`);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import * as pdfjsLib from 'pdfjs-dist';
|
|
2
|
+
// @ts-expect-error - PDFJSWorker import is not properly typed but required for functionality
|
|
3
|
+
import PDFJSWorker from 'pdfjs-dist/build/pdf.worker.entry.js';
|
|
4
|
+
import { pdf2img as _pdf2img, Pdf2ImgOptions } from './pdf2img.js';
|
|
5
|
+
import { pdf2size as _pdf2size, Pdf2SizeOptions } from './pdf2size.js';
|
|
6
|
+
|
|
7
|
+
pdfjsLib.GlobalWorkerOptions.workerSrc = PDFJSWorker as unknown as string;
|
|
8
|
+
|
|
9
|
+
function dataURLToArrayBuffer(dataURL: string): ArrayBuffer {
|
|
10
|
+
// Split out the actual base64 string from the data URL scheme
|
|
11
|
+
const base64String = dataURL.split(',')[1];
|
|
12
|
+
|
|
13
|
+
// Decode the Base64 string to get the binary data
|
|
14
|
+
const byteString = atob(base64String);
|
|
15
|
+
|
|
16
|
+
// Create a typed array from the binary string
|
|
17
|
+
const arrayBuffer = new ArrayBuffer(byteString.length);
|
|
18
|
+
const uintArray = new Uint8Array(arrayBuffer);
|
|
19
|
+
|
|
20
|
+
for (let i = 0; i < byteString.length; i++) {
|
|
21
|
+
uintArray[i] = byteString.charCodeAt(i);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return arrayBuffer;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export const pdf2img = async (
|
|
28
|
+
pdf: ArrayBuffer | Uint8Array,
|
|
29
|
+
options: Pdf2ImgOptions = {},
|
|
30
|
+
): Promise<ArrayBuffer[]> =>
|
|
31
|
+
_pdf2img(pdf, options, {
|
|
32
|
+
getDocument: (pdf) => pdfjsLib.getDocument({ data: pdf, isEvalSupported: false }).promise,
|
|
33
|
+
createCanvas: (width, height) => {
|
|
34
|
+
const canvas = document.createElement('canvas');
|
|
35
|
+
canvas.width = width;
|
|
36
|
+
canvas.height = height;
|
|
37
|
+
return canvas;
|
|
38
|
+
},
|
|
39
|
+
canvasToArrayBuffer: (canvas, imageType) => {
|
|
40
|
+
// Using type assertion to handle the canvas method
|
|
41
|
+
const dataUrl = (canvas as HTMLCanvasElement).toDataURL(`image/${imageType}`);
|
|
42
|
+
return dataURLToArrayBuffer(dataUrl);
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
export const pdf2size = async (pdf: ArrayBuffer | Uint8Array, options: Pdf2SizeOptions = {}) =>
|
|
47
|
+
_pdf2size(pdf, options, {
|
|
48
|
+
getDocument: (pdf) => pdfjsLib.getDocument({ data: pdf, isEvalSupported: false }).promise,
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
export { img2pdf } from './img2pdf.js';
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import * as pdfjsLib from 'pdfjs-dist/legacy/build/pdf';
|
|
2
|
+
// @ts-expect-error - PDFJSWorker import is not properly typed but required for functionality
|
|
3
|
+
import PDFJSWorker from 'pdfjs-dist/legacy/build/pdf.worker.js';
|
|
4
|
+
import { createCanvas } from 'canvas';
|
|
5
|
+
import { pdf2img as _pdf2img, Pdf2ImgOptions } from './pdf2img.js';
|
|
6
|
+
import { pdf2size as _pdf2size, Pdf2SizeOptions } from './pdf2size.js';
|
|
7
|
+
|
|
8
|
+
pdfjsLib.GlobalWorkerOptions.workerSrc = PDFJSWorker as unknown as string;
|
|
9
|
+
|
|
10
|
+
export const pdf2img = async (
|
|
11
|
+
pdf: ArrayBuffer | Uint8Array,
|
|
12
|
+
options: Pdf2ImgOptions = {},
|
|
13
|
+
): Promise<ArrayBuffer[]> =>
|
|
14
|
+
_pdf2img(pdf, options, {
|
|
15
|
+
getDocument: (pdf) => pdfjsLib.getDocument({ data: pdf, isEvalSupported: false }).promise,
|
|
16
|
+
createCanvas: (width, height) => createCanvas(width, height) as unknown as HTMLCanvasElement,
|
|
17
|
+
canvasToArrayBuffer: (canvas) => {
|
|
18
|
+
// Using a more specific type for the canvas from the 'canvas' package
|
|
19
|
+
const nodeCanvas = canvas as unknown as import('canvas').Canvas;
|
|
20
|
+
// Get buffer from the canvas - using the synchronous version without parameters
|
|
21
|
+
// This will use the default PNG format
|
|
22
|
+
const buffer = nodeCanvas.toBuffer();
|
|
23
|
+
// Convert to ArrayBuffer
|
|
24
|
+
return new Uint8Array(buffer).buffer;
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
export const pdf2size = async (pdf: ArrayBuffer | Uint8Array, options: Pdf2SizeOptions = {}) =>
|
|
29
|
+
_pdf2size(pdf, options, {
|
|
30
|
+
getDocument: (pdf) => pdfjsLib.getDocument({ data: pdf, isEvalSupported: false }).promise,
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
export { img2pdf } from './img2pdf.js';
|
package/src/pdf2img.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { PDFDocumentProxy } from 'pdfjs-dist';
|
|
2
|
+
import type { ImageType } from './types.js';
|
|
3
|
+
|
|
4
|
+
interface Environment {
|
|
5
|
+
getDocument: (pdf: ArrayBuffer | Uint8Array) => Promise<PDFDocumentProxy>;
|
|
6
|
+
createCanvas: (width: number, height: number) => HTMLCanvasElement | OffscreenCanvas;
|
|
7
|
+
canvasToArrayBuffer: (
|
|
8
|
+
canvas: HTMLCanvasElement | OffscreenCanvas,
|
|
9
|
+
imageType: ImageType,
|
|
10
|
+
) => ArrayBuffer;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface Pdf2ImgOptions {
|
|
14
|
+
scale?: number;
|
|
15
|
+
imageType?: ImageType;
|
|
16
|
+
range?: {
|
|
17
|
+
start?: number;
|
|
18
|
+
end?: number;
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export async function pdf2img(
|
|
23
|
+
pdf: ArrayBuffer | Uint8Array,
|
|
24
|
+
options: Pdf2ImgOptions = {},
|
|
25
|
+
env: Environment,
|
|
26
|
+
): Promise<ArrayBuffer[]> {
|
|
27
|
+
try {
|
|
28
|
+
const { scale = 1, imageType = 'jpeg', range = {} } = options;
|
|
29
|
+
const { start = 0, end = Infinity } = range;
|
|
30
|
+
|
|
31
|
+
const { getDocument, createCanvas, canvasToArrayBuffer } = env;
|
|
32
|
+
|
|
33
|
+
const pdfDoc = await getDocument(pdf);
|
|
34
|
+
const numPages = pdfDoc.numPages;
|
|
35
|
+
|
|
36
|
+
const startPage = Math.max(start + 1, 1);
|
|
37
|
+
const endPage = Math.min(end + 1, numPages);
|
|
38
|
+
|
|
39
|
+
const results: ArrayBuffer[] = [];
|
|
40
|
+
|
|
41
|
+
for (let pageNum = startPage; pageNum <= endPage; pageNum++) {
|
|
42
|
+
const page = await pdfDoc.getPage(pageNum);
|
|
43
|
+
const viewport = page.getViewport({ scale });
|
|
44
|
+
|
|
45
|
+
const canvas = createCanvas(viewport.width, viewport.height);
|
|
46
|
+
if (!canvas) {
|
|
47
|
+
throw new Error('Failed to create canvas');
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const context = canvas.getContext('2d') as CanvasRenderingContext2D;
|
|
51
|
+
if (!context) {
|
|
52
|
+
throw new Error('Failed to get canvas context');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
await page.render({ canvasContext: context, viewport }).promise;
|
|
56
|
+
const arrayBuffer = canvasToArrayBuffer(canvas, imageType);
|
|
57
|
+
results.push(arrayBuffer);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return results;
|
|
61
|
+
} catch (error) {
|
|
62
|
+
throw new Error(`[@pdfme/converter] pdf2img failed: ${(error as Error).message}`);
|
|
63
|
+
}
|
|
64
|
+
}
|
package/src/pdf2size.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { PDFDocumentProxy } from 'pdfjs-dist';
|
|
2
|
+
import { Size, pt2mm } from '@pdfme/common';
|
|
3
|
+
|
|
4
|
+
interface Environment {
|
|
5
|
+
getDocument: (pdf: ArrayBuffer | Uint8Array) => Promise<PDFDocumentProxy>;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface Pdf2SizeOptions {
|
|
9
|
+
scale?: number;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export async function pdf2size(
|
|
13
|
+
pdf: ArrayBuffer | Uint8Array,
|
|
14
|
+
options: Pdf2SizeOptions = {},
|
|
15
|
+
env: Environment,
|
|
16
|
+
): Promise<Size[]> {
|
|
17
|
+
const { scale = 1 } = options;
|
|
18
|
+
const { getDocument } = env;
|
|
19
|
+
|
|
20
|
+
const pdfDoc = await getDocument(pdf);
|
|
21
|
+
|
|
22
|
+
const promises = Promise.all(
|
|
23
|
+
new Array(pdfDoc.numPages).fill('').map(async (_, i) => {
|
|
24
|
+
return await pdfDoc.getPage(i + 1).then((page) => {
|
|
25
|
+
const { height, width } = page.getViewport({ scale, rotation: 0 });
|
|
26
|
+
|
|
27
|
+
return { height: pt2mm(height), width: pt2mm(width) };
|
|
28
|
+
});
|
|
29
|
+
}),
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
return promises;
|
|
33
|
+
}
|
package/src/types.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type ImageType = 'jpeg' | 'png';
|