unpdf 0.12.1 → 0.12.2

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/index.cjs CHANGED
@@ -54,10 +54,10 @@ async function interopDefault(m) {
54
54
 
55
55
  async function extractImages$1(data, pageNumber) {
56
56
  const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
57
- const page = await pdf.getPage(pageNumber);
58
57
  if (pageNumber < 1 || pageNumber > pdf.numPages) {
59
58
  throw new Error(`Invalid page number. Must be between 1 and ${pdf.numPages}.`);
60
59
  }
60
+ const page = await pdf.getPage(pageNumber);
61
61
  const operatorList = await page.getOperatorList();
62
62
  const { OPS } = await getResolvedPDFJS();
63
63
  const images = [];
@@ -68,7 +68,22 @@ async function extractImages$1(data, pageNumber) {
68
68
  }
69
69
  const imageKey = operatorList.argsArray[i][0];
70
70
  const image = await page.objs.get(imageKey);
71
- images.push(image.data);
71
+ if (!image || !image.data || !image.width || !image.height) {
72
+ continue;
73
+ }
74
+ const { width, height, data: data2 } = image;
75
+ const calculatedChannels = data2.length / (width * height);
76
+ if (![1, 3, 4].includes(calculatedChannels)) {
77
+ continue;
78
+ }
79
+ const channels = calculatedChannels;
80
+ images.push({
81
+ data: data2,
82
+ width,
83
+ height,
84
+ channels,
85
+ key: imageKey
86
+ });
72
87
  }
73
88
  return images;
74
89
  }
package/dist/index.d.cts CHANGED
@@ -3,7 +3,34 @@ import { Canvas } from 'canvas';
3
3
  import { DocumentInitParameters, PDFDocumentProxy } from './types/src/display/api';
4
4
  import * as _PDFJS from './types/src/pdf';
5
5
 
6
- declare function extractImages$1(data: DocumentInitParameters['data'] | PDFDocumentProxy, pageNumber: number): Promise<Uint8ClampedArray[]>;
6
+ interface ExtractedImageObject {
7
+ data: Uint8ClampedArray;
8
+ width: number;
9
+ height: number;
10
+ channels: 1 | 3 | 4;
11
+ key: string;
12
+ }
13
+ /**
14
+ * Extracts images from a specific page of a PDF document, including necessary metadata,
15
+ * such as width, height, and calculated color channels.
16
+ *
17
+ * This version calculates channels based on image data length, width, and height,
18
+ * as the `kind` property provided by PDF.js might not reliably indicate the actual
19
+ * channel count of the raw pixel data (e.g., returning RGBA data even when kind is 3).
20
+ *
21
+ * @example
22
+ * const imagesData = await extractImages(pdf, pageNum)
23
+ *
24
+ * for (const imgData of imagesData) {
25
+ * const imageIndex = totalImagesProcessed + 1;
26
+ * await sharp(imgData.data, {
27
+ * raw: { width: imgData.width, height: imgData.height, channels: imgData.channels }
28
+ * })
29
+ * .png()
30
+ * .toFile(`${imageIndex}.png`);
31
+ * }
32
+ */
33
+ declare function extractImages$1(data: DocumentInitParameters['data'] | PDFDocumentProxy, pageNumber: number): Promise<ExtractedImageObject[]>;
7
34
  declare function renderPageAsImage$1(data: DocumentInitParameters['data'] | PDFDocumentProxy, pageNumber: number, options?: {
8
35
  canvas?: () => Promise<typeof canvas>;
9
36
  /** @default 1 */
package/dist/index.d.mts CHANGED
@@ -3,7 +3,34 @@ import { Canvas } from 'canvas';
3
3
  import { DocumentInitParameters, PDFDocumentProxy } from './types/src/display/api';
4
4
  import * as _PDFJS from './types/src/pdf';
5
5
 
6
- declare function extractImages$1(data: DocumentInitParameters['data'] | PDFDocumentProxy, pageNumber: number): Promise<Uint8ClampedArray[]>;
6
+ interface ExtractedImageObject {
7
+ data: Uint8ClampedArray;
8
+ width: number;
9
+ height: number;
10
+ channels: 1 | 3 | 4;
11
+ key: string;
12
+ }
13
+ /**
14
+ * Extracts images from a specific page of a PDF document, including necessary metadata,
15
+ * such as width, height, and calculated color channels.
16
+ *
17
+ * This version calculates channels based on image data length, width, and height,
18
+ * as the `kind` property provided by PDF.js might not reliably indicate the actual
19
+ * channel count of the raw pixel data (e.g., returning RGBA data even when kind is 3).
20
+ *
21
+ * @example
22
+ * const imagesData = await extractImages(pdf, pageNum)
23
+ *
24
+ * for (const imgData of imagesData) {
25
+ * const imageIndex = totalImagesProcessed + 1;
26
+ * await sharp(imgData.data, {
27
+ * raw: { width: imgData.width, height: imgData.height, channels: imgData.channels }
28
+ * })
29
+ * .png()
30
+ * .toFile(`${imageIndex}.png`);
31
+ * }
32
+ */
33
+ declare function extractImages$1(data: DocumentInitParameters['data'] | PDFDocumentProxy, pageNumber: number): Promise<ExtractedImageObject[]>;
7
34
  declare function renderPageAsImage$1(data: DocumentInitParameters['data'] | PDFDocumentProxy, pageNumber: number, options?: {
8
35
  canvas?: () => Promise<typeof canvas>;
9
36
  /** @default 1 */
package/dist/index.d.ts CHANGED
@@ -3,7 +3,34 @@ import { Canvas } from 'canvas';
3
3
  import { DocumentInitParameters, PDFDocumentProxy } from './types/src/display/api';
4
4
  import * as _PDFJS from './types/src/pdf';
5
5
 
6
- declare function extractImages$1(data: DocumentInitParameters['data'] | PDFDocumentProxy, pageNumber: number): Promise<Uint8ClampedArray[]>;
6
+ interface ExtractedImageObject {
7
+ data: Uint8ClampedArray;
8
+ width: number;
9
+ height: number;
10
+ channels: 1 | 3 | 4;
11
+ key: string;
12
+ }
13
+ /**
14
+ * Extracts images from a specific page of a PDF document, including necessary metadata,
15
+ * such as width, height, and calculated color channels.
16
+ *
17
+ * This version calculates channels based on image data length, width, and height,
18
+ * as the `kind` property provided by PDF.js might not reliably indicate the actual
19
+ * channel count of the raw pixel data (e.g., returning RGBA data even when kind is 3).
20
+ *
21
+ * @example
22
+ * const imagesData = await extractImages(pdf, pageNum)
23
+ *
24
+ * for (const imgData of imagesData) {
25
+ * const imageIndex = totalImagesProcessed + 1;
26
+ * await sharp(imgData.data, {
27
+ * raw: { width: imgData.width, height: imgData.height, channels: imgData.channels }
28
+ * })
29
+ * .png()
30
+ * .toFile(`${imageIndex}.png`);
31
+ * }
32
+ */
33
+ declare function extractImages$1(data: DocumentInitParameters['data'] | PDFDocumentProxy, pageNumber: number): Promise<ExtractedImageObject[]>;
7
34
  declare function renderPageAsImage$1(data: DocumentInitParameters['data'] | PDFDocumentProxy, pageNumber: number, options?: {
8
35
  canvas?: () => Promise<typeof canvas>;
9
36
  /** @default 1 */
package/dist/index.mjs CHANGED
@@ -52,10 +52,10 @@ async function interopDefault(m) {
52
52
 
53
53
  async function extractImages$1(data, pageNumber) {
54
54
  const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
55
- const page = await pdf.getPage(pageNumber);
56
55
  if (pageNumber < 1 || pageNumber > pdf.numPages) {
57
56
  throw new Error(`Invalid page number. Must be between 1 and ${pdf.numPages}.`);
58
57
  }
58
+ const page = await pdf.getPage(pageNumber);
59
59
  const operatorList = await page.getOperatorList();
60
60
  const { OPS } = await getResolvedPDFJS();
61
61
  const images = [];
@@ -66,7 +66,22 @@ async function extractImages$1(data, pageNumber) {
66
66
  }
67
67
  const imageKey = operatorList.argsArray[i][0];
68
68
  const image = await page.objs.get(imageKey);
69
- images.push(image.data);
69
+ if (!image || !image.data || !image.width || !image.height) {
70
+ continue;
71
+ }
72
+ const { width, height, data: data2 } = image;
73
+ const calculatedChannels = data2.length / (width * height);
74
+ if (![1, 3, 4].includes(calculatedChannels)) {
75
+ continue;
76
+ }
77
+ const channels = calculatedChannels;
78
+ images.push({
79
+ data: data2,
80
+ width,
81
+ height,
82
+ channels,
83
+ key: imageKey
84
+ });
70
85
  }
71
86
  return images;
72
87
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "unpdf",
3
3
  "type": "module",
4
- "version": "0.12.1",
4
+ "version": "0.12.2",
5
5
  "packageManager": "pnpm@9.12.1",
6
6
  "description": "Utilities to work with PDFs in Node.js, browser and workers",
7
7
  "author": "Johann Schopplich <hello@johannschopplich.com>",