@iqx-limited/quick-pdf 1.0.21 → 1.1.1

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 CHANGED
@@ -1,13 +1,13 @@
1
1
  # QuickPDF
2
2
  Handling PDFs in NodeJS (📃 to 🖼️)
3
3
 
4
- This project provides a set of utilities for converting various file formats without the need for additional dependencies. It supports multiple file types and leverages technologies like `Playwright`, `PDFKit`, and `pdfjs-dist` for file processing.
4
+ This project provides a set of utilities for converting various file formats without the need for additional dependencies. It supports multiple file types and leverages technologies like `Playwright` and `PDFKit` for file processing.
5
5
 
6
6
  ## Features
7
7
 
8
8
  - **HTML to PDF**: Converts HTML content (from a URL or file path) into a PDF.
9
9
  - **Image to PDF**: Converts images (JPEG, PNG) into a PDF, with optional headers and footers.
10
- - **PDF to Image**: Converts PDF files to image formats (JPEG or PNG) using `pdfjs-dist` and `canvas`.
10
+ - **PDF to Image**: Converts PDF files to image formats (JPEG or PNG) using `playwright`
11
11
  - **Utilities**: Includes helper functions to fetch and read HTML files and read files from local directories.
12
12
 
13
13
  ## Requirements
@@ -89,20 +89,21 @@ img2pdf(input: Buffer | string | URL, options: Options = {})
89
89
 
90
90
  ### Function Signature:
91
91
  ```typescript
92
- pdf2img(input: Buffer | string | URL, options: Options = {})
92
+ pdf2img(input: string | URL, options: Options = {})
93
93
  ```
94
94
 
95
+ If no pdf pages are detected in the input, an empty pages array is returned.
96
+
95
97
  ### Parameters:
96
98
 
97
99
  | Parameter | Type | Description | Data that can be passed |
98
100
  |-----------|---------------------------|--------------------------------------------------|--------------------------------------------|
99
- | `input` | `Buffer \| string \| URL` | The PDF content to convert to images. | A Buffer, file path, or URL pointing to a PDF. String or URL object can be passed for an HTTP or Local Path. |
101
+ | `input` | `string \| URL` | The PDF content to convert to images. | A Buffer, file path, or URL pointing to a PDF. String or URL object can be passed for an HTTP or Local Path. |
100
102
  | `options` | `Options` | Optional configuration for the image conversion. | An object with optional scale, password, and buffer configuration |
101
103
 
102
104
  ### Options Type:
103
105
 
104
106
  | Property | Type | Description |
105
107
  |-----------|----------------------------------------------------------------------|--------------------------------------------------|
106
- | `scale` | `number` | Scaling factor for rendering the PDF pages. Default is 1. |
107
- | `password`| `string` | Optional password for decrypting password-protected PDFs. |
108
- | `mime` | `string` | The mime type to output - "image/png" | "image/jpeg". Default is "image/png". |
108
+ | `quality` | `number` | Quality of image for jpg. Default is 100. |
109
+ | `type` | `string` | The mime type to output - "png" | "jpg". Default is "png". |
@@ -3,4 +3,4 @@ export type Options = {
3
3
  rules?: object;
4
4
  scale?: number;
5
5
  };
6
- export declare const html2pdf: (input: string | URL, options?: Options) => Promise<string | Buffer>;
6
+ export declare const html2pdf: (input: string | URL, options?: Options) => Promise<string | Buffer<ArrayBufferLike>>;
@@ -3,5 +3,5 @@ type Options = {
3
3
  footer?: string;
4
4
  fontSize?: number;
5
5
  };
6
- export declare const img2pdf: (input: Buffer | string | URL, options?: Options) => Promise<Buffer>;
6
+ export declare const img2pdf: (input: Buffer | string | URL, options?: Options) => Promise<Buffer<ArrayBufferLike>>;
7
7
  export {};
@@ -1,11 +1,9 @@
1
1
  type Options = {
2
- scale?: number;
3
- password?: string;
4
- mime?: "image/png" | "image/jpeg" | "image/webp";
2
+ quality?: number;
3
+ type?: "png" | "jpeg";
5
4
  };
6
- export declare const pdf2img: (input: Buffer | string | URL, options?: Options) => Promise<{
5
+ export declare const pdf2img: (input: string | URL, options?: Options) => Promise<{
7
6
  length: number;
8
- metadata: any;
9
- pages: Buffer[];
7
+ pages: Buffer<ArrayBufferLike>[];
10
8
  }>;
11
9
  export {};
@@ -1,74 +1,36 @@
1
- const originalLog = console.log;
2
- console.log = function (message, ...args) {
3
- if (message &&
4
- (message ===
5
- `Warning: applyPath2DToCanvasRenderingContext: "TypeError: Cannot assign to read only property 'clip' of object '#<CanvasRenderingContext2D>'".`)) {
6
- return;
7
- }
8
- originalLog.apply(console, [message, ...args]);
9
- };
10
- import { CanvasFactory } from "../canvas.js";
11
- import { getBuffer } from "../utilies.js";
12
- import { getDocument } from "pdfjs-dist/legacy/build/pdf.mjs";
13
- import { fileTypeFromBuffer } from "file-type";
14
- import { fileURLToPath } from "url";
15
- import { resolve, dirname, join, sep } from "path";
16
- import sharp from "sharp";
17
- const __filename = fileURLToPath(import.meta.url);
18
- const __dirname = dirname(__filename);
1
+ import { chromium } from "playwright";
19
2
  export const pdf2img = async (input, options = {}) => {
20
- const buffer = await getBuffer(input);
21
- const type = await fileTypeFromBuffer(buffer);
22
- if (type?.mime !== "application/pdf") {
23
- console.error(`ERROR: Provided File is not a PDF.`);
24
- process.exit(1);
25
- }
26
- const pdfjsPath = resolve(__dirname, "..", "..", "node_modules", "pdfjs-dist");
27
- const canvasFactory = new CanvasFactory();
28
- const newoptions = {
29
- password: options.password ?? undefined,
30
- standardFontDataUrl: join(pdfjsPath, `standard_fonts${sep}`),
31
- cMapUrl: join(pdfjsPath, `cmaps${sep}`),
32
- isEvalSupported: false,
33
- CanvasFactory: CanvasFactory,
34
- verbosity: 0,
35
- data: Uint8Array.from(buffer)
36
- };
37
- const pdfDocument = await getDocument(newoptions).promise;
38
- const metadata = await pdfDocument.getMetadata();
3
+ const browser = await chromium.launch();
4
+ const page = await browser.newPage();
5
+ await page.goto(input.toString(), { waitUntil: "networkidle" });
6
+ const pageCount = await page.evaluate(() => {
7
+ const pages = document.querySelectorAll(".page");
8
+ return pages.length;
9
+ });
39
10
  const pages = [];
40
- for (let i = 1; i <= pdfDocument.numPages; i++) {
41
- pages.push(await getPage(i));
42
- }
43
- async function getPage(pageNumber) {
44
- const page = await pdfDocument.getPage(pageNumber);
45
- const viewport = page.getViewport({ scale: options.scale ?? 300 / 72 });
46
- const { canvas, context } = canvasFactory.create(viewport.width, viewport.height, true);
47
- await page.render({
48
- canvasContext: context,
49
- viewport
50
- }).promise;
51
- const quality = options.mime === "image/png" ? undefined : 100;
52
- let imageBuffer = undefined;
53
- if (options.mime) {
54
- if (options.mime === "image/png") {
55
- imageBuffer = canvas.toBuffer("image/png");
56
- }
57
- else {
58
- imageBuffer = canvas.toBuffer(options.mime, quality);
11
+ for (let i = 1; i <= pageCount; i++) {
12
+ await page.waitForSelector(`.page[data-page-number='${i}']`, { timeout: 10000 });
13
+ const pdfPage = await page.$(`.page[data-page-number='${i}']`);
14
+ if (pdfPage) {
15
+ const boundingBox = await pdfPage.boundingBox();
16
+ if (boundingBox) {
17
+ const imageBuffer = await page.screenshot({
18
+ type: options.type ?? "png",
19
+ quality: options.quality ?? 100,
20
+ clip: {
21
+ x: boundingBox.x,
22
+ y: boundingBox.y,
23
+ width: boundingBox.width,
24
+ height: boundingBox.height,
25
+ }
26
+ });
27
+ pages.push(imageBuffer);
59
28
  }
60
29
  }
61
- else {
62
- imageBuffer = canvas.toBuffer("image/png");
63
- }
64
- const processedBuffer = await sharp(imageBuffer)
65
- .toFormat(options.mime === "image/jpeg" ? "jpeg" : "png")
66
- .toBuffer();
67
- return processedBuffer;
68
30
  }
31
+ await browser.close();
69
32
  return {
70
- length: pdfDocument.numPages,
71
- metadata: metadata.info,
33
+ length: pageCount,
72
34
  pages
73
35
  };
74
36
  };
@@ -1 +1 @@
1
- {"version":3,"file":"pdf2img.js","sourceRoot":"","sources":["../../src/modules/pdf2img.ts"],"names":[],"mappings":"AAIA,MAAM,WAAW,GAAG,OAAO,CAAC,GAAG,CAAA;AAC/B,OAAO,CAAC,GAAG,GAAG,UAAW,OAAO,EAAE,GAAG,IAAI;IACvC,IACE,OAAO;QACP,CACE,OAAO;YACP,gJAAgJ,CACjJ,EACD,CAAC;QACD,OAAM;IACR,CAAC;IAGD,WAAW,CAAC,KAAK,CAAG,OAAO,EAAE,CAAE,OAAO,EAAE,GAAG,IAAI,CAAE,CAAE,CAAA;AACrD,CAAC,CAAA;AAED,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAA;AAC5C,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAA;AACzC,OAAO,EAAE,WAAW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CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,SAAS,EAAE,CAAC,EAAE,EAAG,CAAC;QACtC,MAAM,IAAI,CAAC,eAAe,CAAG,2BAA2B,CAAC,IAAI,EAAE,EAAE,OAAO,EAAE,KAAK,EAAE,CAAE,CAAA;QAEnF,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,CAAC,CAAG,2BAA2B,CAAC,IAAI,CAAE,CAAA;QACjE,IAAK,OAAO,EAAG,CAAC;YAEd,MAAM,WAAW,GAAG,MAAM,OAAO,CAAC,WAAW,EAAI,CAAA;YACjD,IAAK,WAAW,EAAG,CAAC;gBAElB,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,UAAU,CAAG;oBAC1C,IAAI,EAAE,OAAO,CAAC,IAAI,IAAI,KAAK;oBAC3B,OAAO,EAAE,OAAO,CAAC,OAAO,IAAI,GAAG;oBAC/B,IAAI,EAAE;wBACJ,CAAC,EAAE,WAAW,CAAC,CAAC;wBAChB,CAAC,EAAE,WAAW,CAAC,CAAC;wBAChB,KAAK,EAAE,WAAW,CAAC,KAAK;wBACxB,MAAM,EAAE,WAAW,CAAC,MAAM;qBAC3B;iBACF,CAAE,CAAA;gBAEH,KAAK,CAAC,IAAI,CAAG,WAAW,CAAE,CAAA;YAC5B,CAAC;QACH,CAAC;IACH,CAAC;IAED,MAAM,OAAO,CAAC,KAAK,EAAI,CAAA;IAEvB,OAAO;QACL,MAAM,EAAE,SAAS;QACjB,KAAK;KACN,CAAA;AACH,CAAC,CAAA"}
@@ -1 +1 @@
1
- {"root":["../src/canvas.ts","../src/index.ts","../src/utilies.ts","../src/modules/html2pdf.ts","../src/modules/img2pdf.ts","../src/modules/pdf2img.ts"],"version":"5.6.3"}
1
+ {"root":["../src/index.ts","../src/utilies.ts","../src/modules/html2pdf.ts","../src/modules/img2pdf.ts","../src/modules/pdf2img.ts"],"version":"5.7.2"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iqx-limited/quick-pdf",
3
- "version": "1.0.21",
3
+ "version": "1.1.1",
4
4
  "author": "IQX",
5
5
  "description": "Converting PDFs to images (📃 to 📸)",
6
6
  "type": "module",
@@ -34,25 +34,21 @@
34
34
  },
35
35
  "engineStrict": true,
36
36
  "dependencies": {
37
- "@napi-rs/canvas": "^0.1.62",
38
37
  "file-type": "^19.6.0",
39
- "html-validate": "^8.25.0",
38
+ "html-validate": "^8.26.0",
40
39
  "image-size": "^1.1.1",
41
40
  "path2d-polyfill": "^3.1.3",
42
- "pdfjs-dist": "^4.8.69",
43
41
  "pdfkit": "^0.15.1",
44
42
  "playwright": "^1.49.0",
45
- "semver": "^7.6.3",
46
- "sharp": "^0.33.5"
43
+ "semver": "^7.6.3"
47
44
  },
48
45
  "devDependencies": {
49
46
  "@stylistic/eslint-plugin": "^2.11.0",
50
- "@types/node": "^22.9.0",
51
- "@types/pdfjs-dist": "2.10.377",
52
- "@types/pdfkit": "^0.13.5",
47
+ "@types/node": "^22.10.1",
48
+ "@types/pdfkit": "^0.13.6",
53
49
  "@types/semver": "^7.5.8",
54
50
  "eslint": "^9.15.0",
55
- "typescript": "^5.6.3",
56
- "typescript-eslint": "^8.15.0"
51
+ "typescript": "^5.7.2",
52
+ "typescript-eslint": "^8.16.0"
57
53
  }
58
54
  }