pdfjs-serverless 0.7.0 → 1.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 +66 -82
- package/dist/index.d.mts +17 -0
- package/dist/index.mjs +1 -1
- package/dist/types/src/display/annotation_layer.d.ts +14 -0
- package/dist/types/src/display/annotation_storage.d.ts +1 -8
- package/dist/types/src/display/api.d.ts +80 -34
- package/dist/types/src/display/canvas.d.ts +19 -27
- package/dist/types/src/display/display_utils.d.ts +11 -0
- package/dist/types/src/display/draw_layer.d.ts +2 -1
- package/dist/types/src/display/editor/alt_text.d.ts +1 -1
- package/dist/types/src/display/editor/annotation_editor_layer.d.ts +4 -3
- package/dist/types/src/display/editor/draw.d.ts +2 -1
- package/dist/types/src/display/editor/drawers/contour.d.ts +3 -0
- package/dist/types/src/display/editor/drawers/inkdraw.d.ts +2 -1
- package/dist/types/src/display/editor/drawers/signaturedraw.d.ts +79 -0
- package/dist/types/src/display/editor/editor.d.ts +5 -2
- package/dist/types/src/display/editor/highlight.d.ts +1 -0
- package/dist/types/src/display/editor/ink.d.ts +8 -2
- package/dist/types/src/display/editor/signature.d.ts +80 -0
- package/dist/types/src/display/editor/stamp.d.ts +3 -5
- package/dist/types/src/display/editor/toolbar.d.ts +2 -0
- package/dist/types/src/display/editor/tools.d.ts +5 -7
- package/dist/types/src/display/font_loader.d.ts +3 -8
- package/dist/types/src/display/metadata.d.ts +1 -2
- package/dist/types/src/display/network.d.ts +1 -1
- package/dist/types/src/display/network_utils.d.ts +2 -3
- package/dist/types/src/display/node_utils.d.ts +7 -0
- package/dist/types/src/display/optional_content_config.d.ts +1 -1
- package/dist/types/src/display/pattern_helper.d.ts +4 -2
- package/dist/types/src/display/text_layer.d.ts +9 -9
- package/dist/types/src/display/touch_manager.d.ts +5 -1
- package/dist/types/src/display/wasm_factory.d.ts +20 -0
- package/dist/types/src/display/worker_options.d.ts +2 -2
- package/dist/types/src/pdf.d.ts +9 -3
- package/dist/types/src/shared/message_handler.d.ts +2 -3
- package/dist/types/src/shared/util.d.ts +128 -109
- package/dist/types/web/annotation_editor_layer_builder.d.ts +15 -3
- package/dist/types/web/annotation_layer_builder.d.ts +35 -5
- package/dist/types/web/autolinker.d.ts +28 -0
- package/dist/types/web/base_pdf_page_view.d.ts +27 -0
- package/dist/types/web/draw_layer_builder.d.ts +13 -2
- package/dist/types/web/pdf_find_controller.d.ts +2 -0
- package/dist/types/web/pdf_link_service.d.ts +0 -1
- package/dist/types/web/pdf_page_detail_view.d.ts +32 -0
- package/dist/types/web/pdf_page_view.d.ts +41 -14
- package/dist/types/web/pdf_rendering_queue.d.ts +2 -1
- package/dist/types/web/pdf_thumbnail_view.d.ts +23 -3
- package/dist/types/web/pdf_thumbnail_viewer.d.ts +21 -1
- package/dist/types/web/pdf_viewer.d.ts +31 -0
- package/dist/types/web/struct_tree_layer_builder.d.ts +17 -1
- package/dist/types/web/text_layer_builder.d.ts +25 -17
- package/dist/types/web/xfa_layer_builder.d.ts +14 -3
- package/package.json +13 -21
- package/dist/index.d.ts +0 -4
package/README.md
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
# pdfjs-serverless
|
|
2
2
|
|
|
3
|
-
A redistribution of Mozilla's [PDF.js](https://github.com/mozilla/pdf.js) for
|
|
3
|
+
A redistribution of Mozilla's [PDF.js](https://github.com/mozilla/pdf.js) for edge environments, like Cloudflare Workers. It is especially useful for serverless AI applications, where you want to parse PDF documents and extract text content.
|
|
4
|
+
|
|
5
|
+
This package comes with zero dependencies. The whole export is about 1.4 MB (minified).
|
|
4
6
|
|
|
5
7
|
## PDF.js Compatibility
|
|
6
8
|
|
|
7
9
|
> [!NOTE]
|
|
8
|
-
>
|
|
10
|
+
> `pdfjs-serverless` is currently built from PDF.js v5.2.133.
|
|
9
11
|
|
|
10
12
|
## Installation
|
|
11
13
|
|
|
@@ -13,124 +15,106 @@ Run the following command to add `pdfjs-serverless` to your project.
|
|
|
13
15
|
|
|
14
16
|
```bash
|
|
15
17
|
# pnpm
|
|
16
|
-
pnpm add pdfjs-serverless
|
|
18
|
+
pnpm add -D pdfjs-serverless
|
|
17
19
|
|
|
18
20
|
# npm
|
|
19
|
-
npm install pdfjs-serverless
|
|
21
|
+
npm install -D pdfjs-serverless
|
|
20
22
|
|
|
21
23
|
# yarn
|
|
22
|
-
yarn add pdfjs-serverless
|
|
24
|
+
yarn add -D pdfjs-serverless
|
|
23
25
|
```
|
|
24
26
|
|
|
25
27
|
## Usage
|
|
26
28
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
```ts
|
|
30
|
-
declare function resolvePDFJS(): Promise<typeof PDFJS>
|
|
31
|
-
```
|
|
29
|
+
> [TIP]
|
|
30
|
+
> 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.
|
|
32
31
|
|
|
33
|
-
|
|
32
|
+
`pdfjs-serverless` provides the same API as the original PDF.js library. To use any of the PDF.js exports, rename the import to `pdfjs-serverless` instead of `pdfjs-dist`:
|
|
34
33
|
|
|
35
|
-
```
|
|
36
|
-
|
|
37
|
-
import { getDocument } from 'pdfjs-serverless'
|
|
34
|
+
```diff
|
|
35
|
+
- import { getDocument } from 'pdfjs-dist'
|
|
36
|
+
+ import { getDocument } from 'pdfjs-serverless'
|
|
38
37
|
```
|
|
39
38
|
|
|
40
|
-
|
|
39
|
+
## Examples
|
|
41
40
|
|
|
42
|
-
|
|
43
|
-
import { resolvePDFJS } from 'pdfjs-serverless'
|
|
41
|
+
### 🌩 Cloudflare Workers
|
|
44
42
|
|
|
45
|
-
|
|
43
|
+
```ts
|
|
44
|
+
export default {
|
|
45
|
+
async fetch(request) {
|
|
46
|
+
if (request.method !== 'POST')
|
|
47
|
+
return new Response('Method Not Allowed', { status: 405 })
|
|
48
|
+
|
|
49
|
+
const { getDocument } = await import('pdfjs-serverless')
|
|
50
|
+
|
|
51
|
+
// Get the PDF file from the POST request body as a buffer
|
|
52
|
+
const data = await request.arrayBuffer()
|
|
53
|
+
|
|
54
|
+
const document = await getDocument({
|
|
55
|
+
data: new Uint8Array(data),
|
|
56
|
+
useSystemFonts: true,
|
|
57
|
+
}).promise
|
|
58
|
+
|
|
59
|
+
// Get metadata and initialize output object
|
|
60
|
+
const metadata = await document.getMetadata()
|
|
61
|
+
const output = {
|
|
62
|
+
metadata,
|
|
63
|
+
pages: []
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Iterate through each page and fetch the text content
|
|
67
|
+
for (let i = 1; i <= document.numPages; i++) {
|
|
68
|
+
const page = await document.getPage(i)
|
|
69
|
+
const textContent = await page.getTextContent()
|
|
70
|
+
const contents = textContent.items.map(item => item.str).join(' ')
|
|
71
|
+
|
|
72
|
+
// Add page content to output
|
|
73
|
+
output.pages.push({
|
|
74
|
+
pageNumber: i,
|
|
75
|
+
content: contents
|
|
76
|
+
})
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Return the results as JSON
|
|
80
|
+
return new Response(JSON.stringify(output), {
|
|
81
|
+
headers: { 'Content-Type': 'application/json' }
|
|
82
|
+
})
|
|
83
|
+
}
|
|
84
|
+
}
|
|
46
85
|
```
|
|
47
86
|
|
|
48
|
-
> [!NOTE]
|
|
49
|
-
> Once Cloudflare workers support top-level await, we can remove this wrapper and re-export all PDF.js named exports directly again.
|
|
50
|
-
|
|
51
87
|
### 🦕 Deno
|
|
52
88
|
|
|
53
89
|
```ts
|
|
54
|
-
import {
|
|
90
|
+
import { getDocument } from 'https://esm.sh/pdfjs-serverless'
|
|
55
91
|
|
|
56
|
-
// Initialize PDF.js
|
|
57
|
-
const { getDocument } = await resolvePDFJS()
|
|
58
92
|
const data = Deno.readFileSync('sample.pdf')
|
|
59
|
-
const
|
|
93
|
+
const document = await getDocument({
|
|
60
94
|
data,
|
|
61
95
|
useSystemFonts: true,
|
|
62
96
|
}).promise
|
|
63
97
|
|
|
64
|
-
console.log(await
|
|
98
|
+
console.log(await document.getMetadata())
|
|
65
99
|
|
|
66
100
|
// Iterate through each page and fetch the text content
|
|
67
|
-
for (let i = 1; i <=
|
|
68
|
-
const page = await
|
|
101
|
+
for (let i = 1; i <= document.numPages; i++) {
|
|
102
|
+
const page = await document.getPage(i)
|
|
69
103
|
const textContent = await page.getTextContent()
|
|
70
104
|
const contents = textContent.items.map(item => item.str).join(' ')
|
|
71
105
|
console.log(contents)
|
|
72
106
|
}
|
|
73
107
|
```
|
|
74
108
|
|
|
75
|
-
### 🌩 Cloudflare Workers
|
|
76
|
-
|
|
77
|
-
```ts
|
|
78
|
-
import { resolvePDFJS } from 'pdfjs-serverless'
|
|
79
|
-
|
|
80
|
-
addEventListener('fetch', (event) => {
|
|
81
|
-
event.respondWith(handleRequest(event.request))
|
|
82
|
-
})
|
|
83
|
-
|
|
84
|
-
async function handleRequest(request) {
|
|
85
|
-
if (request.method !== 'POST')
|
|
86
|
-
return new Response('Method Not Allowed', { status: 405 })
|
|
87
|
-
|
|
88
|
-
// Get the PDF file from the POST request body as a buffer
|
|
89
|
-
const data = await request.arrayBuffer()
|
|
90
|
-
|
|
91
|
-
// Initialize PDF.js
|
|
92
|
-
const { getDocument } = await resolvePDFJS()
|
|
93
|
-
const doc = await getDocument({
|
|
94
|
-
data,
|
|
95
|
-
useSystemFonts: true,
|
|
96
|
-
}).promise
|
|
97
|
-
|
|
98
|
-
// Get metadata and initialize output object
|
|
99
|
-
const metadata = await doc.getMetadata()
|
|
100
|
-
const output = {
|
|
101
|
-
metadata,
|
|
102
|
-
pages: []
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
// Iterate through each page and fetch the text content
|
|
106
|
-
for (let i = 1; i <= doc.numPages; i++) {
|
|
107
|
-
const page = await doc.getPage(i)
|
|
108
|
-
const textContent = await page.getTextContent()
|
|
109
|
-
const contents = textContent.items.map(item => item.str).join(' ')
|
|
110
|
-
|
|
111
|
-
// Add page content to output
|
|
112
|
-
output.pages.push({
|
|
113
|
-
pageNumber: i,
|
|
114
|
-
content: contents
|
|
115
|
-
})
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
// Return the results as JSON
|
|
119
|
-
return new Response(JSON.stringify(output), {
|
|
120
|
-
headers: { 'Content-Type': 'application/json' }
|
|
121
|
-
})
|
|
122
|
-
}
|
|
123
|
-
```
|
|
124
|
-
|
|
125
109
|
## How It Works
|
|
126
110
|
|
|
127
|
-
|
|
111
|
+
Heart and soul of this package is the [`rollup.config.ts`](./rollup.config.ts) file. It uses [`rollup`](https://rollupjs.org/) to bundle the PDF.js library into a single file that can be used in serverless environments.
|
|
128
112
|
|
|
129
|
-
|
|
113
|
+
The heavy lifting comes from string replacements of the `PDF.js` library, i.e. removing browser context references and checks such as `typeof window`. Additionally, we enforce Node.js compatibility (may sound paradoxical at first, bear with me), i.e. we mock the `@napi-rs/canvas` module and set the `isNodeJS` flag to `true`.
|
|
130
114
|
|
|
131
|
-
|
|
115
|
+
PDF.js uses a worker to parse PDF documents. This worker is a separate file that is loaded by the main library. For the serverless build, we need to inline the worker code into the main library.
|
|
132
116
|
|
|
133
|
-
|
|
117
|
+
Finally, some mocks are added to the global scope that are not available in serverless environments, such as `FinalizationRegistry` which is not available in Cloudflare Workers.
|
|
134
118
|
|
|
135
119
|
## Inspiration
|
|
136
120
|
|
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,21 @@
|
|
|
1
1
|
import * as PDFJS from './types/src/pdf'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @deprecated Import from `pdfjs-serverless` instead.
|
|
5
|
+
* @example
|
|
6
|
+
* import { getDocument } from 'pdfjs-serverless'
|
|
7
|
+
*
|
|
8
|
+
* const buffer = await fetch('https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf')
|
|
9
|
+
* .then(res => res.arrayBuffer())
|
|
10
|
+
*
|
|
11
|
+
* const document = await getDocument({
|
|
12
|
+
* data: new Uint8Array(buffer),
|
|
13
|
+
* useSystemFonts: true,
|
|
14
|
+
* }).promise
|
|
15
|
+
*
|
|
16
|
+
* console.log(await document.getMetadata())
|
|
17
|
+
*/
|
|
2
18
|
declare function resolvePDFJS(): Promise<typeof PDFJS>
|
|
19
|
+
|
|
3
20
|
export { resolvePDFJS }
|
|
4
21
|
export * from './types/src/pdf'
|