unpdf 2.1.0 → 2.2.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 +12 -12
- package/dist/index.mjs +15 -13
- package/dist/pdfjs-jbig2_nowasm_fallback.mjs +1 -1
- package/dist/pdfjs-openjpeg.mjs +1 -1
- package/dist/pdfjs-openjpeg_nowasm_fallback.mjs +1 -2
- package/dist/pdfjs.mjs +39 -39
- package/dist/types/src/core/colorspace.d.ts +7 -0
- package/dist/types/src/core/crypto.d.ts +10 -2
- package/dist/types/src/core/flate_stream.d.ts +6 -1
- package/dist/types/src/core/image_resizer.d.ts +1 -1
- package/dist/types/src/core/image_utils.d.ts +10 -0
- package/dist/types/src/core/jpx.d.ts +0 -1
- package/dist/types/src/core/parser.d.ts +8 -1
- package/dist/types/src/core/pattern.d.ts +0 -4
- package/dist/types/src/core/primitives.d.ts +1 -0
- package/dist/types/src/core/struct_tree.d.ts +2 -0
- package/dist/types/src/core/wasm_image.d.ts +3 -3
- package/dist/types/src/display/annotation_layer.d.ts +9 -2
- package/dist/types/src/display/api.d.ts +38 -2
- package/dist/types/src/display/api_utils.d.ts +10 -0
- package/dist/types/src/display/canvas.d.ts +13 -3
- package/dist/types/src/display/canvas_dependency_tracker.d.ts +20 -10
- package/dist/types/src/display/canvas_factory.d.ts +1 -11
- package/dist/types/src/display/display_utils.d.ts +0 -71
- package/dist/types/src/display/dom_canvas_factory.d.ts +12 -0
- package/dist/types/src/display/dom_filter_factory.d.ts +23 -0
- package/dist/types/src/display/dom_utils.d.ts +68 -0
- package/dist/types/src/display/editor/draw.d.ts +2 -0
- package/dist/types/src/display/editor/drawers/inkdraw.d.ts +14 -17
- package/dist/types/src/display/editor/drawers/input_smoother.d.ts +28 -0
- package/dist/types/src/display/editor/drawers/outline.d.ts +2 -2
- package/dist/types/src/display/editor/ink.d.ts +2 -1
- package/dist/types/src/display/editor/tools.d.ts +2 -1
- package/dist/types/src/display/filter_factory.d.ts +0 -22
- package/dist/types/src/display/object_handler.d.ts +21 -0
- package/dist/types/src/display/pdf_objects.d.ts +10 -1
- package/dist/types/src/display/renderer_worker_proxy.d.ts +131 -0
- package/dist/types/src/display/text_layer.d.ts +1 -6
- package/dist/types/src/display/worker_options.d.ts +24 -0
- package/dist/types/src/pdf.d.ts +12 -12
- package/dist/types/src/shared/message_handler.d.ts +1 -0
- package/dist/types/src/shared/util.d.ts +4 -0
- package/dist/types/web/annotation_layer_builder.d.ts +1 -0
- package/dist/types/web/autolinker.d.ts +4 -4
- package/dist/types/web/base_download_manager.d.ts +3 -2
- package/dist/types/web/download_manager.d.ts +1 -0
- package/dist/types/web/pdf_page_view.d.ts +1 -0
- package/dist/types/web/pdf_scripting_manager.d.ts +1 -1
- package/dist/types/web/pdf_thumbnail_viewer.d.ts +7 -1
- package/package.json +27 -24
package/README.md
CHANGED
|
@@ -25,7 +25,7 @@ npm install unpdf
|
|
|
25
25
|
|
|
26
26
|
## Use Cases
|
|
27
27
|
|
|
28
|
-
Every method takes binary PDF data (`Uint8Array`, `ArrayBuffer`) or a document proxy from [`getDocumentProxy`](#getdocumentproxy). Pass a proxy to run several methods on one document without parsing it again.
|
|
28
|
+
Every method takes binary PDF data (`Uint8Array`, `ArrayBuffer`) or a document proxy from [`getDocumentProxy`](#getdocumentproxy). PDF.js takes ownership of binary data, so it works for one call only. Pass a proxy to run several methods on one document without parsing it again.
|
|
29
29
|
|
|
30
30
|
### Extract Text
|
|
31
31
|
|
|
@@ -94,20 +94,20 @@ const scannedPageNumbers = pages
|
|
|
94
94
|
|
|
95
95
|
PDF.js does not execute scripts embedded in PDFs, but resource limits are your job:
|
|
96
96
|
|
|
97
|
-
- **Image decoding:** `maxImageSize` is unlimited by default – pass e.g. `getDocumentProxy(data, { maxImageSize: 16_777_216 })` (~16 MP) so a single declared image
|
|
97
|
+
- **Image decoding:** `maxImageSize` is unlimited by default – pass e.g. `getDocumentProxy(data, { maxImageSize: 16_777_216 })` (~16 MP) so a single declared image cannot allocate gigabytes.
|
|
98
98
|
- **Page fan-out:** `extractText`, `extractTextItems`, `extractLinks`, and `inspectPages` process all pages in one call – check `pdf.numPages` against a limit first.
|
|
99
99
|
- **Timeouts:** with the serverless build, parsing runs on your event loop (no worker) – race extraction calls against a timeout.
|
|
100
100
|
|
|
101
101
|
## API
|
|
102
102
|
|
|
103
|
-
`data` is binary PDF data or a `PDFDocumentProxy` throughout.
|
|
103
|
+
`data` is binary PDF data or a `PDFDocumentProxy` throughout. `pageNumber` counts from 1.
|
|
104
104
|
|
|
105
105
|
### `getDocumentProxy`
|
|
106
106
|
|
|
107
|
-
Creates a `PDFDocumentProxy` with `useSystemFonts: true`. In Node.js with `pdfjs-dist` installed, it uses that package's standard fonts instead of system fonts and its CJK character maps (`cMapUrl`). A PDF.js build other than the serverless one also gets that package's image decoders (`wasmUrl`). Cloudflare Workers have no local `pdfjs-dist` – to extract text from CJK fonts without embedded mappings, pass `cMapUrl: 'https://unpkg.com/pdfjs-dist@6.
|
|
107
|
+
Creates a `PDFDocumentProxy` with `useSystemFonts: true`. In Node.js with `pdfjs-dist` installed, it uses that package's standard fonts instead of system fonts and its CJK character maps (`cMapUrl`). A PDF.js build other than the serverless one also gets that package's image decoders (`wasmUrl`). Cloudflare Workers have no local `pdfjs-dist` – to extract text from CJK fonts without embedded mappings, pass `cMapUrl: 'https://unpkg.com/pdfjs-dist@6.5.438/cmaps/'`, `cMapPacked: true`, and `useWorkerFetch: true`. Your options override the defaults.
|
|
108
108
|
|
|
109
109
|
```ts
|
|
110
|
-
function getDocumentProxy(data: Uint8Array | ArrayBuffer, options?:
|
|
110
|
+
function getDocumentProxy(data: Uint8Array | ArrayBuffer, options?: Parameters<typeof import('unpdf/pdfjs').getDocument>[0]): Promise<PDFDocumentProxy>
|
|
111
111
|
```
|
|
112
112
|
|
|
113
113
|
### `extractText`
|
|
@@ -136,7 +136,7 @@ function extractLinks(data): Promise<{ totalPages: number, links: string[][] }>
|
|
|
136
136
|
|
|
137
137
|
### `extractImages`
|
|
138
138
|
|
|
139
|
-
Extracts the images painted on a page – image XObjects, inline images, and image masks – as RGBA pixels or PNG bytes, each with `width
|
|
139
|
+
Extracts the images painted on a page – image XObjects, inline images, and image masks – as RGBA pixels or PNG bytes, each with its `width` and `height` in pixels and its `bounds` in PDF space.
|
|
140
140
|
|
|
141
141
|
```ts
|
|
142
142
|
function extractImages(data, pageNumber: number, options?: { format?: 'rgba' | 'png' }): Promise<ExtractedImage[]>
|
|
@@ -179,7 +179,7 @@ function getMeta(data, options?: { parseDates?: boolean }): Promise<{ info: PDFI
|
|
|
179
179
|
Sets the PDF.js build every other method uses. It loads on first use.
|
|
180
180
|
|
|
181
181
|
```ts
|
|
182
|
-
function definePDFJSModule(pdfjs: () => Promise<
|
|
182
|
+
function definePDFJSModule(pdfjs: () => Promise<typeof import('unpdf/pdfjs')>): void
|
|
183
183
|
```
|
|
184
184
|
|
|
185
185
|
### `getResolvedPDFJS`
|
|
@@ -187,7 +187,7 @@ function definePDFJSModule(pdfjs: () => Promise<PDFJS>): void
|
|
|
187
187
|
Returns the resolved PDF.js module – the serverless build, unless `definePDFJSModule` set another – to call the PDF.js API directly.
|
|
188
188
|
|
|
189
189
|
```ts
|
|
190
|
-
function getResolvedPDFJS(): Promise<
|
|
190
|
+
function getResolvedPDFJS(): Promise<typeof import('unpdf/pdfjs')>
|
|
191
191
|
```
|
|
192
192
|
|
|
193
193
|
### `registerImageDecoders`
|
|
@@ -213,13 +213,13 @@ In Node.js, import the legacy build as shown. The default `pdfjs-dist` entry tar
|
|
|
213
213
|
## How It Works
|
|
214
214
|
|
|
215
215
|
> [!NOTE]
|
|
216
|
-
> The serverless PDF.js bundle is built from PDF.js v6.
|
|
216
|
+
> The serverless PDF.js bundle is built from PDF.js v6.5.438. It requires the global `Iterator` constructor (Safari 18.4+); without it, importing the bundle throws.
|
|
217
217
|
|
|
218
218
|
Heart and soul of this package is the [`pdfjs.rolldown.config.ts`](./pdfjs.rolldown.config.ts) file. It uses [Rolldown](https://rolldown.rs/) to bundle PDF.js for serverless environments. The key techniques:
|
|
219
219
|
|
|
220
|
-
- **
|
|
221
|
-
- **Worker inlining** embeds the PDF.js worker directly into the main bundle, since serverless runtimes
|
|
222
|
-
- **Bundled image decoders** ship PDF.js' JBIG2 and OpenJPEG decoders, WASM and JS fallback, as lazy chunks, so CCITT, JBIG2, and JPEG 2000 images decode without `pdfjs-dist
|
|
220
|
+
- **Source patches** make PDF.js use what the bundle provides: the inlined worker, the bundled image decoders, and the `@napi-rs/canvas` module passed as `canvasImport`.
|
|
221
|
+
- **Worker inlining** embeds the PDF.js worker directly into the main bundle, since serverless runtimes cannot load separate worker files.
|
|
222
|
+
- **Bundled image decoders** ship PDF.js' JBIG2 and OpenJPEG decoders, WASM and JS fallback, as lazy chunks, so CCITT, JBIG2, and JPEG 2000 images decode without `pdfjs-dist`, and a bundle that only extracts text leaves them out. Bundlers that resolve the `workerd` condition, like Wrangler, ship only the JS fallbacks.
|
|
223
223
|
- **Global polyfills** fill in newer APIs like `Promise.try` and `Map.prototype.getOrInsert` for runtimes that lack them, plus `FinalizationRegistry` for Cloudflare Workers.
|
|
224
224
|
|
|
225
225
|
## License
|
package/dist/index.mjs
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
1
|
// src/_internal/dommatrix-polyfill.ts
|
|
2
|
-
/**
|
|
3
|
-
* Stubs `DOMMatrix`, which PDF.js reads while its modules evaluate, so it must
|
|
4
|
-
* run before any PDF.js module is imported.
|
|
5
|
-
*/
|
|
6
2
|
function polyfillDOMMatrix() {
|
|
7
3
|
if (typeof globalThis.DOMMatrix !== "undefined") return;
|
|
8
4
|
globalThis.DOMMatrix = class DOMMatrix {
|
|
@@ -55,6 +51,7 @@ const isNode = isNodeProcess(globalThis.process);
|
|
|
55
51
|
*/
|
|
56
52
|
async function getDocumentProxy(data, options = {}) {
|
|
57
53
|
const { getDocument } = await getResolvedPDFJS();
|
|
54
|
+
if (globalThis.Buffer?.isBuffer(data)) data = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
|
58
55
|
let nodeDefaults = {};
|
|
59
56
|
if (isNode) try {
|
|
60
57
|
const nodeProcess = globalThis.process;
|
|
@@ -121,9 +118,10 @@ async function interopDefault(m) {
|
|
|
121
118
|
return resolved.default || resolved;
|
|
122
119
|
}
|
|
123
120
|
async function loadPDFJS(loader) {
|
|
124
|
-
polyfillDOMMatrix();
|
|
125
121
|
try {
|
|
126
|
-
|
|
122
|
+
const pdfjs = await interopDefault(loader ? loader() : import("unpdf/pdfjs"));
|
|
123
|
+
polyfillDOMMatrix();
|
|
124
|
+
return pdfjs;
|
|
127
125
|
} catch (error) {
|
|
128
126
|
throw new Error(`PDF.js could not be resolved: ${error}`, { cause: error });
|
|
129
127
|
}
|
|
@@ -162,8 +160,8 @@ async function resolveCanvasModule(canvasImport) {
|
|
|
162
160
|
*
|
|
163
161
|
* @remarks
|
|
164
162
|
* `DOMMatrix` is always set because {@link polyfillDOMMatrix} may have
|
|
165
|
-
* installed a minimal stub
|
|
166
|
-
*
|
|
163
|
+
* installed a minimal stub – the real `@napi-rs/canvas` implementation must
|
|
164
|
+
* take precedence for rendering.
|
|
167
165
|
*/
|
|
168
166
|
function injectCanvasConstructors() {
|
|
169
167
|
if (!resolvedCanvasModule) return;
|
|
@@ -497,7 +495,8 @@ function toRGBA(image, isMask, imageKind) {
|
|
|
497
495
|
return rgba;
|
|
498
496
|
}
|
|
499
497
|
async function prepareCanvasEnvironment(canvasImport) {
|
|
500
|
-
|
|
498
|
+
const isWorkerd = globalThis.navigator?.userAgent === "Cloudflare-Workers";
|
|
499
|
+
if (isNode && !isWorkerd) {
|
|
501
500
|
if (!canvasImport) throw new Error("Parameter \"canvasImport\" is required in Node.js environment.");
|
|
502
501
|
await resolveCanvasModule(canvasImport);
|
|
503
502
|
injectCanvasConstructors();
|
|
@@ -570,7 +569,7 @@ function getCoverage(boxes, view) {
|
|
|
570
569
|
const right = edges[i + 1];
|
|
571
570
|
const spans = clippedBoxes.filter((box) => box.x0 <= left && box.x1 >= right).sort((a, b) => a.y0 - b.y0);
|
|
572
571
|
let coveredHeight = 0;
|
|
573
|
-
let coveredTop = -
|
|
572
|
+
let coveredTop = -1e400;
|
|
574
573
|
for (const { y0, y1 } of spans) {
|
|
575
574
|
const start = Math.max(y0, coveredTop);
|
|
576
575
|
if (y1 > start) {
|
|
@@ -585,7 +584,8 @@ function getCoverage(boxes, view) {
|
|
|
585
584
|
// src/links.ts
|
|
586
585
|
async function extractLinks(data) {
|
|
587
586
|
return await withDocument(data, async (pdf) => {
|
|
588
|
-
const links =
|
|
587
|
+
const links = [];
|
|
588
|
+
for (let pageNumber = 1; pageNumber <= pdf.numPages; pageNumber++) links.push(await getPageLinks(pdf, pageNumber));
|
|
589
589
|
return {
|
|
590
590
|
totalPages: pdf.numPages,
|
|
591
591
|
links
|
|
@@ -637,7 +637,8 @@ function parseISODateString(isoDateString) {
|
|
|
637
637
|
// src/text.ts
|
|
638
638
|
async function extractTextItems(data) {
|
|
639
639
|
return await withDocument(data, async (pdf) => {
|
|
640
|
-
const items =
|
|
640
|
+
const items = [];
|
|
641
|
+
for (let pageNumber = 1; pageNumber <= pdf.numPages; pageNumber++) items.push(await getPageTextItems(pdf, pageNumber));
|
|
641
642
|
return {
|
|
642
643
|
totalPages: pdf.numPages,
|
|
643
644
|
items
|
|
@@ -665,7 +666,8 @@ async function getPageTextItems(document, pageNumber) {
|
|
|
665
666
|
async function extractText(data, options = {}) {
|
|
666
667
|
const { mergePages = false } = options;
|
|
667
668
|
return await withDocument(data, async (pdf) => {
|
|
668
|
-
const texts =
|
|
669
|
+
const texts = [];
|
|
670
|
+
for (let pageNumber = 1; pageNumber <= pdf.numPages; pageNumber++) texts.push(await getPageText(pdf, pageNumber));
|
|
669
671
|
return {
|
|
670
672
|
totalPages: pdf.numPages,
|
|
671
673
|
text: mergePages ? normalizeMergedText(texts) : texts
|