@pdfme/converter 5.4.0-dev.6 → 5.4.0-dev.65
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/cjs/src/img2pdf.js +6 -6
- package/dist/esm/src/img2pdf.js +6 -6
- package/package.json +1 -1
- package/src/img2pdf.ts +13 -13
package/dist/cjs/src/img2pdf.js
CHANGED
@@ -5,18 +5,18 @@ const pdf_lib_1 = require("@pdfme/pdf-lib");
|
|
5
5
|
const common_1 = require("@pdfme/common");
|
6
6
|
function detectImageType(buffer) {
|
7
7
|
const bytes = new Uint8Array(buffer);
|
8
|
-
if (bytes.length >= 2 && bytes[0] ===
|
8
|
+
if (bytes.length >= 2 && bytes[0] === 0xff && bytes[1] === 0xd8) {
|
9
9
|
return 'jpeg';
|
10
10
|
}
|
11
11
|
if (bytes.length >= 8 &&
|
12
12
|
bytes[0] === 0x89 &&
|
13
13
|
bytes[1] === 0x50 &&
|
14
|
-
bytes[2] ===
|
14
|
+
bytes[2] === 0x4e &&
|
15
15
|
bytes[3] === 0x47 &&
|
16
|
-
bytes[4] ===
|
17
|
-
bytes[5] ===
|
18
|
-
bytes[6] ===
|
19
|
-
bytes[7] ===
|
16
|
+
bytes[4] === 0x0d &&
|
17
|
+
bytes[5] === 0x0a &&
|
18
|
+
bytes[6] === 0x1a &&
|
19
|
+
bytes[7] === 0x0a) {
|
20
20
|
return 'png';
|
21
21
|
}
|
22
22
|
return 'unknown';
|
package/dist/esm/src/img2pdf.js
CHANGED
@@ -2,18 +2,18 @@ import { PDFDocument } from '@pdfme/pdf-lib';
|
|
2
2
|
import { mm2pt } from '@pdfme/common';
|
3
3
|
function detectImageType(buffer) {
|
4
4
|
const bytes = new Uint8Array(buffer);
|
5
|
-
if (bytes.length >= 2 && bytes[0] ===
|
5
|
+
if (bytes.length >= 2 && bytes[0] === 0xff && bytes[1] === 0xd8) {
|
6
6
|
return 'jpeg';
|
7
7
|
}
|
8
8
|
if (bytes.length >= 8 &&
|
9
9
|
bytes[0] === 0x89 &&
|
10
10
|
bytes[1] === 0x50 &&
|
11
|
-
bytes[2] ===
|
11
|
+
bytes[2] === 0x4e &&
|
12
12
|
bytes[3] === 0x47 &&
|
13
|
-
bytes[4] ===
|
14
|
-
bytes[5] ===
|
15
|
-
bytes[6] ===
|
16
|
-
bytes[7] ===
|
13
|
+
bytes[4] === 0x0d &&
|
14
|
+
bytes[5] === 0x0a &&
|
15
|
+
bytes[6] === 0x1a &&
|
16
|
+
bytes[7] === 0x0a) {
|
17
17
|
return 'png';
|
18
18
|
}
|
19
19
|
return 'unknown';
|
package/package.json
CHANGED
package/src/img2pdf.ts
CHANGED
@@ -5,14 +5,14 @@ import type { ImageType } from './types.js';
|
|
5
5
|
interface Img2PdfOptions {
|
6
6
|
scale?: number;
|
7
7
|
imageType?: ImageType;
|
8
|
-
size?: { height: number
|
8
|
+
size?: { height: number; width: number }; // in millimeters
|
9
9
|
margin?: [number, number, number, number]; // in millimeters [top, right, bottom, left]
|
10
10
|
}
|
11
11
|
|
12
12
|
function detectImageType(buffer: ArrayBuffer): 'jpeg' | 'png' | 'unknown' {
|
13
13
|
const bytes = new Uint8Array(buffer);
|
14
14
|
|
15
|
-
if (bytes.length >= 2 && bytes[0] ===
|
15
|
+
if (bytes.length >= 2 && bytes[0] === 0xff && bytes[1] === 0xd8) {
|
16
16
|
return 'jpeg';
|
17
17
|
}
|
18
18
|
|
@@ -20,12 +20,12 @@ function detectImageType(buffer: ArrayBuffer): 'jpeg' | 'png' | 'unknown' {
|
|
20
20
|
bytes.length >= 8 &&
|
21
21
|
bytes[0] === 0x89 &&
|
22
22
|
bytes[1] === 0x50 &&
|
23
|
-
bytes[2] ===
|
23
|
+
bytes[2] === 0x4e &&
|
24
24
|
bytes[3] === 0x47 &&
|
25
|
-
bytes[4] ===
|
26
|
-
bytes[5] ===
|
27
|
-
bytes[6] ===
|
28
|
-
bytes[7] ===
|
25
|
+
bytes[4] === 0x0d &&
|
26
|
+
bytes[5] === 0x0a &&
|
27
|
+
bytes[6] === 0x1a &&
|
28
|
+
bytes[7] === 0x0a
|
29
29
|
) {
|
30
30
|
return 'png';
|
31
31
|
}
|
@@ -64,30 +64,30 @@ export async function img2pdf(
|
|
64
64
|
|
65
65
|
const page = doc.addPage();
|
66
66
|
const { width: imgWidth, height: imgHeight } = image.scale(scale);
|
67
|
-
|
67
|
+
|
68
68
|
// Set page size based on size option or image dimensions
|
69
69
|
const pageWidth = size ? mm2pt(size.width) : imgWidth;
|
70
70
|
const pageHeight = size ? mm2pt(size.height) : imgHeight;
|
71
71
|
page.setSize(pageWidth, pageHeight);
|
72
|
-
|
72
|
+
|
73
73
|
// Convert margins from mm to points
|
74
74
|
const [topMargin, rightMargin, bottomMargin, leftMargin] = margin.map(mm2pt);
|
75
|
-
|
75
|
+
|
76
76
|
// Calculate available space for the image after applying margins
|
77
77
|
const availableWidth = pageWidth - leftMargin - rightMargin;
|
78
78
|
const availableHeight = pageHeight - topMargin - bottomMargin;
|
79
|
-
|
79
|
+
|
80
80
|
// Calculate scaling to fit image within available space while maintaining aspect ratio
|
81
81
|
const widthRatio = availableWidth / imgWidth;
|
82
82
|
const heightRatio = availableHeight / imgHeight;
|
83
83
|
const ratio = Math.min(widthRatio, heightRatio, 1); // Don't upscale images
|
84
|
-
|
84
|
+
|
85
85
|
// Calculate final image dimensions and position
|
86
86
|
const finalWidth = imgWidth * ratio;
|
87
87
|
const finalHeight = imgHeight * ratio;
|
88
88
|
const x = leftMargin + (availableWidth - finalWidth) / 2; // Center horizontally
|
89
89
|
const y = bottomMargin + (availableHeight - finalHeight) / 2; // Center vertically
|
90
|
-
|
90
|
+
|
91
91
|
page.drawImage(image, {
|
92
92
|
x,
|
93
93
|
y,
|