@iqx-limited/quick-pdf 1.0.2 → 1.0.4
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 +2 -1
- package/dist/canvas.d.ts +9 -2
- package/dist/canvas.js +19 -0
- package/dist/canvas.js.map +1 -1
- package/dist/modules/pdf2img.d.ts +2 -9
- package/dist/modules/pdf2img.js +17 -12
- package/dist/modules/pdf2img.js.map +1 -1
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -28,6 +28,7 @@ html2pdf(input: string | URL)
|
|
|
28
28
|
| Parameter | Type | Description | Data that can be passed |
|
|
29
29
|
|-----------|---------------|--------------------------------------------------|--------------------------------------------|
|
|
30
30
|
| `input` | `string \| URL` | The HTML content to convert to PDF. | A file path or a URL pointing to HTML content, as either a Node URL object or a Node string. Alternativly pass an html string directly. |
|
|
31
|
+
| `base64` | `string?` | Return a base64 encoded string instead of a buffer | Boolean |
|
|
31
32
|
|
|
32
33
|
## 2. `img2pdf`
|
|
33
34
|
|
|
@@ -70,4 +71,4 @@ pdf2img(input: Buffer | string | URL, options: Options = {})
|
|
|
70
71
|
|-----------|----------------------------------------------------------------------|--------------------------------------------------|
|
|
71
72
|
| `scale` | `number` | Scaling factor for rendering the PDF pages. Default is 1. |
|
|
72
73
|
| `password`| `string` | Optional password for decrypting password-protected PDFs. |
|
|
73
|
-
| `
|
|
74
|
+
| `mime` | `string` | The mime type to output - "image/png" | "image/jpeg". Default is "image/png". |
|
package/dist/canvas.d.ts
CHANGED
|
@@ -1,7 +1,14 @@
|
|
|
1
|
-
import { Canvas } from "canvas";
|
|
1
|
+
import { Canvas, CanvasRenderingContext2D } from "canvas";
|
|
2
|
+
type Factory = {
|
|
3
|
+
canvas: Canvas | null;
|
|
4
|
+
context: CanvasRenderingContext2D | null;
|
|
5
|
+
};
|
|
2
6
|
export declare class CanvasFactory {
|
|
3
7
|
create(width: number, height: number, transparent: boolean): {
|
|
4
8
|
canvas: Canvas;
|
|
5
|
-
context:
|
|
9
|
+
context: CanvasRenderingContext2D;
|
|
6
10
|
};
|
|
11
|
+
reset(factory: Factory, width: number, height: number): void;
|
|
12
|
+
destroy(factory: Factory): void;
|
|
7
13
|
}
|
|
14
|
+
export {};
|
package/dist/canvas.js
CHANGED
|
@@ -14,5 +14,24 @@ export class CanvasFactory {
|
|
|
14
14
|
context
|
|
15
15
|
};
|
|
16
16
|
}
|
|
17
|
+
reset(factory, width, height) {
|
|
18
|
+
if (!factory.canvas) {
|
|
19
|
+
throw new Error("Canvas is not specified");
|
|
20
|
+
}
|
|
21
|
+
if (width <= 0 || height <= 0) {
|
|
22
|
+
throw new Error("Invalid canvas size");
|
|
23
|
+
}
|
|
24
|
+
factory.canvas.width = width;
|
|
25
|
+
factory.canvas.height = height;
|
|
26
|
+
}
|
|
27
|
+
destroy(factory) {
|
|
28
|
+
if (!factory.canvas) {
|
|
29
|
+
throw new Error("Canvas is not specified");
|
|
30
|
+
}
|
|
31
|
+
factory.canvas.width = 0;
|
|
32
|
+
factory.canvas.height = 0;
|
|
33
|
+
factory.canvas = null;
|
|
34
|
+
factory.context = null;
|
|
35
|
+
}
|
|
17
36
|
}
|
|
18
37
|
//# sourceMappingURL=canvas.js.map
|
package/dist/canvas.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"canvas.js","sourceRoot":"","sources":["../src/canvas.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"canvas.js","sourceRoot":"","sources":["../src/canvas.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,MAAM,EAA4B,MAAM,QAAQ,CAAA;AAOzD,MAAM,OAAO,aAAa;IAWjB,MAAM,CAAG,KAAa,EAAE,MAAc,EAAE,WAAoB;QACjE,IAAK,KAAK,IAAI,CAAC,IAAI,MAAM,IAAI,CAAC,EAAG,CAAC;YAChC,MAAM,IAAI,KAAK,CAAG,qBAAqB,CAAE,CAAA;QAC3C,CAAC;QAED,MAAM,MAAM,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
-
options: PngConfig;
|
|
8
|
-
} | {
|
|
9
|
-
mime: "image/jpeg";
|
|
10
|
-
options: JpegConfig;
|
|
11
|
-
};
|
|
4
|
+
mime?: "image/png" | "image/jpeg";
|
|
12
5
|
};
|
|
13
6
|
export declare const pdf2img: (input: Buffer | string | URL, options?: Options) => Promise<{
|
|
14
7
|
length: number;
|
|
15
|
-
metadata:
|
|
8
|
+
metadata: any;
|
|
16
9
|
pages: Buffer[];
|
|
17
10
|
}>;
|
|
18
11
|
export {};
|
package/dist/modules/pdf2img.js
CHANGED
|
@@ -1,12 +1,19 @@
|
|
|
1
|
+
const originalLog = console.log;
|
|
2
|
+
console.log = function (message, ...args) {
|
|
3
|
+
if (message && message === `Warning: applyPath2DToCanvasRenderingContext: "TypeError: Cannot assign to read only property 'clip' of object '#<CanvasRenderingContext2D>'".`) {
|
|
4
|
+
return;
|
|
5
|
+
}
|
|
6
|
+
originalLog.apply(console, [message, ...args]);
|
|
7
|
+
};
|
|
1
8
|
import { CanvasFactory } from "../canvas.js";
|
|
2
9
|
import { getBuffer } from "../utilies.js";
|
|
3
10
|
import { getDocument } from "pdfjs-dist/legacy/build/pdf.mjs";
|
|
4
11
|
import { fileTypeFromBuffer } from "file-type";
|
|
5
12
|
import { fileURLToPath } from "url";
|
|
6
13
|
import { resolve, dirname, join, sep } from "path";
|
|
14
|
+
import sharp from "sharp";
|
|
7
15
|
const __filename = fileURLToPath(import.meta.url);
|
|
8
16
|
const __dirname = dirname(__filename);
|
|
9
|
-
const canvasFactory = new CanvasFactory();
|
|
10
17
|
export const pdf2img = async (input, options = {}) => {
|
|
11
18
|
const buffer = await getBuffer(input);
|
|
12
19
|
const type = await fileTypeFromBuffer(buffer);
|
|
@@ -15,11 +22,14 @@ export const pdf2img = async (input, options = {}) => {
|
|
|
15
22
|
process.exit(1);
|
|
16
23
|
}
|
|
17
24
|
const pdfjsPath = resolve(__dirname, "..", "..", "node_modules", "pdfjs-dist");
|
|
25
|
+
const canvasFactory = new CanvasFactory();
|
|
18
26
|
const newoptions = {
|
|
19
27
|
password: options.password ?? undefined,
|
|
20
28
|
standardFontDataUrl: join(pdfjsPath, `standard_fonts${sep}`),
|
|
21
29
|
cMapUrl: join(pdfjsPath, `cmaps${sep}`),
|
|
22
30
|
isEvalSupported: false,
|
|
31
|
+
CanvasFactory: CanvasFactory,
|
|
32
|
+
verbosity: 0,
|
|
23
33
|
data: Uint8Array.from(buffer)
|
|
24
34
|
};
|
|
25
35
|
const pdfDocument = await getDocument(newoptions).promise;
|
|
@@ -30,22 +40,17 @@ export const pdf2img = async (input, options = {}) => {
|
|
|
30
40
|
}
|
|
31
41
|
async function getPage(pageNumber) {
|
|
32
42
|
const page = await pdfDocument.getPage(pageNumber);
|
|
33
|
-
const viewport = page.getViewport({ scale: options.scale ??
|
|
43
|
+
const viewport = page.getViewport({ scale: options.scale ?? 300 / 72 });
|
|
34
44
|
const { canvas, context } = canvasFactory.create(viewport.width, viewport.height, true);
|
|
35
45
|
await page.render({
|
|
36
46
|
canvasContext: context,
|
|
37
47
|
viewport
|
|
38
48
|
}).promise;
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
resolution: 1920,
|
|
45
|
-
compressionLevel: 0,
|
|
46
|
-
filters: canvas.PNG_FILTER_NONE
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
+
const imageBuffer = canvas.toBuffer();
|
|
50
|
+
const processedBuffer = await sharp(imageBuffer)
|
|
51
|
+
.toFormat(options.mime === "image/jpeg" ? "jpeg" : "png")
|
|
52
|
+
.toBuffer();
|
|
53
|
+
return processedBuffer;
|
|
49
54
|
}
|
|
50
55
|
return {
|
|
51
56
|
length: pdfDocument.numPages,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pdf2img.js","sourceRoot":"","sources":["../../src/modules/pdf2img.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAA;AAC5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E,CAAA;QAG1E,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,aAAa,CAAC,MAAM,CAC9C,QAAQ,CAAC,KAAK,EACd,QAAQ,CAAC,MAAM,EACf,IAAI,CACL,CAAA;QAGD,MAAM,IAAI,CAAC,MAAM,CAAG;YAClB,aAAa,EAAE,OAAc;YAC7B,QAAQ;SACT,CAAE,CAAC,OAAO,CAAA;QAEX,MAAM,WAAW,GAAG,MAAM,CAAC,QAAQ,EAAI,CAAA;QAEvC,MAAM,eAAe,GAAG,MAAM,KAAK,CAAG,WAAW,CAAE;aAChD,QAAQ,CAAG,OAAO,CAAC,IAAI,KAAK,YAAY,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,CAAE;aAC3D,QAAQ,EAAI,CAAA;QAEf,OAAO,eAAe,CAAA;IACxB,CAAC;IAGD,OAAO;QACL,MAAM,EAAE,WAAW,CAAC,QAAQ;QAC5B,QAAQ,EAAE,QAAQ,CAAC,IAAI;QACvB,KAAK;KACN,CAAA;AACH,CAAC,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@iqx-limited/quick-pdf",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"author": "IQX",
|
|
5
5
|
"description": "Converting PDFs to images (📃 to 📸)",
|
|
6
6
|
"type": "module",
|
|
@@ -41,7 +41,8 @@
|
|
|
41
41
|
"pdfjs-dist": "^4.8.69",
|
|
42
42
|
"pdfkit": "^0.15.1",
|
|
43
43
|
"playwright": "^1.48.2",
|
|
44
|
-
"semver": "^7.6.3"
|
|
44
|
+
"semver": "^7.6.3",
|
|
45
|
+
"sharp": "^0.33.5"
|
|
45
46
|
},
|
|
46
47
|
"devDependencies": {
|
|
47
48
|
"@stylistic/eslint-plugin": "^2.10.1",
|