pdfjs-serverless 1.3.0 → 2.0.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 +52 -38
- package/dist/image-decoders.d.mts +1 -0
- package/dist/image-decoders.mjs +1 -0
- package/dist/index.d.mts +3 -3
- package/dist/index.mjs +41 -40
- package/dist/licenses/LICENSE_JBIG2 +196 -0
- package/dist/licenses/LICENSE_OPENJPEG +39 -0
- package/dist/licenses/LICENSE_PDFJS +177 -0
- package/dist/licenses/LICENSE_PDFJS_JBIG2 +13 -0
- package/dist/licenses/LICENSE_PDFJS_OPENJPEG +22 -0
- package/dist/pdfjs-decoders-workerd.mjs +1 -0
- package/dist/pdfjs-decoders.mjs +1 -0
- package/dist/pdfjs-jbig2.mjs +1 -0
- package/dist/pdfjs-jbig2_nowasm_fallback.mjs +1 -0
- package/dist/pdfjs-loader.mjs +1 -0
- package/dist/pdfjs-openjpeg.mjs +1 -0
- package/dist/pdfjs-openjpeg_nowasm_fallback.mjs +2 -0
- package/dist/pdfjs-rolldown-runtime.mjs +1 -0
- package/dist/types/external/qcms/qcms.d.ts +11 -7
- package/dist/types/external/qcms/qcms_utils.d.ts +1 -6
- package/dist/types/src/core/base_stream.d.ts +8 -1
- package/dist/types/src/core/catalog.d.ts +12 -12
- package/dist/types/src/core/colorspace.d.ts +6 -15
- package/dist/types/src/core/core_utils.d.ts +18 -14
- package/dist/types/src/core/crypto.d.ts +0 -1
- package/dist/types/src/core/file_spec.d.ts +4 -9
- package/dist/types/src/core/icc_colorspace.d.ts +0 -1
- package/dist/types/src/core/image_resizer.d.ts +24 -0
- package/dist/types/src/core/image_utils.d.ts +4 -4
- package/dist/types/src/core/jpeg_stream.d.ts +1 -1
- package/dist/types/src/core/jpg.d.ts +9 -13
- package/dist/types/src/core/parser.d.ts +0 -2
- package/dist/types/src/core/pattern.d.ts +0 -2
- package/dist/types/src/core/primitives.d.ts +30 -16
- package/dist/types/src/core/sasl_prep.d.ts +1 -0
- package/dist/types/src/core/sound.d.ts +39 -0
- package/dist/types/src/core/string_utils.d.ts +1 -1
- package/dist/types/src/core/struct_tree.d.ts +3 -3
- package/dist/types/src/core/xml_parser.d.ts +1 -2
- package/dist/types/src/core/xref.d.ts +2 -15
- package/dist/types/src/display/annotation_layer.d.ts +11 -22
- package/dist/types/src/display/annotation_storage.d.ts +7 -7
- package/dist/types/src/display/api.d.ts +155 -108
- package/dist/types/src/display/canvas.d.ts +2 -1
- package/dist/types/src/display/canvas_dependency_tracker.d.ts +8 -3
- package/dist/types/src/display/content_disposition.d.ts +0 -1
- package/dist/types/src/display/display_utils.d.ts +2 -2
- package/dist/types/src/display/draw_layer.d.ts +2 -6
- package/dist/types/src/display/editor/annotation_editor_layer.d.ts +18 -19
- package/dist/types/src/display/editor/color_picker.d.ts +1 -1
- package/dist/types/src/display/editor/comment.d.ts +1 -1
- package/dist/types/src/display/editor/draw.d.ts +78 -27
- package/dist/types/src/display/editor/drawers/freedraw.d.ts +11 -9
- package/dist/types/src/display/editor/drawers/highlight.d.ts +99 -15
- package/dist/types/src/display/editor/drawers/inkdraw.d.ts +7 -2
- package/dist/types/src/display/editor/drawers/outline.d.ts +65 -2
- package/dist/types/src/display/editor/editor.d.ts +24 -22
- package/dist/types/src/display/editor/freetext.d.ts +1 -1
- package/dist/types/src/display/editor/highlight.d.ts +44 -35
- package/dist/types/src/display/editor/ink.d.ts +7 -2
- package/dist/types/src/display/editor/stamp.d.ts +1 -1
- package/dist/types/src/display/editor/tools.d.ts +21 -19
- package/dist/types/src/display/filter_factory.d.ts +1 -3
- package/dist/types/src/display/font_loader.d.ts +1 -15
- package/dist/types/src/display/network_utils.d.ts +1 -0
- package/dist/types/src/display/obj_bin_transform_display.d.ts +25 -38
- package/dist/types/src/display/page_viewport.d.ts +5 -5
- package/dist/types/src/display/pages_mapper.d.ts +10 -4
- package/dist/types/src/display/pattern_helper.d.ts +4 -3
- package/dist/types/src/display/pdf_objects.d.ts +1 -3
- package/dist/types/src/display/text_layer.d.ts +6 -4
- package/dist/types/src/display/touch_manager.d.ts +46 -9
- package/dist/types/src/display/worker_options.d.ts +2 -2
- package/dist/types/src/display/xfa_layer.d.ts +9 -14
- package/dist/types/src/display/xfa_text.d.ts +4 -7
- package/dist/types/src/pdf.d.ts +8 -7
- package/dist/types/src/shared/base_pdf_stream.d.ts +6 -7
- package/dist/types/src/shared/css_utils.d.ts +8 -0
- package/dist/types/src/shared/message_handler.d.ts +2 -11
- package/dist/types/src/shared/obj_bin_transform_utils.d.ts +4 -3
- package/dist/types/src/shared/util.d.ts +4 -6
- package/dist/types/web/annotation_editor_layer_builder.d.ts +10 -8
- package/dist/types/web/annotation_layer_builder.d.ts +17 -21
- package/dist/types/web/draw_layer_builder.d.ts +7 -9
- package/dist/types/web/event_utils.d.ts +12 -13
- package/dist/types/web/genericl10n.d.ts +2 -2
- package/dist/types/web/l10n.d.ts +1 -1
- package/dist/types/web/pdf_find_controller.d.ts +9 -10
- package/dist/types/web/pdf_find_utils.d.ts +8 -14
- package/dist/types/web/pdf_history.d.ts +2 -2
- package/dist/types/web/pdf_link_service.d.ts +7 -9
- package/dist/types/web/pdf_page_view.d.ts +15 -15
- package/dist/types/web/pdf_rendering_queue.d.ts +7 -8
- package/dist/types/web/pdf_scripting_manager.d.ts +5 -5
- package/dist/types/web/pdf_thumbnail_view.d.ts +11 -11
- package/dist/types/web/pdf_thumbnail_viewer.d.ts +21 -22
- package/dist/types/web/pdf_viewer.d.ts +49 -29
- package/dist/types/web/renderable_view.d.ts +3 -3
- package/dist/types/web/sidebar.d.ts +3 -3
- package/dist/types/web/struct_tree_layer_builder.d.ts +21 -7
- package/dist/types/web/text_accessibility.d.ts +4 -2
- package/dist/types/web/text_highlighter.d.ts +7 -8
- package/dist/types/web/text_layer_builder.d.ts +11 -11
- package/dist/types/web/ui_utils.d.ts +8 -13
- package/dist/types/web/xfa_layer_builder.d.ts +13 -13
- package/package.json +27 -16
package/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# pdfjs-serverless
|
|
2
2
|
|
|
3
|
-
A redistribution of Mozilla's [PDF.js](https://github.com/mozilla/pdf.js) as a
|
|
3
|
+
A redistribution of Mozilla's [PDF.js](https://github.com/mozilla/pdf.js) as a bundle for edge and serverless runtimes, like Cloudflare Workers. Use it as a drop-in replacement for `pdfjs-dist` to parse PDF documents and extract text content – no extra dependencies needed.
|
|
4
4
|
|
|
5
|
-
The
|
|
5
|
+
The main bundle is about 1.7 MB (minified) and leaves the image decoders to an [opt-in import](#decoding-ccitt-jbig2-and-jpeg-2000-images). It ships the complete PDF.js type declarations and is tested in Node.js 24+, Bun, and Cloudflare Workers.
|
|
6
6
|
|
|
7
7
|
## Installation
|
|
8
8
|
|
|
@@ -19,7 +19,7 @@ npm install pdfjs-serverless
|
|
|
19
19
|
> [!TIP]
|
|
20
20
|
> For common operations, such as extracting text content or images from PDF files, you can use the [`unpdf` package](https://github.com/unjs/unpdf). It is a wrapper around `pdfjs-serverless` and provides a simple API for common use cases.
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
Swap the import from `pdfjs-dist` to `pdfjs-serverless`:
|
|
23
23
|
|
|
24
24
|
```diff
|
|
25
25
|
- import { getDocument } from 'pdfjs-dist'
|
|
@@ -36,54 +36,68 @@ export default {
|
|
|
36
36
|
if (request.method !== 'POST')
|
|
37
37
|
return new Response('Method Not Allowed', { status: 405 })
|
|
38
38
|
|
|
39
|
-
|
|
40
|
-
const
|
|
39
|
+
const data = new Uint8Array(await request.arrayBuffer())
|
|
40
|
+
const document = await getDocument({ data, useSystemFonts: true }).promise
|
|
41
41
|
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
// Get metadata and initialize output object
|
|
48
|
-
const metadata = await document.getMetadata()
|
|
49
|
-
const output = {
|
|
50
|
-
metadata,
|
|
51
|
-
pages: []
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// Iterate through each page and fetch the text content
|
|
55
|
-
for (let i = 1; i <= document.numPages; i++) {
|
|
56
|
-
const page = await document.getPage(i)
|
|
57
|
-
const textContent = await page.getTextContent()
|
|
58
|
-
const contents = textContent.items.map(item => item.str).join(' ')
|
|
59
|
-
|
|
60
|
-
// Add page content to output
|
|
61
|
-
output.pages.push({
|
|
62
|
-
pageNumber: i,
|
|
63
|
-
content: contents
|
|
64
|
-
})
|
|
42
|
+
const pages = []
|
|
43
|
+
for (let pageNumber = 1; pageNumber <= document.numPages; pageNumber++) {
|
|
44
|
+
const page = await document.getPage(pageNumber)
|
|
45
|
+
const { items } = await page.getTextContent()
|
|
46
|
+
pages.push({ pageNumber, content: items.map(item => item.str).join(' ') })
|
|
65
47
|
}
|
|
66
48
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
headers: { 'Content-Type': 'application/json' }
|
|
70
|
-
})
|
|
71
|
-
}
|
|
49
|
+
return Response.json({ metadata: await document.getMetadata(), pages })
|
|
50
|
+
},
|
|
72
51
|
}
|
|
73
52
|
```
|
|
74
53
|
|
|
75
|
-
|
|
54
|
+
A ready-to-run Cloudflare Workers example lives in [`examples/cloudflare`](./examples/cloudflare).
|
|
55
|
+
|
|
56
|
+
### Rendering Pages
|
|
57
|
+
|
|
58
|
+
Rendering needs a canvas implementation, which serverless runtimes like Cloudflare Workers lack. In Node.js, hand [`@napi-rs/canvas`](https://github.com/Brooooooklyn/canvas) to the bundle through a well-known global symbol and expose its `Path2D`:
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
import { writeFile } from 'node:fs/promises'
|
|
62
|
+
import * as canvas from '@napi-rs/canvas'
|
|
63
|
+
import { getDocument } from 'pdfjs-serverless'
|
|
64
|
+
import 'pdfjs-serverless/image-decoders'
|
|
65
|
+
|
|
66
|
+
globalThis[Symbol.for('pdfjs-serverless.canvasModule')] = canvas
|
|
67
|
+
globalThis.Path2D ??= canvas.Path2D
|
|
68
|
+
|
|
69
|
+
const document = await getDocument({ data }).promise
|
|
70
|
+
const page = await document.getPage(1)
|
|
71
|
+
const viewport = page.getViewport({ scale: 2 })
|
|
72
|
+
const target = canvas.createCanvas(viewport.width, viewport.height)
|
|
73
|
+
|
|
74
|
+
await page.render({ canvas: target, canvasContext: target.getContext('2d'), viewport }).promise
|
|
75
|
+
await writeFile('page-1.png', await target.encode('png'))
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Without a canvas module, rendering throws a descriptive error.
|
|
79
|
+
|
|
80
|
+
### Decoding CCITT, JBIG2, and JPEG 2000 Images
|
|
81
|
+
|
|
82
|
+
Rendering a page or reading its image data needs PDF.js' JBIG2 and OpenJPEG decoders for images in these formats. Text extraction doesn't, so your bundle leaves them out unless you import them once:
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
import 'pdfjs-serverless/image-decoders'
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
The decoders then load on the first such image. Without the import, PDF.js skips these images and warns that the import is missing.
|
|
76
89
|
|
|
77
90
|
## How It Works
|
|
78
91
|
|
|
79
92
|
> [!NOTE]
|
|
80
|
-
> `pdfjs-serverless` is
|
|
93
|
+
> `pdfjs-serverless` is built from PDF.js v6.4.299. It requires the global `Iterator` constructor (Safari 18.4+); without it, importing the bundle throws.
|
|
81
94
|
|
|
82
|
-
Heart and soul of this package is the [`rolldown.config.ts`](./rolldown.config.ts) file. It uses [Rolldown](https://rolldown.rs/) to bundle PDF.js
|
|
95
|
+
Heart and soul of this package is the [`rolldown.config.ts`](./rolldown.config.ts) file. It uses [Rolldown](https://rolldown.rs/) to bundle PDF.js for serverless environments. The key techniques:
|
|
83
96
|
|
|
84
|
-
- **String replacements** rewrite the environment detection: `isNodeJS` becomes `typeof window === "undefined"`, so serverless runtimes take the Node.js code paths – paradoxical, but it unlocks the right branches. The `@napi-rs/canvas` import is bridged to
|
|
97
|
+
- **String replacements** rewrite the environment detection: `isNodeJS` becomes `typeof window === "undefined"`, so serverless runtimes take the Node.js code paths – paradoxical, but it unlocks the right branches. The `@napi-rs/canvas` import is bridged to a global symbol (see [Rendering Pages](#rendering-pages)).
|
|
85
98
|
- **Worker inlining** embeds the PDF.js worker directly into the main bundle, since serverless runtimes can't load separate worker files.
|
|
86
|
-
- **
|
|
99
|
+
- **Bundled image decoders** ship PDF.js' JBIG2 and OpenJPEG decoders, WASM and JS fallback, as lazy chunks behind [`pdfjs-serverless/image-decoders`](#decoding-ccitt-jbig2-and-jpeg-2000-images), so CCITT, JBIG2, and JPEG 2000 images decode without `pdfjs-dist` or a `wasmUrl`. Where the runtime can't compile WASM, as in Cloudflare Workers, the bundle turns `useWasm` off and the JS decoders take over. Bundlers that resolve the `workerd` condition, like Wrangler, ship only the JS fallbacks.
|
|
100
|
+
- **Mocks and polyfills** provide missing globals: `DOMMatrix`, `navigator` and `FinalizationRegistry` are stubbed in [`src/mocks.mjs`](./src/mocks.mjs); `Promise.try`, `Map.prototype.getOrInsert`, `Uint8Array.prototype.toHex` and friends are polyfilled in [`src/polyfills.mjs`](./src/polyfills.mjs).
|
|
87
101
|
|
|
88
102
|
## Inspiration
|
|
89
103
|
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
globalThis[Symbol.for(`pdfjs-serverless.loadImageDecoder`)]??=async e=>{let{loadDecoder:t}=await import(`#pdfjs-decoders`);return await t(e)};
|
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import * as PDFJS from './types/src/pdf'
|
|
1
|
+
import * as PDFJS from './types/src/pdf.js'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* @deprecated Import from `pdfjs-serverless` instead. Will be removed in v2.
|
|
@@ -18,5 +18,5 @@ import * as PDFJS from './types/src/pdf'
|
|
|
18
18
|
declare function resolvePDFJS(): Promise<typeof PDFJS>
|
|
19
19
|
|
|
20
20
|
export { resolvePDFJS }
|
|
21
|
-
export * from './types/src/pdf'
|
|
22
|
-
export type { TextContent, TextItem, TextMarkedContent, TextStyle } from './types/src/display/api'
|
|
21
|
+
export * from './types/src/pdf.js'
|
|
22
|
+
export type { TextContent, TextItem, TextMarkedContent, TextStyle } from './types/src/display/api.js'
|