unpdf 0.12.0 → 0.12.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
@@ -38,20 +38,23 @@ yarn add unpdf
38
38
  ### Extract Text From PDF
39
39
 
40
40
  ```ts
41
- import { extractText, getDocumentProxy } from "unpdf";
41
+ import { extractText, getDocumentProxy } from 'unpdf'
42
42
 
43
43
  // Fetch a PDF file from the web
44
44
  const buffer = await fetch(
45
- "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf",
46
- ).then((res) => res.arrayBuffer());
45
+ 'https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf',
46
+ ).then(res => res.arrayBuffer())
47
47
 
48
48
  // Or load it from the filesystem
49
- const buffer = await readFile("./dummy.pdf");
49
+ const buffer = await readFile('./dummy.pdf')
50
50
 
51
51
  // Load PDF from buffer
52
- const pdf = await getDocumentProxy(new Uint8Array(buffer));
52
+ const pdf = await getDocumentProxy(new Uint8Array(buffer))
53
53
  // Extract text from PDF
54
- const { totalPages, text } = await extractText(pdf, { mergePages: true });
54
+ const { totalPages, text } = await extractText(pdf, { mergePages: true })
55
+
56
+ console.log(`Total pages: ${totalPages}`)
57
+ console.log(text)
55
58
  ```
56
59
 
57
60
  ### Access the PDF.js API
@@ -65,19 +68,19 @@ This will return the resolved PDF.js module and gives full access to the PDF.js
65
68
  Especially useful for platforms like 🦕 Deno or if you want to use the PDF.js API directly. If no custom build was defined beforehand, the serverless build bundled with `unpdf` will be initialized.
66
69
 
67
70
  ```ts
68
- import { getResolvedPDFJS } from "unpdf";
71
+ import { getResolvedPDFJS } from 'unpdf'
69
72
 
70
- const { getDocument } = await getResolvedPDFJS();
71
- const data = Deno.readFileSync("dummy.pdf");
72
- const doc = await getDocument(data).promise;
73
+ const { getDocument } = await getResolvedPDFJS()
74
+ const data = Deno.readFileSync('dummy.pdf')
75
+ const doc = await getDocument(data).promise
73
76
 
74
- console.log(await doc.getMetadata());
77
+ console.log(await doc.getMetadata())
75
78
 
76
79
  for (let i = 1; i <= doc.numPages; i++) {
77
- const page = await doc.getPage(i);
78
- const textContent = await page.getTextContent();
79
- const contents = textContent.items.map((item) => item.str).join(" ");
80
- console.log(contents);
80
+ const page = await doc.getPage(i)
81
+ const textContent = await page.getTextContent()
82
+ const contents = textContent.items.map(item => item.str).join(' ')
83
+ console.log(contents)
81
84
  }
82
85
  ```
83
86
 
@@ -91,12 +94,12 @@ Generally speaking, you don't need to worry about the PDF.js build. `unpdf` ship
91
94
  ```ts
92
95
  // Before using any other method, define the PDF.js module
93
96
  // if you need another PDF.js build
94
- import { configureUnPDF } from "unpdf";
97
+ import { configureUnPDF } from 'unpdf'
95
98
 
96
99
  await configureUnPDF({
97
100
  // Use the official PDF.js build (make sure to install it first)
98
- pdfjs: () => import("pdfjs-dist"),
99
- });
101
+ pdfjs: () => import('pdfjs-dist'),
102
+ })
100
103
 
101
104
  // Now, you can use the other methods
102
105
  // …
@@ -115,7 +118,7 @@ interface UnPDFConfiguration {
115
118
  * // Use the official PDF.js build (make sure to install it first)
116
119
  * () => import('pdfjs-dist')
117
120
  */
118
- pdfjs?: () => Promise<PDFJS>;
121
+ pdfjs?: () => Promise<PDFJS>
119
122
  }
120
123
  ```
121
124
 
@@ -126,7 +129,7 @@ interface UnPDFConfiguration {
126
129
  Define a custom PDF.js module, like the legacy build. Make sure to call this method before using any other methods.
127
130
 
128
131
  ```ts
129
- function configureUnPDF(config: UnPDFConfiguration): Promise<void>;
132
+ function configureUnPDF(config: UnPDFConfiguration): Promise<void>
130
133
  ```
131
134
 
132
135
  ### `getResolvedPDFJS`
@@ -134,18 +137,18 @@ function configureUnPDF(config: UnPDFConfiguration): Promise<void>;
134
137
  Returns the resolved PDF.js module. If no build is defined, the latest version will be initialized.
135
138
 
136
139
  ```ts
137
- function getResolvedPDFJS(): Promise<PDFJS>;
140
+ function getResolvedPDFJS(): Promise<PDFJS>
138
141
  ```
139
142
 
140
143
  ### `getMeta`
141
144
 
142
145
  ```ts
143
146
  function getMeta(
144
- data: DocumentInitParameters["data"] | PDFDocumentProxy,
147
+ data: DocumentInitParameters['data'] | PDFDocumentProxy,
145
148
  ): Promise<{
146
- info: Record<string, any>;
147
- metadata: Record<string, any>;
148
- }>;
149
+ info: Record<string, any>
150
+ metadata: Record<string, any>
151
+ }>
149
152
  ```
150
153
 
151
154
  ### `extractText`
@@ -154,12 +157,12 @@ Extracts all text from a PDF. If `mergePages` is set to `true`, the text of all
154
157
 
155
158
  ```ts
156
159
  function extractText(
157
- data: DocumentInitParameters["data"] | PDFDocumentProxy,
160
+ data: DocumentInitParameters['data'] | PDFDocumentProxy,
158
161
  { mergePages }?: { mergePages?: boolean },
159
162
  ): Promise<{
160
- totalPages: number;
161
- text: string | string[];
162
- }>;
163
+ totalPages: number
164
+ text: string | string[]
165
+ }>
163
166
  ```
164
167
 
165
168
  ### `renderPageAsImage`
@@ -177,37 +180,37 @@ In order to use this method, you have to meet the following requirements:
177
180
  **Example**
178
181
 
179
182
  ```ts
180
- import { configureUnPDF, renderPageAsImage } from "unpdf";
183
+ import { configureUnPDF, renderPageAsImage } from 'unpdf'
181
184
 
182
185
  await configureUnPDF({
183
186
  // Use the official PDF.js build
184
- pdfjs: () => import("pdfjs-dist"),
185
- });
187
+ pdfjs: () => import('pdfjs-dist'),
188
+ })
186
189
 
187
- const pdf = await readFile("./dummy.pdf");
188
- const buffer = new Uint8Array(pdf);
189
- const pageNumber = 1;
190
+ const pdf = await readFile('./dummy.pdf')
191
+ const buffer = new Uint8Array(pdf)
192
+ const pageNumber = 1
190
193
 
191
194
  const result = await renderPageAsImage(buffer, pageNumber, {
192
- canvas: () => import("canvas"),
193
- });
194
- await writeFile("dummy-page-1.png", Buffer.from(result));
195
+ canvas: () => import('canvas'),
196
+ })
197
+ await writeFile('dummy-page-1.png', result)
195
198
  ```
196
199
 
197
200
  **Type Declaration**
198
201
 
199
202
  ```ts
200
203
  declare function renderPageAsImage(
201
- data: DocumentInitParameters["data"],
204
+ data: DocumentInitParameters['data'],
202
205
  pageNumber: number,
203
206
  options?: {
204
- canvas?: () => Promise<typeof import("canvas")>;
207
+ canvas?: () => Promise<typeof import('canvas')>
205
208
  /** @default 1 */
206
- scale?: number;
207
- width?: number;
208
- height?: number;
209
+ scale?: number
210
+ width?: number
211
+ height?: number
209
212
  },
210
- ): Promise<ArrayBuffer>;
213
+ ): Promise<ArrayBuffer>
211
214
  ```
212
215
 
213
216
  ## FAQ
package/dist/index.cjs CHANGED
@@ -33,19 +33,15 @@ async function resolvePDFJSImports(pdfjsResolver, { force = false } = {}) {
33
33
  return;
34
34
  } catch (error) {
35
35
  console.error(error);
36
- throw new Error(
37
- "Resolving failed. Please check the provided configuration."
38
- );
36
+ throw new Error("Resolving failed. Please check the provided configuration.");
39
37
  }
40
38
  }
41
39
  try {
42
- const { resolvePDFJS } = await Promise.resolve().then(function () { return pdfjs; });
40
+ const { resolvePDFJS } = await import('unpdf/pdfjs');
43
41
  resolvedModule = await resolvePDFJS();
44
42
  } catch (error) {
45
43
  console.error(error);
46
- throw new Error(
47
- "PDF.js is not available. Please add the package as a dependency."
48
- );
44
+ throw new Error("PDF.js is not available. Please add the package as a dependency.");
49
45
  }
50
46
  }
51
47
  function isPDFDocumentProxy(data) {
@@ -56,39 +52,11 @@ async function interopDefault(m) {
56
52
  return resolved.default || resolved;
57
53
  }
58
54
 
59
- async function getMeta$1(data) {
60
- const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
61
- const meta = await pdf.getMetadata();
62
- return {
63
- info: meta?.info ?? {},
64
- metadata: meta?.metadata?.getAll() ?? {}
65
- };
66
- }
67
-
68
- async function extractText$1(data, options = {}) {
69
- const { mergePages = false } = { ...options };
70
- const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
71
- const texts = await Promise.all(
72
- Array.from({ length: pdf.numPages }, (_, i) => getPageText(pdf, i + 1))
73
- );
74
- return {
75
- totalPages: pdf.numPages,
76
- text: mergePages ? texts.join("\n").replace(/\s+/g, " ") : texts
77
- };
78
- }
79
- async function getPageText(document, pageNumber) {
80
- const page = await document.getPage(pageNumber);
81
- const content = await page.getTextContent();
82
- return content.items.filter((item) => item.str != null).map((item) => item.str + (item.hasEOL ? "\n" : "")).join("");
83
- }
84
-
85
55
  async function extractImages$1(data, pageNumber) {
86
56
  const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
87
57
  const page = await pdf.getPage(pageNumber);
88
58
  if (pageNumber < 1 || pageNumber > pdf.numPages) {
89
- throw new Error(
90
- `Invalid page number. Must be between 1 and ${pdf.numPages}.`
91
- );
59
+ throw new Error(`Invalid page number. Must be between 1 and ${pdf.numPages}.`);
92
60
  }
93
61
  const operatorList = await page.getOperatorList();
94
62
  const { OPS } = await getResolvedPDFJS();
@@ -109,9 +77,7 @@ async function renderPageAsImage$1(data, pageNumber, options = {}) {
109
77
  const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data, { canvasFactory });
110
78
  const page = await pdf.getPage(pageNumber);
111
79
  if (pageNumber < 1 || pageNumber > pdf.numPages) {
112
- throw new Error(
113
- `Invalid page number. Must be between 1 and ${pdf.numPages}.`
114
- );
80
+ throw new Error(`Invalid page number. Must be between 1 and ${pdf.numPages}.`);
115
81
  }
116
82
  let outputScale = options.scale || 1;
117
83
  let viewport = page.getViewport({ scale: outputScale });
@@ -177,6 +143,32 @@ async function createIsomorphicCanvasFactory(canvas) {
177
143
  };
178
144
  }
179
145
 
146
+ async function getMeta$1(data) {
147
+ const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
148
+ const meta = await pdf.getMetadata();
149
+ return {
150
+ info: meta?.info ?? {},
151
+ metadata: meta?.metadata?.getAll() ?? {}
152
+ };
153
+ }
154
+
155
+ async function extractText$1(data, options = {}) {
156
+ const { mergePages = false } = { ...options };
157
+ const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
158
+ const texts = await Promise.all(
159
+ Array.from({ length: pdf.numPages }, (_, i) => getPageText(pdf, i + 1))
160
+ );
161
+ return {
162
+ totalPages: pdf.numPages,
163
+ text: mergePages ? texts.join("\n").replace(/\s+/g, " ") : texts
164
+ };
165
+ }
166
+ async function getPageText(document, pageNumber) {
167
+ const page = await document.getPage(pageNumber);
168
+ const content = await page.getTextContent();
169
+ return content.items.filter((item) => item.str != null).map((item) => item.str + (item.hasEOL ? "\n" : "")).join("");
170
+ }
171
+
180
172
  async function configureUnPDF(options) {
181
173
  const { pdfjs } = { ...options };
182
174
  if (pdfjs) {
@@ -201,10 +193,6 @@ const renderPageAsImage = async (...args) => {
201
193
  return await renderPageAsImage$1(...args);
202
194
  };
203
195
 
204
- const pdfjs = {
205
- __proto__: null
206
- };
207
-
208
196
  exports.configureUnPDF = configureUnPDF;
209
197
  exports.createIsomorphicCanvasFactory = createIsomorphicCanvasFactory;
210
198
  exports.extractImages = extractImages;
package/dist/index.d.cts CHANGED
@@ -1,28 +1,10 @@
1
- import { DocumentInitParameters, PDFDocumentProxy } from './types/src/display/api';
2
1
  import * as canvas from 'canvas';
3
2
  import { Canvas } from 'canvas';
3
+ import { DocumentInitParameters, PDFDocumentProxy } from './types/src/display/api';
4
4
  import * as _PDFJS from './types/src/pdf';
5
5
 
6
- declare function getMeta$1(data: DocumentInitParameters["data"] | PDFDocumentProxy): Promise<{
7
- info: Record<string, any>;
8
- metadata: Record<string, any>;
9
- }>;
10
-
11
- declare function extractText$1(data: DocumentInitParameters["data"] | PDFDocumentProxy, options?: {
12
- mergePages?: false;
13
- }): Promise<{
14
- totalPages: number;
15
- text: string[];
16
- }>;
17
- declare function extractText$1(data: DocumentInitParameters["data"] | PDFDocumentProxy, options: {
18
- mergePages: true;
19
- }): Promise<{
20
- totalPages: number;
21
- text: string;
22
- }>;
23
-
24
- declare function extractImages$1(data: DocumentInitParameters["data"] | PDFDocumentProxy, pageNumber: number): Promise<Uint8ClampedArray[]>;
25
- declare function renderPageAsImage$1(data: DocumentInitParameters["data"] | PDFDocumentProxy, pageNumber: number, options?: {
6
+ declare function extractImages$1(data: DocumentInitParameters['data'] | PDFDocumentProxy, pageNumber: number): Promise<Uint8ClampedArray[]>;
7
+ declare function renderPageAsImage$1(data: DocumentInitParameters['data'] | PDFDocumentProxy, pageNumber: number, options?: {
26
8
  canvas?: () => Promise<typeof canvas>;
27
9
  /** @default 1 */
28
10
  scale?: number;
@@ -45,6 +27,24 @@ declare function createIsomorphicCanvasFactory(canvas?: () => Promise<typeof can
45
27
  }): void;
46
28
  }>;
47
29
 
30
+ declare function getMeta$1(data: DocumentInitParameters['data'] | PDFDocumentProxy): Promise<{
31
+ info: Record<string, any>;
32
+ metadata: Record<string, any>;
33
+ }>;
34
+
35
+ declare function extractText$1(data: DocumentInitParameters['data'] | PDFDocumentProxy, options?: {
36
+ mergePages?: false;
37
+ }): Promise<{
38
+ totalPages: number;
39
+ text: string[];
40
+ }>;
41
+ declare function extractText$1(data: DocumentInitParameters['data'] | PDFDocumentProxy, options: {
42
+ mergePages: true;
43
+ }): Promise<{
44
+ totalPages: number;
45
+ text: string;
46
+ }>;
47
+
48
48
  type PDFJS = typeof _PDFJS;
49
49
  interface UnPDFConfiguration {
50
50
  /**
@@ -59,6 +59,8 @@ interface UnPDFConfiguration {
59
59
  pdfjs?: () => Promise<any>;
60
60
  }
61
61
 
62
+ declare function configureUnPDF(options: UnPDFConfiguration): Promise<void>;
63
+
62
64
  /**
63
65
  * Returns a PDFDocumentProxy instance from a given binary data.
64
66
  *
@@ -66,14 +68,12 @@ interface UnPDFConfiguration {
66
68
  * - `isEvalSupported: false`
67
69
  * - `useSystemFonts: true`
68
70
  */
69
- declare function getDocumentProxy(data: DocumentInitParameters["data"], options?: DocumentInitParameters): Promise<PDFDocumentProxy>;
71
+ declare function getDocumentProxy(data: DocumentInitParameters['data'], options?: DocumentInitParameters): Promise<PDFDocumentProxy>;
70
72
  declare function getResolvedPDFJS(): Promise<PDFJS>;
71
73
  declare function resolvePDFJSImports(pdfjsResolver?: () => Promise<any>, { force }?: {
72
74
  force?: boolean | undefined;
73
75
  }): Promise<void>;
74
76
 
75
- declare function configureUnPDF(options: UnPDFConfiguration): Promise<void>;
76
-
77
77
  declare const getMeta: typeof getMeta$1;
78
78
  declare const extractText: typeof extractText$1;
79
79
  declare const extractImages: typeof extractImages$1;
package/dist/index.d.mts CHANGED
@@ -1,28 +1,10 @@
1
- import { DocumentInitParameters, PDFDocumentProxy } from './types/src/display/api';
2
1
  import * as canvas from 'canvas';
3
2
  import { Canvas } from 'canvas';
3
+ import { DocumentInitParameters, PDFDocumentProxy } from './types/src/display/api';
4
4
  import * as _PDFJS from './types/src/pdf';
5
5
 
6
- declare function getMeta$1(data: DocumentInitParameters["data"] | PDFDocumentProxy): Promise<{
7
- info: Record<string, any>;
8
- metadata: Record<string, any>;
9
- }>;
10
-
11
- declare function extractText$1(data: DocumentInitParameters["data"] | PDFDocumentProxy, options?: {
12
- mergePages?: false;
13
- }): Promise<{
14
- totalPages: number;
15
- text: string[];
16
- }>;
17
- declare function extractText$1(data: DocumentInitParameters["data"] | PDFDocumentProxy, options: {
18
- mergePages: true;
19
- }): Promise<{
20
- totalPages: number;
21
- text: string;
22
- }>;
23
-
24
- declare function extractImages$1(data: DocumentInitParameters["data"] | PDFDocumentProxy, pageNumber: number): Promise<Uint8ClampedArray[]>;
25
- declare function renderPageAsImage$1(data: DocumentInitParameters["data"] | PDFDocumentProxy, pageNumber: number, options?: {
6
+ declare function extractImages$1(data: DocumentInitParameters['data'] | PDFDocumentProxy, pageNumber: number): Promise<Uint8ClampedArray[]>;
7
+ declare function renderPageAsImage$1(data: DocumentInitParameters['data'] | PDFDocumentProxy, pageNumber: number, options?: {
26
8
  canvas?: () => Promise<typeof canvas>;
27
9
  /** @default 1 */
28
10
  scale?: number;
@@ -45,6 +27,24 @@ declare function createIsomorphicCanvasFactory(canvas?: () => Promise<typeof can
45
27
  }): void;
46
28
  }>;
47
29
 
30
+ declare function getMeta$1(data: DocumentInitParameters['data'] | PDFDocumentProxy): Promise<{
31
+ info: Record<string, any>;
32
+ metadata: Record<string, any>;
33
+ }>;
34
+
35
+ declare function extractText$1(data: DocumentInitParameters['data'] | PDFDocumentProxy, options?: {
36
+ mergePages?: false;
37
+ }): Promise<{
38
+ totalPages: number;
39
+ text: string[];
40
+ }>;
41
+ declare function extractText$1(data: DocumentInitParameters['data'] | PDFDocumentProxy, options: {
42
+ mergePages: true;
43
+ }): Promise<{
44
+ totalPages: number;
45
+ text: string;
46
+ }>;
47
+
48
48
  type PDFJS = typeof _PDFJS;
49
49
  interface UnPDFConfiguration {
50
50
  /**
@@ -59,6 +59,8 @@ interface UnPDFConfiguration {
59
59
  pdfjs?: () => Promise<any>;
60
60
  }
61
61
 
62
+ declare function configureUnPDF(options: UnPDFConfiguration): Promise<void>;
63
+
62
64
  /**
63
65
  * Returns a PDFDocumentProxy instance from a given binary data.
64
66
  *
@@ -66,14 +68,12 @@ interface UnPDFConfiguration {
66
68
  * - `isEvalSupported: false`
67
69
  * - `useSystemFonts: true`
68
70
  */
69
- declare function getDocumentProxy(data: DocumentInitParameters["data"], options?: DocumentInitParameters): Promise<PDFDocumentProxy>;
71
+ declare function getDocumentProxy(data: DocumentInitParameters['data'], options?: DocumentInitParameters): Promise<PDFDocumentProxy>;
70
72
  declare function getResolvedPDFJS(): Promise<PDFJS>;
71
73
  declare function resolvePDFJSImports(pdfjsResolver?: () => Promise<any>, { force }?: {
72
74
  force?: boolean | undefined;
73
75
  }): Promise<void>;
74
76
 
75
- declare function configureUnPDF(options: UnPDFConfiguration): Promise<void>;
76
-
77
77
  declare const getMeta: typeof getMeta$1;
78
78
  declare const extractText: typeof extractText$1;
79
79
  declare const extractImages: typeof extractImages$1;
package/dist/index.d.ts CHANGED
@@ -1,28 +1,10 @@
1
- import { DocumentInitParameters, PDFDocumentProxy } from './types/src/display/api';
2
1
  import * as canvas from 'canvas';
3
2
  import { Canvas } from 'canvas';
3
+ import { DocumentInitParameters, PDFDocumentProxy } from './types/src/display/api';
4
4
  import * as _PDFJS from './types/src/pdf';
5
5
 
6
- declare function getMeta$1(data: DocumentInitParameters["data"] | PDFDocumentProxy): Promise<{
7
- info: Record<string, any>;
8
- metadata: Record<string, any>;
9
- }>;
10
-
11
- declare function extractText$1(data: DocumentInitParameters["data"] | PDFDocumentProxy, options?: {
12
- mergePages?: false;
13
- }): Promise<{
14
- totalPages: number;
15
- text: string[];
16
- }>;
17
- declare function extractText$1(data: DocumentInitParameters["data"] | PDFDocumentProxy, options: {
18
- mergePages: true;
19
- }): Promise<{
20
- totalPages: number;
21
- text: string;
22
- }>;
23
-
24
- declare function extractImages$1(data: DocumentInitParameters["data"] | PDFDocumentProxy, pageNumber: number): Promise<Uint8ClampedArray[]>;
25
- declare function renderPageAsImage$1(data: DocumentInitParameters["data"] | PDFDocumentProxy, pageNumber: number, options?: {
6
+ declare function extractImages$1(data: DocumentInitParameters['data'] | PDFDocumentProxy, pageNumber: number): Promise<Uint8ClampedArray[]>;
7
+ declare function renderPageAsImage$1(data: DocumentInitParameters['data'] | PDFDocumentProxy, pageNumber: number, options?: {
26
8
  canvas?: () => Promise<typeof canvas>;
27
9
  /** @default 1 */
28
10
  scale?: number;
@@ -45,6 +27,24 @@ declare function createIsomorphicCanvasFactory(canvas?: () => Promise<typeof can
45
27
  }): void;
46
28
  }>;
47
29
 
30
+ declare function getMeta$1(data: DocumentInitParameters['data'] | PDFDocumentProxy): Promise<{
31
+ info: Record<string, any>;
32
+ metadata: Record<string, any>;
33
+ }>;
34
+
35
+ declare function extractText$1(data: DocumentInitParameters['data'] | PDFDocumentProxy, options?: {
36
+ mergePages?: false;
37
+ }): Promise<{
38
+ totalPages: number;
39
+ text: string[];
40
+ }>;
41
+ declare function extractText$1(data: DocumentInitParameters['data'] | PDFDocumentProxy, options: {
42
+ mergePages: true;
43
+ }): Promise<{
44
+ totalPages: number;
45
+ text: string;
46
+ }>;
47
+
48
48
  type PDFJS = typeof _PDFJS;
49
49
  interface UnPDFConfiguration {
50
50
  /**
@@ -59,6 +59,8 @@ interface UnPDFConfiguration {
59
59
  pdfjs?: () => Promise<any>;
60
60
  }
61
61
 
62
+ declare function configureUnPDF(options: UnPDFConfiguration): Promise<void>;
63
+
62
64
  /**
63
65
  * Returns a PDFDocumentProxy instance from a given binary data.
64
66
  *
@@ -66,14 +68,12 @@ interface UnPDFConfiguration {
66
68
  * - `isEvalSupported: false`
67
69
  * - `useSystemFonts: true`
68
70
  */
69
- declare function getDocumentProxy(data: DocumentInitParameters["data"], options?: DocumentInitParameters): Promise<PDFDocumentProxy>;
71
+ declare function getDocumentProxy(data: DocumentInitParameters['data'], options?: DocumentInitParameters): Promise<PDFDocumentProxy>;
70
72
  declare function getResolvedPDFJS(): Promise<PDFJS>;
71
73
  declare function resolvePDFJSImports(pdfjsResolver?: () => Promise<any>, { force }?: {
72
74
  force?: boolean | undefined;
73
75
  }): Promise<void>;
74
76
 
75
- declare function configureUnPDF(options: UnPDFConfiguration): Promise<void>;
76
-
77
77
  declare const getMeta: typeof getMeta$1;
78
78
  declare const extractText: typeof extractText$1;
79
79
  declare const extractImages: typeof extractImages$1;
package/dist/index.mjs CHANGED
@@ -31,19 +31,15 @@ async function resolvePDFJSImports(pdfjsResolver, { force = false } = {}) {
31
31
  return;
32
32
  } catch (error) {
33
33
  console.error(error);
34
- throw new Error(
35
- "Resolving failed. Please check the provided configuration."
36
- );
34
+ throw new Error("Resolving failed. Please check the provided configuration.");
37
35
  }
38
36
  }
39
37
  try {
40
- const { resolvePDFJS } = await Promise.resolve().then(function () { return pdfjs; });
38
+ const { resolvePDFJS } = await import('unpdf/pdfjs');
41
39
  resolvedModule = await resolvePDFJS();
42
40
  } catch (error) {
43
41
  console.error(error);
44
- throw new Error(
45
- "PDF.js is not available. Please add the package as a dependency."
46
- );
42
+ throw new Error("PDF.js is not available. Please add the package as a dependency.");
47
43
  }
48
44
  }
49
45
  function isPDFDocumentProxy(data) {
@@ -54,39 +50,11 @@ async function interopDefault(m) {
54
50
  return resolved.default || resolved;
55
51
  }
56
52
 
57
- async function getMeta$1(data) {
58
- const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
59
- const meta = await pdf.getMetadata();
60
- return {
61
- info: meta?.info ?? {},
62
- metadata: meta?.metadata?.getAll() ?? {}
63
- };
64
- }
65
-
66
- async function extractText$1(data, options = {}) {
67
- const { mergePages = false } = { ...options };
68
- const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
69
- const texts = await Promise.all(
70
- Array.from({ length: pdf.numPages }, (_, i) => getPageText(pdf, i + 1))
71
- );
72
- return {
73
- totalPages: pdf.numPages,
74
- text: mergePages ? texts.join("\n").replace(/\s+/g, " ") : texts
75
- };
76
- }
77
- async function getPageText(document, pageNumber) {
78
- const page = await document.getPage(pageNumber);
79
- const content = await page.getTextContent();
80
- return content.items.filter((item) => item.str != null).map((item) => item.str + (item.hasEOL ? "\n" : "")).join("");
81
- }
82
-
83
53
  async function extractImages$1(data, pageNumber) {
84
54
  const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
85
55
  const page = await pdf.getPage(pageNumber);
86
56
  if (pageNumber < 1 || pageNumber > pdf.numPages) {
87
- throw new Error(
88
- `Invalid page number. Must be between 1 and ${pdf.numPages}.`
89
- );
57
+ throw new Error(`Invalid page number. Must be between 1 and ${pdf.numPages}.`);
90
58
  }
91
59
  const operatorList = await page.getOperatorList();
92
60
  const { OPS } = await getResolvedPDFJS();
@@ -107,9 +75,7 @@ async function renderPageAsImage$1(data, pageNumber, options = {}) {
107
75
  const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data, { canvasFactory });
108
76
  const page = await pdf.getPage(pageNumber);
109
77
  if (pageNumber < 1 || pageNumber > pdf.numPages) {
110
- throw new Error(
111
- `Invalid page number. Must be between 1 and ${pdf.numPages}.`
112
- );
78
+ throw new Error(`Invalid page number. Must be between 1 and ${pdf.numPages}.`);
113
79
  }
114
80
  let outputScale = options.scale || 1;
115
81
  let viewport = page.getViewport({ scale: outputScale });
@@ -175,6 +141,32 @@ async function createIsomorphicCanvasFactory(canvas) {
175
141
  };
176
142
  }
177
143
 
144
+ async function getMeta$1(data) {
145
+ const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
146
+ const meta = await pdf.getMetadata();
147
+ return {
148
+ info: meta?.info ?? {},
149
+ metadata: meta?.metadata?.getAll() ?? {}
150
+ };
151
+ }
152
+
153
+ async function extractText$1(data, options = {}) {
154
+ const { mergePages = false } = { ...options };
155
+ const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
156
+ const texts = await Promise.all(
157
+ Array.from({ length: pdf.numPages }, (_, i) => getPageText(pdf, i + 1))
158
+ );
159
+ return {
160
+ totalPages: pdf.numPages,
161
+ text: mergePages ? texts.join("\n").replace(/\s+/g, " ") : texts
162
+ };
163
+ }
164
+ async function getPageText(document, pageNumber) {
165
+ const page = await document.getPage(pageNumber);
166
+ const content = await page.getTextContent();
167
+ return content.items.filter((item) => item.str != null).map((item) => item.str + (item.hasEOL ? "\n" : "")).join("");
168
+ }
169
+
178
170
  async function configureUnPDF(options) {
179
171
  const { pdfjs } = { ...options };
180
172
  if (pdfjs) {
@@ -199,8 +191,4 @@ const renderPageAsImage = async (...args) => {
199
191
  return await renderPageAsImage$1(...args);
200
192
  };
201
193
 
202
- const pdfjs = {
203
- __proto__: null
204
- };
205
-
206
194
  export { configureUnPDF, createIsomorphicCanvasFactory, extractImages, extractText, getDocumentProxy, getMeta, getResolvedPDFJS, renderPageAsImage, resolvePDFJSImports };
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "unpdf",
3
3
  "type": "module",
4
- "version": "0.12.0",
5
- "packageManager": "pnpm@9.11.0",
4
+ "version": "0.12.1",
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>",
8
8
  "license": "MIT",
@@ -56,9 +56,7 @@
56
56
  "build:pdfjs": "rollup --config pdfjs.rollup.config.ts --configPlugin typescript && cp -R node_modules/pdfjs-dist/types dist",
57
57
  "dev": "unbuild --stub",
58
58
  "lint": "eslint .",
59
- "lint:fix": "npm run lint -- --fix",
60
- "format": "prettier \"**/*.{json,md,cjs,mjs,ts,yml}\" --write",
61
- "format:check": "prettier \"**/*.{json,md,cjs,mjs,ts,yml}\" --check",
59
+ "lint:fix": "eslint . --fix",
62
60
  "release": "bumpp",
63
61
  "test": "vitest",
64
62
  "test:types": "tsc --noEmit"
@@ -67,6 +65,7 @@
67
65
  "canvas": "^2.11.2"
68
66
  },
69
67
  "devDependencies": {
68
+ "@antfu/eslint-config": "^3.7.3",
70
69
  "@rollup/plugin-alias": "^5.1.1",
71
70
  "@rollup/plugin-commonjs": "^28.0.0",
72
71
  "@rollup/plugin-inject": "^5.0.5",
@@ -74,19 +73,17 @@
74
73
  "@rollup/plugin-replace": "^6.0.1",
75
74
  "@rollup/plugin-terser": "^0.4.4",
76
75
  "@rollup/plugin-typescript": "^12.1.0",
77
- "@types/node": "^20.16.10",
76
+ "@types/node": "^20.16.11",
78
77
  "bumpp": "^9.6.1",
79
- "eslint": "^9.11.1",
80
- "eslint-config-unjs": "^0.4.1",
78
+ "eslint": "^9.12.0",
81
79
  "fast-glob": "^3.3.2",
82
- "pdfjs-dist": "^4.6.82",
83
- "prettier": "^3.3.3",
84
- "rollup": "^4.22.5",
80
+ "pdfjs-dist": "~4.6.82",
81
+ "rollup": "^4.24.0",
85
82
  "tslib": "^2.7.0",
86
83
  "typescript": "^5.5.4",
87
- "unbuild": "^3.0.0-rc.8",
84
+ "unbuild": "^3.0.0-rc.11",
88
85
  "unenv": "^1.10.0",
89
- "vitest": "^2.1.1"
86
+ "vitest": "^2.1.2"
90
87
  },
91
88
  "pnpm": {
92
89
  "patchedDependencies": {