unpdf 0.7.1 → 0.9.0
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 +39 -46
- package/dist/index.cjs +6 -1
- package/dist/index.d.cts +5 -5
- package/dist/index.d.mts +5 -5
- package/dist/index.d.ts +5 -5
- package/dist/index.mjs +6 -1
- package/dist/pdfjs.d.mts +3 -1
- package/dist/pdfjs.d.ts +3 -1
- package/dist/pdfjs.mjs +1 -1
- package/dist/types/src/display/annotation_layer.d.ts +14 -7
- package/dist/types/src/display/annotation_storage.d.ts +4 -4
- package/dist/types/src/display/api.d.ts +14 -9
- package/dist/types/src/display/content_disposition.d.ts +7 -0
- package/dist/types/src/display/display_utils.d.ts +2 -5
- package/dist/types/src/display/editor/annotation_editor_layer.d.ts +13 -8
- package/dist/types/src/display/editor/editor.d.ts +51 -15
- package/dist/types/src/display/editor/freetext.d.ts +2 -9
- package/dist/types/src/display/editor/ink.d.ts +6 -12
- package/dist/types/src/display/editor/stamp.d.ts +5 -1
- package/dist/types/src/display/editor/tools.d.ts +15 -4
- package/dist/types/src/display/fetch_stream.d.ts +68 -0
- package/dist/types/src/display/font_loader.d.ts +4 -1
- package/dist/types/src/display/network.d.ts +83 -0
- package/dist/types/src/display/network_utils.d.ts +14 -0
- package/dist/types/src/display/node_stream.d.ts +71 -0
- package/dist/types/src/display/node_utils.d.ts +27 -0
- package/dist/types/src/display/transport_stream.d.ts +3 -0
- package/dist/types/src/display/xfa_layer.d.ts +2 -1
- package/dist/types/src/display/xfa_text.d.ts +2 -0
- package/dist/types/src/interfaces.d.ts +118 -0
- package/dist/types/src/pdf.d.ts +3 -3
- package/dist/types/src/shared/util.d.ts +1 -0
- package/dist/types/web/annotation_editor_layer_builder.d.ts +8 -5
- package/dist/types/web/annotation_layer_builder.d.ts +12 -17
- package/dist/types/web/app_options.d.ts +2 -1
- package/dist/types/web/download_manager.d.ts +1 -1
- package/dist/types/web/generic_scripting.d.ts +1 -1
- package/dist/types/web/genericl10n.d.ts +14 -7
- package/dist/types/web/interfaces.d.ts +16 -8
- package/dist/types/web/l10n.d.ts +27 -0
- package/dist/types/web/l10n_utils.d.ts +7 -5
- package/dist/types/web/pdf_find_controller.d.ts +2 -1
- package/dist/types/web/pdf_link_service.d.ts +1 -1
- package/dist/types/web/pdf_page_view.d.ts +9 -7
- package/dist/types/web/pdf_thumbnail_view.d.ts +8 -15
- package/dist/types/web/pdf_thumbnail_viewer.d.ts +2 -8
- package/dist/types/web/pdf_viewer.d.ts +20 -14
- package/dist/types/web/text_accessibility.d.ts +5 -3
- package/dist/types/web/text_layer_builder.d.ts +7 -2
- package/dist/types/web/ui_utils.d.ts +5 -9
- package/dist/types/web/xfa_layer_builder.d.ts +4 -6
- package/package.json +21 -18
- package/dist/types/external/webL10n/l10n.d.ts +0 -0
package/README.md
CHANGED
|
@@ -9,11 +9,16 @@ This library is also intended as a modern alternative to the unmaintained but st
|
|
|
9
9
|
## Features
|
|
10
10
|
|
|
11
11
|
- 🏗️ Works in Node.js, browser and workers
|
|
12
|
-
- 🪭 Includes serverless build of PDF.js ([`unpdf/pdfjs`](./package.json#
|
|
12
|
+
- 🪭 Includes serverless build of PDF.js ([`unpdf/pdfjs`](./package.json#L34))
|
|
13
13
|
- 💬 Extract text and images from PDFs
|
|
14
14
|
- 🧱 Opt-in to legacy PDF.js build
|
|
15
15
|
- 💨 Zero dependencies
|
|
16
16
|
|
|
17
|
+
## PDF.js Compatibility
|
|
18
|
+
|
|
19
|
+
> [!NOTE]
|
|
20
|
+
> This package is currently using PDF.js v4.0.189.
|
|
21
|
+
|
|
17
22
|
## Installation
|
|
18
23
|
|
|
19
24
|
Run the following command to add `unpdf` to your project.
|
|
@@ -50,40 +55,20 @@ const pdf = await getDocumentProxy(new Uint8Array(pdf));
|
|
|
50
55
|
const { totalPages, text } = await extractText(pdf, { mergePages: true });
|
|
51
56
|
```
|
|
52
57
|
|
|
53
|
-
###
|
|
54
|
-
|
|
55
|
-
Generally, 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.
|
|
56
|
-
|
|
57
|
-
```ts
|
|
58
|
-
// Before using any other methods, define the PDF.js module
|
|
59
|
-
import { defineUnPDFConfig } from "unpdf";
|
|
60
|
-
|
|
61
|
-
defineUnPDFConfig({
|
|
62
|
-
// Use the legacy build
|
|
63
|
-
pdfjs: () => import("pdfjs-dist/legacy/build/pdf.js"),
|
|
64
|
-
});
|
|
58
|
+
### Access the PDF.js API
|
|
65
59
|
|
|
66
|
-
|
|
67
|
-
// …
|
|
68
|
-
```
|
|
60
|
+
This will return the resolved PDF.js module and gives full access to the PDF.js API, like:
|
|
69
61
|
|
|
70
|
-
|
|
62
|
+
- `getDocument`
|
|
63
|
+
- `version`
|
|
64
|
+
- … and all other methods
|
|
71
65
|
|
|
72
|
-
|
|
66
|
+
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.
|
|
73
67
|
|
|
74
68
|
```ts
|
|
75
69
|
import { getResolvedPDFJS } from "unpdf";
|
|
76
70
|
|
|
77
|
-
const {
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
### Use Serverless PDF.js Build In 🦕 Deno
|
|
81
|
-
|
|
82
|
-
Instead of using the methods provided by `unpdf`, you can directly import the serverless PDF.js build in Deno. This is useful if you want to use the PDF.js API directly.
|
|
83
|
-
|
|
84
|
-
```ts
|
|
85
|
-
import { getDocument } from "https://esm.sh/unpdf/pdfjs";
|
|
86
|
-
|
|
71
|
+
const { getDocument } = await getResolvedPDFJS();
|
|
87
72
|
const data = Deno.readFileSync("dummy.pdf");
|
|
88
73
|
const doc = await getDocument(data).promise;
|
|
89
74
|
|
|
@@ -97,18 +82,35 @@ for (let i = 1; i <= doc.numPages; i++) {
|
|
|
97
82
|
}
|
|
98
83
|
```
|
|
99
84
|
|
|
85
|
+
### Use Official or Legacy PDF.js Build
|
|
86
|
+
|
|
87
|
+
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
|
+
|
|
89
|
+
```ts
|
|
90
|
+
// Before using any other methods, define the PDF.js module
|
|
91
|
+
import { defineUnPDFConfig } from "unpdf";
|
|
92
|
+
|
|
93
|
+
defineUnPDFConfig({
|
|
94
|
+
// Use the official PDF.js build (make sure to install it first)
|
|
95
|
+
pdfjs: () => import("pdfjs-dist"),
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
// Now, you can use the other methods
|
|
99
|
+
// …
|
|
100
|
+
```
|
|
101
|
+
|
|
100
102
|
## Config
|
|
101
103
|
|
|
102
104
|
```ts
|
|
103
105
|
interface UnPDFConfiguration {
|
|
104
106
|
/**
|
|
105
|
-
* By default, UnPDF will use the latest version of PDF.js
|
|
106
|
-
*
|
|
107
|
-
*
|
|
107
|
+
* By default, UnPDF will use the latest version of PDF.js compiled for
|
|
108
|
+
* serverless environments. If you want to use a different version, you can
|
|
109
|
+
* provide a custom resolver function.
|
|
108
110
|
*
|
|
109
111
|
* @example
|
|
110
|
-
* // Use the
|
|
111
|
-
* () => import('pdfjs-dist
|
|
112
|
+
* // Use the official PDF.js build (make sure to install it first)
|
|
113
|
+
* () => import('pdfjs-dist')
|
|
112
114
|
*/
|
|
113
115
|
pdfjs?: () => Promise<PDFJS>;
|
|
114
116
|
}
|
|
@@ -194,31 +196,22 @@ declare function renderPageAsImage(
|
|
|
194
196
|
data: BinaryData | PDFDocumentProxy,
|
|
195
197
|
pageNumber: number,
|
|
196
198
|
options?: {
|
|
197
|
-
canvas?: () => Promise<typeof "canvas">;
|
|
199
|
+
canvas?: () => Promise<typeof import("canvas")>;
|
|
198
200
|
/** @default 1 */
|
|
199
|
-
scale?: number
|
|
201
|
+
scale?: number;
|
|
200
202
|
width?: number;
|
|
201
203
|
height?: number;
|
|
202
204
|
},
|
|
203
205
|
): Promise<ArrayBuffer>;
|
|
204
206
|
```
|
|
205
207
|
|
|
206
|
-
### `extractImages`
|
|
207
|
-
|
|
208
|
-
```ts
|
|
209
|
-
function extractImages(
|
|
210
|
-
data: BinaryData | PDFDocumentProxy,
|
|
211
|
-
pageNumber: number,
|
|
212
|
-
): Promise<Uint8ClampedArray[]>;
|
|
213
|
-
```
|
|
214
|
-
|
|
215
208
|
## FAQ
|
|
216
209
|
|
|
217
|
-
### Why Is `canvas`
|
|
210
|
+
### Why Is `canvas` An Optional Dependency?
|
|
218
211
|
|
|
219
212
|
The official PDF.js library depends on the `canvas` module for Node.js environments, which [doesn't work inside worker threads](https://github.com/Automattic/node-canvas/issues/1394). That's why `unpdf` ships with a serverless build of PDF.js that mocks the `canvas` module.
|
|
220
213
|
|
|
221
|
-
However, to render PDF pages as images, you need to install the `canvas` module
|
|
214
|
+
However, to render PDF pages as images in Node.js environments, you need to install the `canvas` module. That's why it is a peer dependency.
|
|
222
215
|
|
|
223
216
|
## License
|
|
224
217
|
|
package/dist/index.cjs
CHANGED
|
@@ -25,6 +25,10 @@ async function resolvePDFJSImports(pdfjsResolver, { force = false } = {}) {
|
|
|
25
25
|
if (pdfjsResolver) {
|
|
26
26
|
try {
|
|
27
27
|
const _import = await pdfjsResolver();
|
|
28
|
+
if ("resolvePDFJS" in _import) {
|
|
29
|
+
resolvedModule = await _import.resolvePDFJS();
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
28
32
|
resolvedModule = _import.default || _import;
|
|
29
33
|
return;
|
|
30
34
|
} catch {
|
|
@@ -34,7 +38,8 @@ async function resolvePDFJSImports(pdfjsResolver, { force = false } = {}) {
|
|
|
34
38
|
}
|
|
35
39
|
}
|
|
36
40
|
try {
|
|
37
|
-
|
|
41
|
+
const { resolvePDFJS } = await import('unpdf/pdfjs');
|
|
42
|
+
resolvedModule = await resolvePDFJS();
|
|
38
43
|
} catch {
|
|
39
44
|
throw new Error(
|
|
40
45
|
"PDF.js is not available. Please add the package as a dependency."
|
package/dist/index.d.cts
CHANGED
|
@@ -26,13 +26,13 @@ declare function renderPageAsImage$1(data: BinaryData | PDFDocumentProxy, pageNu
|
|
|
26
26
|
type PDFJS = typeof _PDFJS;
|
|
27
27
|
interface UnPDFConfiguration {
|
|
28
28
|
/**
|
|
29
|
-
* By default, UnPDF will use the latest version of PDF.js
|
|
30
|
-
*
|
|
31
|
-
*
|
|
29
|
+
* By default, UnPDF will use the latest version of PDF.js compiled for
|
|
30
|
+
* serverless environments. If you want to use a different version, you can
|
|
31
|
+
* provide a custom resolver function.
|
|
32
32
|
*
|
|
33
33
|
* @example
|
|
34
|
-
* // Use the
|
|
35
|
-
* () => import('pdfjs-dist
|
|
34
|
+
* // Use the official PDF.js build (make sure to install it first)
|
|
35
|
+
* () => import('pdfjs-dist')
|
|
36
36
|
*/
|
|
37
37
|
pdfjs?: () => Promise<any>;
|
|
38
38
|
}
|
package/dist/index.d.mts
CHANGED
|
@@ -26,13 +26,13 @@ declare function renderPageAsImage$1(data: BinaryData | PDFDocumentProxy, pageNu
|
|
|
26
26
|
type PDFJS = typeof _PDFJS;
|
|
27
27
|
interface UnPDFConfiguration {
|
|
28
28
|
/**
|
|
29
|
-
* By default, UnPDF will use the latest version of PDF.js
|
|
30
|
-
*
|
|
31
|
-
*
|
|
29
|
+
* By default, UnPDF will use the latest version of PDF.js compiled for
|
|
30
|
+
* serverless environments. If you want to use a different version, you can
|
|
31
|
+
* provide a custom resolver function.
|
|
32
32
|
*
|
|
33
33
|
* @example
|
|
34
|
-
* // Use the
|
|
35
|
-
* () => import('pdfjs-dist
|
|
34
|
+
* // Use the official PDF.js build (make sure to install it first)
|
|
35
|
+
* () => import('pdfjs-dist')
|
|
36
36
|
*/
|
|
37
37
|
pdfjs?: () => Promise<any>;
|
|
38
38
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -26,13 +26,13 @@ declare function renderPageAsImage$1(data: BinaryData | PDFDocumentProxy, pageNu
|
|
|
26
26
|
type PDFJS = typeof _PDFJS;
|
|
27
27
|
interface UnPDFConfiguration {
|
|
28
28
|
/**
|
|
29
|
-
* By default, UnPDF will use the latest version of PDF.js
|
|
30
|
-
*
|
|
31
|
-
*
|
|
29
|
+
* By default, UnPDF will use the latest version of PDF.js compiled for
|
|
30
|
+
* serverless environments. If you want to use a different version, you can
|
|
31
|
+
* provide a custom resolver function.
|
|
32
32
|
*
|
|
33
33
|
* @example
|
|
34
|
-
* // Use the
|
|
35
|
-
* () => import('pdfjs-dist
|
|
34
|
+
* // Use the official PDF.js build (make sure to install it first)
|
|
35
|
+
* () => import('pdfjs-dist')
|
|
36
36
|
*/
|
|
37
37
|
pdfjs?: () => Promise<any>;
|
|
38
38
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -23,6 +23,10 @@ async function resolvePDFJSImports(pdfjsResolver, { force = false } = {}) {
|
|
|
23
23
|
if (pdfjsResolver) {
|
|
24
24
|
try {
|
|
25
25
|
const _import = await pdfjsResolver();
|
|
26
|
+
if ("resolvePDFJS" in _import) {
|
|
27
|
+
resolvedModule = await _import.resolvePDFJS();
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
26
30
|
resolvedModule = _import.default || _import;
|
|
27
31
|
return;
|
|
28
32
|
} catch {
|
|
@@ -32,7 +36,8 @@ async function resolvePDFJSImports(pdfjsResolver, { force = false } = {}) {
|
|
|
32
36
|
}
|
|
33
37
|
}
|
|
34
38
|
try {
|
|
35
|
-
|
|
39
|
+
const { resolvePDFJS } = await import('unpdf/pdfjs');
|
|
40
|
+
resolvedModule = await resolvePDFJS();
|
|
36
41
|
} catch {
|
|
37
42
|
throw new Error(
|
|
38
43
|
"PDF.js is not available. Please add the package as a dependency."
|
package/dist/pdfjs.d.mts
CHANGED
package/dist/pdfjs.d.ts
CHANGED