unpdf 0.11.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 +51 -46
- package/dist/index.cjs +34 -40
- package/dist/index.d.cts +24 -24
- package/dist/index.d.mts +24 -24
- package/dist/index.d.ts +24 -24
- package/dist/index.mjs +34 -40
- package/dist/pdfjs.mjs +1 -1
- package/dist/types/src/display/annotation_layer.d.ts +3 -3
- package/dist/types/src/display/annotation_storage.d.ts +9 -0
- package/dist/types/src/display/api.d.ts +48 -26
- package/dist/types/src/display/base_factory.d.ts +4 -0
- package/dist/types/src/display/canvas.d.ts +2 -0
- package/dist/types/src/display/display_utils.d.ts +2 -1
- package/dist/types/src/display/draw_layer.d.ts +1 -1
- package/dist/types/src/display/editor/alt_text.d.ts +14 -3
- package/dist/types/src/display/editor/annotation_editor_layer.d.ts +3 -1
- package/dist/types/src/display/editor/editor.d.ts +11 -2
- package/dist/types/src/display/editor/freetext.d.ts +2 -2
- package/dist/types/src/display/editor/highlight.d.ts +4 -4
- package/dist/types/src/display/editor/ink.d.ts +4 -3
- package/dist/types/src/display/editor/outliner.d.ts +3 -3
- package/dist/types/src/display/editor/stamp.d.ts +21 -0
- package/dist/types/src/display/editor/toolbar.d.ts +3 -2
- package/dist/types/src/display/editor/tools.d.ts +20 -5
- package/dist/types/src/display/node_stream.d.ts +1 -1
- package/dist/types/src/display/text_layer.d.ts +10 -11
- package/dist/types/src/display/worker_options.d.ts +2 -2
- package/dist/types/src/pdf.d.ts +1 -4
- package/dist/types/src/shared/util.d.ts +3 -0
- package/dist/types/web/annotation_layer_builder.d.ts +1 -0
- package/dist/types/web/app_options.d.ts +4 -2
- package/dist/types/web/download_manager.d.ts +1 -2
- package/dist/types/web/event_utils.d.ts +8 -6
- package/dist/types/web/genericl10n.d.ts +5 -5
- package/dist/types/web/interfaces.d.ts +2 -9
- package/dist/types/web/l10n.d.ts +6 -2
- package/dist/types/web/pdf_find_controller.d.ts +24 -0
- package/dist/types/web/pdf_link_service.d.ts +1 -1
- package/dist/types/web/pdf_page_view.d.ts +12 -6
- package/dist/types/web/pdf_thumbnail_view.d.ts +11 -3
- package/dist/types/web/pdf_thumbnail_viewer.d.ts +16 -1
- package/dist/types/web/pdf_viewer.d.ts +22 -6
- package/dist/types/web/text_accessibility.d.ts +1 -1
- package/dist/types/web/text_layer_builder.d.ts +4 -4
- package/dist/types/web/ui_utils.d.ts +7 -2
- package/package.json +21 -24
package/README.md
CHANGED
|
@@ -16,8 +16,7 @@ This library is also intended as a modern alternative to the unmaintained but st
|
|
|
16
16
|
|
|
17
17
|
## PDF.js Compatibility
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
> This package is currently using PDF.js v4.3.136.
|
|
19
|
+
The serverless build of PDF.js provided by `unpdf` is based on PDF.js v4.6.82. If you need a different version, you can [use another PDF.js build](#use-official-or-legacy-pdfjs-build).
|
|
21
20
|
|
|
22
21
|
## Installation
|
|
23
22
|
|
|
@@ -39,20 +38,23 @@ yarn add unpdf
|
|
|
39
38
|
### Extract Text From PDF
|
|
40
39
|
|
|
41
40
|
```ts
|
|
42
|
-
import { extractText, getDocumentProxy } from
|
|
41
|
+
import { extractText, getDocumentProxy } from 'unpdf'
|
|
43
42
|
|
|
44
43
|
// Fetch a PDF file from the web
|
|
45
44
|
const buffer = await fetch(
|
|
46
|
-
|
|
47
|
-
).then(
|
|
45
|
+
'https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf',
|
|
46
|
+
).then(res => res.arrayBuffer())
|
|
48
47
|
|
|
49
48
|
// Or load it from the filesystem
|
|
50
|
-
const buffer = await readFile(
|
|
49
|
+
const buffer = await readFile('./dummy.pdf')
|
|
51
50
|
|
|
52
51
|
// Load PDF from buffer
|
|
53
|
-
const pdf = await getDocumentProxy(new Uint8Array(buffer))
|
|
52
|
+
const pdf = await getDocumentProxy(new Uint8Array(buffer))
|
|
54
53
|
// Extract text from PDF
|
|
55
|
-
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)
|
|
56
58
|
```
|
|
57
59
|
|
|
58
60
|
### Access the PDF.js API
|
|
@@ -66,19 +68,19 @@ This will return the resolved PDF.js module and gives full access to the PDF.js
|
|
|
66
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.
|
|
67
69
|
|
|
68
70
|
```ts
|
|
69
|
-
import { getResolvedPDFJS } from
|
|
71
|
+
import { getResolvedPDFJS } from 'unpdf'
|
|
70
72
|
|
|
71
|
-
const { getDocument } = await getResolvedPDFJS()
|
|
72
|
-
const data = Deno.readFileSync(
|
|
73
|
-
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
|
|
74
76
|
|
|
75
|
-
console.log(await doc.getMetadata())
|
|
77
|
+
console.log(await doc.getMetadata())
|
|
76
78
|
|
|
77
79
|
for (let i = 1; i <= doc.numPages; i++) {
|
|
78
|
-
const page = await doc.getPage(i)
|
|
79
|
-
const textContent = await page.getTextContent()
|
|
80
|
-
const contents = textContent.items.map(
|
|
81
|
-
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)
|
|
82
84
|
}
|
|
83
85
|
```
|
|
84
86
|
|
|
@@ -86,15 +88,18 @@ for (let i = 1; i <= doc.numPages; i++) {
|
|
|
86
88
|
|
|
87
89
|
Generally speaking, you don't need to worry about the PDF.js build. `unpdf` ships with a serverless build of the latest PDF.js version. However, if you want to use the official PDF.js version or the legacy build, you can define a custom PDF.js module.
|
|
88
90
|
|
|
91
|
+
> [!WARNING]
|
|
92
|
+
> The latest PDF.js v4.6.82 uses `Promise.withResolvers`, which may not be supported in all environments, such as Node < 22. Consider to use the bundled serverless build, which includes a polyfill, or use an older version of PDF.js.
|
|
93
|
+
|
|
89
94
|
```ts
|
|
90
95
|
// Before using any other method, define the PDF.js module
|
|
91
96
|
// if you need another PDF.js build
|
|
92
|
-
import { configureUnPDF } from
|
|
97
|
+
import { configureUnPDF } from 'unpdf'
|
|
93
98
|
|
|
94
99
|
await configureUnPDF({
|
|
95
100
|
// Use the official PDF.js build (make sure to install it first)
|
|
96
|
-
pdfjs: () => import(
|
|
97
|
-
})
|
|
101
|
+
pdfjs: () => import('pdfjs-dist'),
|
|
102
|
+
})
|
|
98
103
|
|
|
99
104
|
// Now, you can use the other methods
|
|
100
105
|
// …
|
|
@@ -113,7 +118,7 @@ interface UnPDFConfiguration {
|
|
|
113
118
|
* // Use the official PDF.js build (make sure to install it first)
|
|
114
119
|
* () => import('pdfjs-dist')
|
|
115
120
|
*/
|
|
116
|
-
pdfjs?: () => Promise<PDFJS
|
|
121
|
+
pdfjs?: () => Promise<PDFJS>
|
|
117
122
|
}
|
|
118
123
|
```
|
|
119
124
|
|
|
@@ -124,7 +129,7 @@ interface UnPDFConfiguration {
|
|
|
124
129
|
Define a custom PDF.js module, like the legacy build. Make sure to call this method before using any other methods.
|
|
125
130
|
|
|
126
131
|
```ts
|
|
127
|
-
function configureUnPDF(config: UnPDFConfiguration): Promise<void
|
|
132
|
+
function configureUnPDF(config: UnPDFConfiguration): Promise<void>
|
|
128
133
|
```
|
|
129
134
|
|
|
130
135
|
### `getResolvedPDFJS`
|
|
@@ -132,18 +137,18 @@ function configureUnPDF(config: UnPDFConfiguration): Promise<void>;
|
|
|
132
137
|
Returns the resolved PDF.js module. If no build is defined, the latest version will be initialized.
|
|
133
138
|
|
|
134
139
|
```ts
|
|
135
|
-
function getResolvedPDFJS(): Promise<PDFJS
|
|
140
|
+
function getResolvedPDFJS(): Promise<PDFJS>
|
|
136
141
|
```
|
|
137
142
|
|
|
138
143
|
### `getMeta`
|
|
139
144
|
|
|
140
145
|
```ts
|
|
141
146
|
function getMeta(
|
|
142
|
-
data: DocumentInitParameters[
|
|
147
|
+
data: DocumentInitParameters['data'] | PDFDocumentProxy,
|
|
143
148
|
): Promise<{
|
|
144
|
-
info: Record<string, any
|
|
145
|
-
metadata: Record<string, any
|
|
146
|
-
}
|
|
149
|
+
info: Record<string, any>
|
|
150
|
+
metadata: Record<string, any>
|
|
151
|
+
}>
|
|
147
152
|
```
|
|
148
153
|
|
|
149
154
|
### `extractText`
|
|
@@ -152,12 +157,12 @@ Extracts all text from a PDF. If `mergePages` is set to `true`, the text of all
|
|
|
152
157
|
|
|
153
158
|
```ts
|
|
154
159
|
function extractText(
|
|
155
|
-
data: DocumentInitParameters[
|
|
160
|
+
data: DocumentInitParameters['data'] | PDFDocumentProxy,
|
|
156
161
|
{ mergePages }?: { mergePages?: boolean },
|
|
157
162
|
): Promise<{
|
|
158
|
-
totalPages: number
|
|
159
|
-
text: string | string[]
|
|
160
|
-
}
|
|
163
|
+
totalPages: number
|
|
164
|
+
text: string | string[]
|
|
165
|
+
}>
|
|
161
166
|
```
|
|
162
167
|
|
|
163
168
|
### `renderPageAsImage`
|
|
@@ -175,37 +180,37 @@ In order to use this method, you have to meet the following requirements:
|
|
|
175
180
|
**Example**
|
|
176
181
|
|
|
177
182
|
```ts
|
|
178
|
-
import { configureUnPDF, renderPageAsImage } from
|
|
183
|
+
import { configureUnPDF, renderPageAsImage } from 'unpdf'
|
|
179
184
|
|
|
180
185
|
await configureUnPDF({
|
|
181
186
|
// Use the official PDF.js build
|
|
182
|
-
pdfjs: () => import(
|
|
183
|
-
})
|
|
187
|
+
pdfjs: () => import('pdfjs-dist'),
|
|
188
|
+
})
|
|
184
189
|
|
|
185
|
-
const pdf = await readFile(
|
|
186
|
-
const buffer = new Uint8Array(pdf)
|
|
187
|
-
const pageNumber = 1
|
|
190
|
+
const pdf = await readFile('./dummy.pdf')
|
|
191
|
+
const buffer = new Uint8Array(pdf)
|
|
192
|
+
const pageNumber = 1
|
|
188
193
|
|
|
189
194
|
const result = await renderPageAsImage(buffer, pageNumber, {
|
|
190
|
-
canvas: () => import(
|
|
191
|
-
})
|
|
192
|
-
await writeFile(
|
|
195
|
+
canvas: () => import('canvas'),
|
|
196
|
+
})
|
|
197
|
+
await writeFile('dummy-page-1.png', result)
|
|
193
198
|
```
|
|
194
199
|
|
|
195
200
|
**Type Declaration**
|
|
196
201
|
|
|
197
202
|
```ts
|
|
198
203
|
declare function renderPageAsImage(
|
|
199
|
-
data: DocumentInitParameters[
|
|
204
|
+
data: DocumentInitParameters['data'],
|
|
200
205
|
pageNumber: number,
|
|
201
206
|
options?: {
|
|
202
|
-
canvas?: () => Promise<typeof import(
|
|
207
|
+
canvas?: () => Promise<typeof import('canvas')>
|
|
203
208
|
/** @default 1 */
|
|
204
|
-
scale?: number
|
|
205
|
-
width?: number
|
|
206
|
-
height?: number
|
|
209
|
+
scale?: number
|
|
210
|
+
width?: number
|
|
211
|
+
height?: number
|
|
207
212
|
},
|
|
208
|
-
): Promise<ArrayBuffer
|
|
213
|
+
): Promise<ArrayBuffer>
|
|
209
214
|
```
|
|
210
215
|
|
|
211
216
|
## FAQ
|
package/dist/index.cjs
CHANGED
|
@@ -31,19 +31,17 @@ async function resolvePDFJSImports(pdfjsResolver, { force = false } = {}) {
|
|
|
31
31
|
resolvedModule = await resolvedModule.resolvePDFJS();
|
|
32
32
|
}
|
|
33
33
|
return;
|
|
34
|
-
} catch {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
);
|
|
34
|
+
} catch (error) {
|
|
35
|
+
console.error(error);
|
|
36
|
+
throw new Error("Resolving failed. Please check the provided configuration.");
|
|
38
37
|
}
|
|
39
38
|
}
|
|
40
39
|
try {
|
|
41
40
|
const { resolvePDFJS } = await import('unpdf/pdfjs');
|
|
42
41
|
resolvedModule = await resolvePDFJS();
|
|
43
|
-
} catch {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
);
|
|
42
|
+
} catch (error) {
|
|
43
|
+
console.error(error);
|
|
44
|
+
throw new Error("PDF.js is not available. Please add the package as a dependency.");
|
|
47
45
|
}
|
|
48
46
|
}
|
|
49
47
|
function isPDFDocumentProxy(data) {
|
|
@@ -54,39 +52,11 @@ async function interopDefault(m) {
|
|
|
54
52
|
return resolved.default || resolved;
|
|
55
53
|
}
|
|
56
54
|
|
|
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
55
|
async function extractImages$1(data, pageNumber) {
|
|
84
56
|
const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
|
|
85
57
|
const page = await pdf.getPage(pageNumber);
|
|
86
58
|
if (pageNumber < 1 || pageNumber > pdf.numPages) {
|
|
87
|
-
throw new Error(
|
|
88
|
-
`Invalid page number. Must be between 1 and ${pdf.numPages}.`
|
|
89
|
-
);
|
|
59
|
+
throw new Error(`Invalid page number. Must be between 1 and ${pdf.numPages}.`);
|
|
90
60
|
}
|
|
91
61
|
const operatorList = await page.getOperatorList();
|
|
92
62
|
const { OPS } = await getResolvedPDFJS();
|
|
@@ -107,9 +77,7 @@ async function renderPageAsImage$1(data, pageNumber, options = {}) {
|
|
|
107
77
|
const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data, { canvasFactory });
|
|
108
78
|
const page = await pdf.getPage(pageNumber);
|
|
109
79
|
if (pageNumber < 1 || pageNumber > pdf.numPages) {
|
|
110
|
-
throw new Error(
|
|
111
|
-
`Invalid page number. Must be between 1 and ${pdf.numPages}.`
|
|
112
|
-
);
|
|
80
|
+
throw new Error(`Invalid page number. Must be between 1 and ${pdf.numPages}.`);
|
|
113
81
|
}
|
|
114
82
|
let outputScale = options.scale || 1;
|
|
115
83
|
let viewport = page.getViewport({ scale: outputScale });
|
|
@@ -175,6 +143,32 @@ async function createIsomorphicCanvasFactory(canvas) {
|
|
|
175
143
|
};
|
|
176
144
|
}
|
|
177
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
|
+
|
|
178
172
|
async function configureUnPDF(options) {
|
|
179
173
|
const { pdfjs } = { ...options };
|
|
180
174
|
if (pdfjs) {
|
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
|
|
7
|
-
|
|
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[
|
|
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
|
|
7
|
-
|
|
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[
|
|
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
|
|
7
|
-
|
|
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[
|
|
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
|
@@ -29,19 +29,17 @@ async function resolvePDFJSImports(pdfjsResolver, { force = false } = {}) {
|
|
|
29
29
|
resolvedModule = await resolvedModule.resolvePDFJS();
|
|
30
30
|
}
|
|
31
31
|
return;
|
|
32
|
-
} catch {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
);
|
|
32
|
+
} catch (error) {
|
|
33
|
+
console.error(error);
|
|
34
|
+
throw new Error("Resolving failed. Please check the provided configuration.");
|
|
36
35
|
}
|
|
37
36
|
}
|
|
38
37
|
try {
|
|
39
38
|
const { resolvePDFJS } = await import('unpdf/pdfjs');
|
|
40
39
|
resolvedModule = await resolvePDFJS();
|
|
41
|
-
} catch {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
);
|
|
40
|
+
} catch (error) {
|
|
41
|
+
console.error(error);
|
|
42
|
+
throw new Error("PDF.js is not available. Please add the package as a dependency.");
|
|
45
43
|
}
|
|
46
44
|
}
|
|
47
45
|
function isPDFDocumentProxy(data) {
|
|
@@ -52,39 +50,11 @@ async function interopDefault(m) {
|
|
|
52
50
|
return resolved.default || resolved;
|
|
53
51
|
}
|
|
54
52
|
|
|
55
|
-
async function getMeta$1(data) {
|
|
56
|
-
const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
|
|
57
|
-
const meta = await pdf.getMetadata();
|
|
58
|
-
return {
|
|
59
|
-
info: meta?.info ?? {},
|
|
60
|
-
metadata: meta?.metadata?.getAll() ?? {}
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
async function extractText$1(data, options = {}) {
|
|
65
|
-
const { mergePages = false } = { ...options };
|
|
66
|
-
const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
|
|
67
|
-
const texts = await Promise.all(
|
|
68
|
-
Array.from({ length: pdf.numPages }, (_, i) => getPageText(pdf, i + 1))
|
|
69
|
-
);
|
|
70
|
-
return {
|
|
71
|
-
totalPages: pdf.numPages,
|
|
72
|
-
text: mergePages ? texts.join("\n").replace(/\s+/g, " ") : texts
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
async function getPageText(document, pageNumber) {
|
|
76
|
-
const page = await document.getPage(pageNumber);
|
|
77
|
-
const content = await page.getTextContent();
|
|
78
|
-
return content.items.filter((item) => item.str != null).map((item) => item.str + (item.hasEOL ? "\n" : "")).join("");
|
|
79
|
-
}
|
|
80
|
-
|
|
81
53
|
async function extractImages$1(data, pageNumber) {
|
|
82
54
|
const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data);
|
|
83
55
|
const page = await pdf.getPage(pageNumber);
|
|
84
56
|
if (pageNumber < 1 || pageNumber > pdf.numPages) {
|
|
85
|
-
throw new Error(
|
|
86
|
-
`Invalid page number. Must be between 1 and ${pdf.numPages}.`
|
|
87
|
-
);
|
|
57
|
+
throw new Error(`Invalid page number. Must be between 1 and ${pdf.numPages}.`);
|
|
88
58
|
}
|
|
89
59
|
const operatorList = await page.getOperatorList();
|
|
90
60
|
const { OPS } = await getResolvedPDFJS();
|
|
@@ -105,9 +75,7 @@ async function renderPageAsImage$1(data, pageNumber, options = {}) {
|
|
|
105
75
|
const pdf = isPDFDocumentProxy(data) ? data : await getDocumentProxy(data, { canvasFactory });
|
|
106
76
|
const page = await pdf.getPage(pageNumber);
|
|
107
77
|
if (pageNumber < 1 || pageNumber > pdf.numPages) {
|
|
108
|
-
throw new Error(
|
|
109
|
-
`Invalid page number. Must be between 1 and ${pdf.numPages}.`
|
|
110
|
-
);
|
|
78
|
+
throw new Error(`Invalid page number. Must be between 1 and ${pdf.numPages}.`);
|
|
111
79
|
}
|
|
112
80
|
let outputScale = options.scale || 1;
|
|
113
81
|
let viewport = page.getViewport({ scale: outputScale });
|
|
@@ -173,6 +141,32 @@ async function createIsomorphicCanvasFactory(canvas) {
|
|
|
173
141
|
};
|
|
174
142
|
}
|
|
175
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
|
+
|
|
176
170
|
async function configureUnPDF(options) {
|
|
177
171
|
const { pdfjs } = { ...options };
|
|
178
172
|
if (pdfjs) {
|