unpdf 2.0.1 → 2.1.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 +10 -2
- package/dist/index.d.mts +13 -1
- package/dist/index.mjs +25 -3
- package/dist/pdfjs-decoders-workerd.mjs +1 -0
- package/dist/pdfjs-decoders.mjs +1 -1
- package/dist/pdfjs-loader.mjs +1 -0
- package/dist/pdfjs.mjs +1 -1
- package/package.json +7 -1
package/README.md
CHANGED
|
@@ -104,7 +104,7 @@ PDF.js does not execute scripts embedded in PDFs, but resource limits are your j
|
|
|
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
|
|
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.4.299/cmaps/'`, `cMapPacked: true`, and `useWorkerFetch: true`. Your options override the defaults.
|
|
108
108
|
|
|
109
109
|
```ts
|
|
110
110
|
function getDocumentProxy(data: Uint8Array | ArrayBuffer, options?: DocumentInitParameters): Promise<PDFDocumentProxy>
|
|
@@ -190,6 +190,14 @@ Returns the resolved PDF.js module – the serverless build, unless `definePDFJS
|
|
|
190
190
|
function getResolvedPDFJS(): Promise<PDFJS>
|
|
191
191
|
```
|
|
192
192
|
|
|
193
|
+
### `registerImageDecoders`
|
|
194
|
+
|
|
195
|
+
Registers PDF.js' JBIG2 and OpenJPEG decoders, which CCITT, JBIG2, and JPEG 2000 images need, with the serverless PDF.js build. `extractImages`, `renderPageAsImage`, and `inspectPages` call it themselves – call it before decoding these images through PDF.js directly, such as on the pages of a `getDocumentProxy` document. Another build set through `definePDFJSModule`, such as `pdfjs-dist`, ignores it and loads its decoders from `wasmUrl`, which [`getDocumentProxy`](#getdocumentproxy) sets in Node.js.
|
|
196
|
+
|
|
197
|
+
```ts
|
|
198
|
+
function registerImageDecoders(): void
|
|
199
|
+
```
|
|
200
|
+
|
|
193
201
|
## Custom PDF.js Build
|
|
194
202
|
|
|
195
203
|
`unpdf` uses its serverless build unless you define another one, e.g. to pin a different PDF.js version. Install `pdfjs-dist` and call `definePDFJSModule` before any other method:
|
|
@@ -211,7 +219,7 @@ Heart and soul of this package is the [`pdfjs.rolldown.config.ts`](./pdfjs.rolld
|
|
|
211
219
|
|
|
212
220
|
- **String replacements** strip browser-specific references from the PDF.js source.
|
|
213
221
|
- **Worker inlining** embeds the PDF.js worker directly into the main bundle, since serverless runtimes can't load separate worker files.
|
|
214
|
-
- **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` or a `wasmUrl`.
|
|
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` or a `wasmUrl`. `extractImages`, `renderPageAsImage`, and `inspectPages` load them, so a bundle that only extracts text leaves them out. PDF.js called directly, through `getResolvedPDFJS` or `unpdf/pdfjs`, skips these images with warnings until [`registerImageDecoders`](#registerimagedecoders) has run. Bundlers that resolve the `workerd` condition, like Wrangler, ship only the JS fallbacks, since workerd can't compile WASM at runtime.
|
|
215
223
|
- **Global polyfills** fill in newer APIs like `Promise.try` and `Map.prototype.getOrInsert` for runtimes that lack them, plus `FinalizationRegistry` for Cloudflare Workers.
|
|
216
224
|
|
|
217
225
|
## License
|
package/dist/index.d.mts
CHANGED
|
@@ -63,6 +63,17 @@ export declare function renderPageAsImage(data: DocumentInitParameters["data"] |
|
|
|
63
63
|
toDataURL: true;
|
|
64
64
|
}): Promise<string>;
|
|
65
65
|
export declare function renderPageAsImage(data: DocumentInitParameters["data"] | PDFDocumentProxy, pageNumber: number, options?: RenderPageAsImageOptions): Promise<Uint8Array | string>;
|
|
66
|
+
/**
|
|
67
|
+
* Registers PDF.js' JBIG2 and OpenJPEG decoders, which CCITT, JBIG2, and JPEG
|
|
68
|
+
* 2000 images need, with the serverless PDF.js build. `extractImages`,
|
|
69
|
+
* `renderPageAsImage`, and `inspectPages` call this themselves, so a bundle that
|
|
70
|
+
* only extracts text leaves the decoders out. Call it before decoding these
|
|
71
|
+
* images through PDF.js directly; the decoders load on the first such image.
|
|
72
|
+
* Another build set through `definePDFJSModule`, such as `pdfjs-dist`, ignores
|
|
73
|
+
* it and loads its decoders from `wasmUrl`, which `getDocumentProxy` sets in
|
|
74
|
+
* Node.js.
|
|
75
|
+
*/
|
|
76
|
+
export declare function registerImageDecoders(): void;
|
|
66
77
|
interface PageInspection {
|
|
67
78
|
/** Characters of all text on the page, visible or not, whitespace excluded. */
|
|
68
79
|
textLength: number;
|
|
@@ -166,7 +177,8 @@ export declare function extractText(data: DocumentInitParameters["data"] | PDFDo
|
|
|
166
177
|
/**
|
|
167
178
|
* Creates a `PDFDocumentProxy` from binary PDF data, with `useSystemFonts:
|
|
168
179
|
* true`. In Node.js with `pdfjs-dist` installed, it uses that package's
|
|
169
|
-
* standard fonts instead of system fonts
|
|
180
|
+
* standard fonts instead of system fonts and its packed CMaps. A PDF.js build
|
|
181
|
+
* other than the serverless one also gets that package's image decoders.
|
|
170
182
|
*/
|
|
171
183
|
export declare function getDocumentProxy(data: DocumentInitParameters["data"], options?: DocumentInitParameters): Promise<PDFDocumentProxy>;
|
|
172
184
|
/**
|
package/dist/index.mjs
CHANGED
|
@@ -50,7 +50,8 @@ const isNode = isNodeProcess(globalThis.process);
|
|
|
50
50
|
/**
|
|
51
51
|
* Creates a `PDFDocumentProxy` from binary PDF data, with `useSystemFonts:
|
|
52
52
|
* true`. In Node.js with `pdfjs-dist` installed, it uses that package's
|
|
53
|
-
* standard fonts instead of system fonts
|
|
53
|
+
* standard fonts instead of system fonts and its packed CMaps. A PDF.js build
|
|
54
|
+
* other than the serverless one also gets that package's image decoders.
|
|
54
55
|
*/
|
|
55
56
|
async function getDocumentProxy(data, options = {}) {
|
|
56
57
|
const { getDocument } = await getResolvedPDFJS();
|
|
@@ -64,7 +65,8 @@ async function getDocumentProxy(data, options = {}) {
|
|
|
64
65
|
useSystemFonts: false,
|
|
65
66
|
standardFontDataUrl: `${join(pdfjsDir, "standard_fonts")}/`,
|
|
66
67
|
cMapUrl: `${join(pdfjsDir, "cmaps")}/`,
|
|
67
|
-
cMapPacked: true
|
|
68
|
+
cMapPacked: true,
|
|
69
|
+
wasmUrl: `${join(pdfjsDir, "wasm")}/`
|
|
68
70
|
};
|
|
69
71
|
} catch {}
|
|
70
72
|
return await getDocument({
|
|
@@ -348,6 +350,23 @@ function createCRCTable() {
|
|
|
348
350
|
}
|
|
349
351
|
return table;
|
|
350
352
|
}
|
|
353
|
+
// src/image-decoders.ts
|
|
354
|
+
/**
|
|
355
|
+
* Registers PDF.js' JBIG2 and OpenJPEG decoders, which CCITT, JBIG2, and JPEG
|
|
356
|
+
* 2000 images need, with the serverless PDF.js build. `extractImages`,
|
|
357
|
+
* `renderPageAsImage`, and `inspectPages` call this themselves, so a bundle that
|
|
358
|
+
* only extracts text leaves the decoders out. Call it before decoding these
|
|
359
|
+
* images through PDF.js directly; the decoders load on the first such image.
|
|
360
|
+
* Another build set through `definePDFJSModule`, such as `pdfjs-dist`, ignores
|
|
361
|
+
* it and loads its decoders from `wasmUrl`, which `getDocumentProxy` sets in
|
|
362
|
+
* Node.js.
|
|
363
|
+
*/
|
|
364
|
+
function registerImageDecoders() {
|
|
365
|
+
globalThis[Symbol.for("unpdf.loadImageDecoder")] ??= async (fileName) => {
|
|
366
|
+
const { loadDecoder } = await import("#pdfjs-decoders");
|
|
367
|
+
return await loadDecoder(fileName);
|
|
368
|
+
};
|
|
369
|
+
}
|
|
351
370
|
// src/image.ts
|
|
352
371
|
/**
|
|
353
372
|
* Extracts the images painted on a page – image XObjects, inline images, and
|
|
@@ -362,6 +381,7 @@ function createCRCTable() {
|
|
|
362
381
|
* some images out as bitmaps instead of pixels, which are skipped.
|
|
363
382
|
*/
|
|
364
383
|
async function extractImages(data, pageNumber, options = {}) {
|
|
384
|
+
registerImageDecoders();
|
|
365
385
|
return await withDocument(data, async (pdf) => {
|
|
366
386
|
if (pageNumber < 1 || pageNumber > pdf.numPages) throw new Error(`Invalid page number. Must be between 1 and ${pdf.numPages}.`);
|
|
367
387
|
const page = await pdf.getPage(pageNumber);
|
|
@@ -398,6 +418,7 @@ async function extractImages(data, pageNumber, options = {}) {
|
|
|
398
418
|
});
|
|
399
419
|
}
|
|
400
420
|
async function renderPageAsImage(data, pageNumber, options = {}) {
|
|
421
|
+
registerImageDecoders();
|
|
401
422
|
await prepareCanvasEnvironment(options.canvasImport);
|
|
402
423
|
return await withDocument(data, async (pdf) => {
|
|
403
424
|
if (pageNumber < 1 || pageNumber > pdf.numPages) throw new Error(`Invalid page number. Must be between 1 and ${pdf.numPages}.`);
|
|
@@ -494,6 +515,7 @@ const WHITESPACE_PATTERN = /\s/g;
|
|
|
494
515
|
* from born-digital ones. Where that line lies is left to the caller.
|
|
495
516
|
*/
|
|
496
517
|
async function inspectPages(data) {
|
|
518
|
+
registerImageDecoders();
|
|
497
519
|
return await withDocument(data, async (pdf) => {
|
|
498
520
|
const { OPS } = await getResolvedPDFJS();
|
|
499
521
|
const optionalContentConfig = await pdf.getOptionalContentConfig();
|
|
@@ -660,4 +682,4 @@ async function getPageText(document, pageNumber) {
|
|
|
660
682
|
function normalizeMergedText(texts) {
|
|
661
683
|
return texts.join("\n").replace(/[^\S\n]+/g, " ").replace(/ ?\n ?/g, "\n").replace(/\n{3,}/g, "\n\n");
|
|
662
684
|
}
|
|
663
|
-
export { definePDFJSModule, extractImages, extractLinks, extractText, extractTextItems, getDocumentProxy, getMeta, getResolvedPDFJS, inspectPages, renderPageAsImage };
|
|
685
|
+
export { definePDFJSModule, extractImages, extractLinks, extractText, extractTextItems, getDocumentProxy, getMeta, getResolvedPDFJS, inspectPages, registerImageDecoders, renderPageAsImage };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{t as e}from"./pdfjs-loader.mjs";const t=e({"jbig2_nowasm_fallback.js":()=>import(`./pdfjs-jbig2_nowasm_fallback.mjs`),"openjpeg_nowasm_fallback.js":()=>import(`./pdfjs-openjpeg_nowasm_fallback.mjs`)});export{t as loadDecoder};
|
package/dist/pdfjs-decoders.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const e
|
|
1
|
+
import{t as e}from"./pdfjs-loader.mjs";const t=e({"jbig2.wasm":()=>import(`./pdfjs-jbig2.mjs`),"jbig2_nowasm_fallback.js":()=>import(`./pdfjs-jbig2_nowasm_fallback.mjs`),"openjpeg.wasm":()=>import(`./pdfjs-openjpeg.mjs`),"openjpeg_nowasm_fallback.js":()=>import(`./pdfjs-openjpeg_nowasm_fallback.mjs`)});export{t as loadDecoder};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function createDecoderLoader(e){return async t=>{if(!Object.hasOwn(e,t))throw Error(`No bundled decoder for ${t}`);let{default:n}=await e[t]();return n}}export{createDecoderLoader as t};
|