unpdf 1.4.0 → 1.5.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 +22 -38
- package/dist/index.d.cts +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/pdfjs.mjs +107 -1
- package/dist/types/src/display/annotation_layer.d.ts +14 -11
- package/dist/types/src/display/annotation_storage.d.ts +8 -8
- package/dist/types/src/display/api.d.ts +81 -57
- package/dist/types/src/display/api_utils.d.ts +1 -1
- package/dist/types/src/display/binary_data_factory.d.ts +26 -0
- package/dist/types/src/display/canvas.d.ts +19 -16
- package/dist/types/src/display/canvas_dependency_tracker.d.ts +66 -10
- package/dist/types/src/display/canvas_factory.d.ts +3 -1
- package/dist/types/src/display/display_utils.d.ts +4 -3
- package/dist/types/src/display/draw_layer.d.ts +2 -6
- package/dist/types/src/display/editor/alt_text.d.ts +1 -1
- package/dist/types/src/display/editor/annotation_editor_layer.d.ts +7 -12
- package/dist/types/src/display/editor/color_picker.d.ts +2 -2
- package/dist/types/src/display/editor/comment.d.ts +12 -0
- package/dist/types/src/display/editor/draw.d.ts +3 -7
- package/dist/types/src/display/editor/drawers/freedraw.d.ts +3 -3
- package/dist/types/src/display/editor/drawers/signaturedraw.d.ts +13 -13
- package/dist/types/src/display/editor/editor.d.ts +16 -13
- package/dist/types/src/display/editor/freetext.d.ts +2 -2
- package/dist/types/src/display/editor/highlight.d.ts +3 -3
- package/dist/types/src/display/editor/toolbar.d.ts +2 -2
- package/dist/types/src/display/editor/tools.d.ts +37 -1
- package/dist/types/src/display/fetch_stream.d.ts +3 -56
- package/dist/types/src/display/font_loader.d.ts +2 -1
- package/dist/types/src/display/network.d.ts +14 -80
- package/dist/types/src/display/network_stream.d.ts +2 -0
- package/dist/types/src/display/network_utils.d.ts +3 -3
- package/dist/types/src/display/node_stream.d.ts +2 -52
- package/dist/types/src/display/node_utils.d.ts +5 -19
- package/dist/types/src/{shared/obj-bin-transform.d.ts → display/obj_bin_transform_display.d.ts} +21 -18
- package/dist/types/src/display/pages_mapper.d.ts +72 -0
- package/dist/types/src/display/pattern_helper.d.ts +7 -2
- package/dist/types/src/display/text_layer.d.ts +16 -10
- package/dist/types/src/display/text_layer_images.d.ts +10 -0
- package/dist/types/src/display/transport_stream.d.ts +8 -64
- package/dist/types/src/display/webgpu_mesh.d.ts +3 -0
- package/dist/types/src/display/worker_options.d.ts +2 -2
- package/dist/types/src/display/xfa_layer.d.ts +3 -3
- package/dist/types/src/pdf.d.ts +5 -1
- package/dist/types/src/{interfaces.d.ts → shared/base_pdf_stream.d.ts} +26 -27
- package/dist/types/src/shared/obj_bin_transform_utils.d.ts +26 -0
- package/dist/types/src/shared/util.d.ts +14 -5
- package/dist/types/web/annotation_editor_layer_builder.d.ts +7 -8
- package/dist/types/web/annotation_layer_builder.d.ts +9 -9
- package/dist/types/web/app_options.d.ts +1 -1
- package/dist/types/web/autolinker.d.ts +3 -2
- package/dist/types/web/base_download_manager.d.ts +24 -0
- package/dist/types/web/base_pdf_page_view.d.ts +5 -5
- package/dist/types/web/comment_manager.d.ts +2 -2
- package/dist/types/web/download_manager.d.ts +3 -12
- package/dist/types/web/draw_layer_builder.d.ts +0 -12
- package/dist/types/web/genericl10n.d.ts +6 -10
- package/dist/types/web/l10n.d.ts +3 -6
- package/dist/types/web/menu.d.ts +11 -0
- package/dist/types/web/pdf_find_controller.d.ts +9 -11
- package/dist/types/web/pdf_history.d.ts +3 -3
- package/dist/types/web/pdf_link_service.d.ts +2 -7
- package/dist/types/web/pdf_page_detail_view.d.ts +1 -7
- package/dist/types/web/pdf_page_view.d.ts +17 -11
- package/dist/types/web/pdf_rendering_queue.d.ts +10 -14
- package/dist/types/web/pdf_thumbnail_view.d.ts +18 -42
- package/dist/types/web/pdf_thumbnail_viewer.d.ts +57 -13
- package/dist/types/web/pdf_viewer.component.d.ts +1 -1
- package/dist/types/web/pdf_viewer.d.ts +30 -27
- package/dist/types/web/renderable_view.d.ts +33 -0
- package/dist/types/web/sidebar.d.ts +65 -0
- package/dist/types/web/struct_tree_layer_builder.d.ts +1 -1
- package/dist/types/web/text_accessibility.d.ts +1 -1
- package/dist/types/web/text_layer_builder.d.ts +11 -6
- package/dist/types/web/ui_utils.d.ts +3 -11
- package/dist/types/web/xfa_layer_builder.d.ts +4 -4
- package/package.json +22 -17
- package/dist/types/src/display/cmap_reader_factory.d.ts +0 -25
- package/dist/types/src/display/standard_fontdata_factory.d.ts +0 -20
- package/dist/types/src/display/wasm_factory.d.ts +0 -20
- package/dist/types/web/interfaces.d.ts +0 -174
package/README.md
CHANGED
|
@@ -1,40 +1,25 @@
|
|
|
1
1
|
# unpdf
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Utilities for PDF extraction and rendering across all JavaScript runtimes – Node.js, Deno, Bun, the browser, and serverless environments like Cloudflare Workers. Especially useful for AI applications that need to summarize or analyze PDF documents.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
This library is also intended as a modern alternative to the unmaintained but still popular [`pdf-parse`](https://www.npmjs.com/package/pdf-parse).
|
|
5
|
+
Ships with a serverless build of Mozilla's [PDF.js](https://github.com/mozilla/pdf.js), optimized for edge environments. If you're coming from [`pdf-parse`](https://www.npmjs.com/package/pdf-parse), `unpdf` is a modern, actively maintained alternative with broader runtime support.
|
|
8
6
|
|
|
9
7
|
## Features
|
|
10
8
|
|
|
11
|
-
- 🏗️
|
|
9
|
+
- 🏗️ Works in Node.js, browser and serverless environments
|
|
12
10
|
- 🪭 Includes serverless build of PDF.js ([`unpdf/pdfjs`](./package.json#L34))
|
|
13
11
|
- 💬 Extract [text](#extract-text-from-pdf), [links](#extractlinks), and [images](#extractimages) from PDF files
|
|
14
12
|
- 🧠 Perfect for AI applications and PDF summarization
|
|
15
|
-
- 🧱 Opt-in to legacy PDF.js build
|
|
16
|
-
- 💨 Zero dependencies
|
|
17
|
-
|
|
18
|
-
## PDF.js Compatibility
|
|
19
|
-
|
|
20
|
-
> [!Tip]
|
|
21
|
-
> The serverless PDF.js bundle provided by `unpdf` is built from PDF.js v5.4.296.
|
|
22
|
-
|
|
23
|
-
You can use an [official PDF.js build](#official-or-legacy-pdfjs-build) by using the [`definePDFJSModule`](#definepdfjsmodule) method. This is useful if you want to use a specific version or a custom build of PDF.js.
|
|
13
|
+
- 🧱 Opt-in to official or legacy PDF.js build
|
|
24
14
|
|
|
25
15
|
## Installation
|
|
26
16
|
|
|
27
|
-
Run the following command to add `unpdf` to your project.
|
|
28
|
-
|
|
29
17
|
```bash
|
|
30
18
|
# pnpm
|
|
31
|
-
pnpm add
|
|
19
|
+
pnpm add unpdf
|
|
32
20
|
|
|
33
21
|
# npm
|
|
34
|
-
npm install
|
|
35
|
-
|
|
36
|
-
# yarn
|
|
37
|
-
yarn add -D unpdf
|
|
22
|
+
npm install unpdf
|
|
38
23
|
```
|
|
39
24
|
|
|
40
25
|
## Usage
|
|
@@ -44,15 +29,11 @@ yarn add -D unpdf
|
|
|
44
29
|
```ts
|
|
45
30
|
import { extractText, getDocumentProxy } from 'unpdf'
|
|
46
31
|
|
|
47
|
-
//
|
|
32
|
+
// Fetch a PDF from the web or load it from the file system
|
|
48
33
|
const buffer = await fetch('https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf')
|
|
49
34
|
.then(res => res.arrayBuffer())
|
|
50
|
-
const buffer = await readFile('./dummy.pdf')
|
|
51
35
|
|
|
52
|
-
// Then, load the PDF file into a PDF.js document
|
|
53
36
|
const pdf = await getDocumentProxy(new Uint8Array(buffer))
|
|
54
|
-
|
|
55
|
-
// Finally, extract the text from the PDF file
|
|
56
37
|
const { totalPages, text } = await extractText(pdf, { mergePages: true })
|
|
57
38
|
|
|
58
39
|
console.log(`Total pages: ${totalPages}`)
|
|
@@ -64,9 +45,9 @@ console.log(text)
|
|
|
64
45
|
Usually 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.
|
|
65
46
|
|
|
66
47
|
> [!WARNING]
|
|
67
|
-
> PDF.js v5.x uses `Promise.withResolvers`, which may not be supported in all environments, such as Node < 22. Consider
|
|
48
|
+
> PDF.js v5.x uses `Promise.withResolvers`, which may not be supported in all environments, such as Node < 22. Consider using the bundled serverless build, which includes a polyfill, or use an older version of PDF.js.
|
|
68
49
|
|
|
69
|
-
For example, if you want to use the official PDF.js build
|
|
50
|
+
For example, if you want to use the official PDF.js build:
|
|
70
51
|
|
|
71
52
|
```ts
|
|
72
53
|
import { definePDFJSModule, extractText, getDocumentProxy } from 'unpdf'
|
|
@@ -107,6 +88,17 @@ const document = await getDocument(new Uint8Array(data)).promise
|
|
|
107
88
|
console.log(await document.getMetadata())
|
|
108
89
|
```
|
|
109
90
|
|
|
91
|
+
## How It Works
|
|
92
|
+
|
|
93
|
+
> [!NOTE]
|
|
94
|
+
> The serverless PDF.js bundle is built from PDF.js v5.6.205.
|
|
95
|
+
|
|
96
|
+
Heart and soul of this package is the [`pdfjs.rollup.config.ts`](./pdfjs.rollup.config.ts) file. It uses [Rollup](https://rollupjs.org/) to bundle PDF.js into a single file for serverless environments. The key techniques:
|
|
97
|
+
|
|
98
|
+
- **String replacements** strip browser-specific references from the PDF.js source.
|
|
99
|
+
- **Worker inlining** embeds the PDF.js worker directly into the main bundle, since serverless runtimes can't load separate worker files.
|
|
100
|
+
- **Global polyfills** provide missing APIs like `FinalizationRegistry` (unavailable in Cloudflare Workers).
|
|
101
|
+
|
|
110
102
|
## API
|
|
111
103
|
|
|
112
104
|
### `definePDFJSModule`
|
|
@@ -209,15 +201,7 @@ for (const link of links) console.log(link)
|
|
|
209
201
|
|
|
210
202
|
### `extractImages`
|
|
211
203
|
|
|
212
|
-
Extracts images from a specific page of a PDF document, including necessary metadata such as width, height, and calculated color channels.
|
|
213
|
-
|
|
214
|
-
> [!NOTE]
|
|
215
|
-
> This method will only work in Node.js and browser environments.
|
|
216
|
-
|
|
217
|
-
In order to use this method, make sure to meet the following requirements:
|
|
218
|
-
|
|
219
|
-
- Use the official PDF.js build (see below for details).
|
|
220
|
-
- Install the [`@napi-rs/canvas`](https://github.com/Brooooooklyn/canvas) package if you are using Node.js. This package is required to render the PDF page as an image.
|
|
204
|
+
Extracts images from a specific page of a PDF document, including necessary metadata such as width, height, and calculated color channels. Works with both the serverless and official PDF.js build.
|
|
221
205
|
|
|
222
206
|
**Type Declaration**
|
|
223
207
|
|
|
@@ -285,7 +269,7 @@ To render a PDF page as an image, you can use the `renderPageAsImage` method. Th
|
|
|
285
269
|
|
|
286
270
|
In order to use this method, make sure to meet the following requirements:
|
|
287
271
|
|
|
288
|
-
- Use the official PDF.js build (see
|
|
272
|
+
- Use the official PDF.js build (see [Official or Legacy PDF.js Build](#official-or-legacy-pdfjs-build)).
|
|
289
273
|
- Install the [`@napi-rs/canvas`](https://github.com/Brooooooklyn/canvas) package if you are using Node.js. This package is required to render the PDF page as an image.
|
|
290
274
|
|
|
291
275
|
**Type Declaration**
|
package/dist/index.d.cts
CHANGED
|
@@ -125,7 +125,7 @@ declare function extractText$1(data: DocumentInitParameters['data'] | PDFDocumen
|
|
|
125
125
|
* await definePDFJSModule(() => import('pdfjs-dist'))
|
|
126
126
|
*/
|
|
127
127
|
declare function definePDFJSModule(pdfjs: () => Promise<any>): Promise<void>;
|
|
128
|
-
/** @deprecated Use `definePDFJSModule` instead. */
|
|
128
|
+
/** @deprecated Use `definePDFJSModule` instead. Will be removed in v2. */
|
|
129
129
|
declare function configureUnPDF(options: {
|
|
130
130
|
pdfjs?: () => Promise<any>;
|
|
131
131
|
}): Promise<void>;
|
package/dist/index.d.mts
CHANGED
|
@@ -125,7 +125,7 @@ declare function extractText$1(data: DocumentInitParameters['data'] | PDFDocumen
|
|
|
125
125
|
* await definePDFJSModule(() => import('pdfjs-dist'))
|
|
126
126
|
*/
|
|
127
127
|
declare function definePDFJSModule(pdfjs: () => Promise<any>): Promise<void>;
|
|
128
|
-
/** @deprecated Use `definePDFJSModule` instead. */
|
|
128
|
+
/** @deprecated Use `definePDFJSModule` instead. Will be removed in v2. */
|
|
129
129
|
declare function configureUnPDF(options: {
|
|
130
130
|
pdfjs?: () => Promise<any>;
|
|
131
131
|
}): Promise<void>;
|
package/dist/index.d.ts
CHANGED
|
@@ -125,7 +125,7 @@ declare function extractText$1(data: DocumentInitParameters['data'] | PDFDocumen
|
|
|
125
125
|
* await definePDFJSModule(() => import('pdfjs-dist'))
|
|
126
126
|
*/
|
|
127
127
|
declare function definePDFJSModule(pdfjs: () => Promise<any>): Promise<void>;
|
|
128
|
-
/** @deprecated Use `definePDFJSModule` instead. */
|
|
128
|
+
/** @deprecated Use `definePDFJSModule` instead. Will be removed in v2. */
|
|
129
129
|
declare function configureUnPDF(options: {
|
|
130
130
|
pdfjs?: () => Promise<any>;
|
|
131
131
|
}): Promise<void>;
|