pdfjs-serverless 0.3.2 → 0.4.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 +82 -11
- package/dist/index.d.mts +3 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.mjs +1 -1
- package/package.json +6 -1
package/README.md
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
# pdfjs-serverless
|
|
2
2
|
|
|
3
|
-
A redistribution of Mozilla's [PDF.js](https://github.com/mozilla/pdf.js) for serverless environments, like Deno Deploy and Cloudflare Workers with zero dependencies.
|
|
3
|
+
A redistribution of Mozilla's [PDF.js](https://github.com/mozilla/pdf.js) for serverless environments, like Deno Deploy and Cloudflare Workers with zero dependencies. The whole export is about 1.4 MB (minified).
|
|
4
4
|
|
|
5
5
|
## PDF.js Compatibility
|
|
6
6
|
|
|
7
7
|
> [!NOTE]
|
|
8
8
|
> This package is currently using PDF.js v4.0.189.
|
|
9
9
|
|
|
10
|
-
If you run into issues with the current version, please open an [issue](https://github.com/johannschopplich/pdfjs-serverless/issues/new/choose) or even better, open a [pull request](https://github.com/johannschopplich/pdfjs-serverless/compare).
|
|
11
|
-
|
|
12
10
|
## Installation
|
|
13
11
|
|
|
14
12
|
Run the following command to add `pdfjs-serverless` to your project.
|
|
@@ -24,28 +22,44 @@ npm install pdfjs-serverless
|
|
|
24
22
|
yarn add pdfjs-serverless
|
|
25
23
|
```
|
|
26
24
|
|
|
27
|
-
##
|
|
25
|
+
## Usage
|
|
28
26
|
|
|
29
|
-
|
|
27
|
+
Since PDF.js v4, the library migrated to ESM. Which is great. However, it also uses a top-level await, which is not supported by Cloudflare workers yet. Therefore, we have to wrap all named exports in a function that resolves the PDF.js library:
|
|
30
28
|
|
|
31
|
-
|
|
29
|
+
```ts
|
|
30
|
+
declare function resolvePDFJS(): Promise<typeof PDFJS>
|
|
31
|
+
```
|
|
32
32
|
|
|
33
|
-
|
|
33
|
+
So, instead of importing the named exports directly:
|
|
34
34
|
|
|
35
|
-
|
|
35
|
+
```ts
|
|
36
|
+
import { getDocument } from 'pdfjs-serverless'
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
We have to use the `resolvePDFJS` function to get the named exports:
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
import { resolvePDFJS } from 'pdfjs-serverless'
|
|
43
|
+
|
|
44
|
+
const { getDocument } = await resolvePDFJS()
|
|
45
|
+
```
|
|
36
46
|
|
|
37
|
-
|
|
47
|
+
> [!NOTE]
|
|
48
|
+
> Once Cloudflare workers support top-level await, we can remove this wrapper and pass all named exports directly again.
|
|
38
49
|
|
|
39
50
|
### 🦕 Deno
|
|
40
51
|
|
|
41
52
|
```ts
|
|
42
|
-
import {
|
|
53
|
+
import { resolvePDFJS } from 'https://esm.sh/pdfjs-serverless'
|
|
43
54
|
|
|
44
|
-
|
|
55
|
+
// Initialize PDF.js
|
|
56
|
+
const { getDocument } = await resolvePDFJS()
|
|
57
|
+
const data = Deno.readFileSync('sample.pdf')
|
|
45
58
|
const doc = await getDocument(data).promise
|
|
46
59
|
|
|
47
60
|
console.log(await doc.getMetadata())
|
|
48
61
|
|
|
62
|
+
// Iterate through each page and fetch the text content
|
|
49
63
|
for (let i = 1; i <= doc.numPages; i++) {
|
|
50
64
|
const page = await doc.getPage(i)
|
|
51
65
|
const textContent = await page.getTextContent()
|
|
@@ -54,6 +68,63 @@ for (let i = 1; i <= doc.numPages; i++) {
|
|
|
54
68
|
}
|
|
55
69
|
```
|
|
56
70
|
|
|
71
|
+
### 🌩 Cloudflare Workers
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
import { resolvePDFJS } from 'pdfjs-serverless'
|
|
75
|
+
|
|
76
|
+
addEventListener('fetch', (event) => {
|
|
77
|
+
event.respondWith(handleRequest(event.request))
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
async function handleRequest(request) {
|
|
81
|
+
if (request.method !== 'POST')
|
|
82
|
+
return new Response('Method Not Allowed', { status: 405 })
|
|
83
|
+
|
|
84
|
+
// Get the PDF file from the POST request body as a buffer
|
|
85
|
+
const data = await request.arrayBuffer()
|
|
86
|
+
|
|
87
|
+
// Initialize PDF.js
|
|
88
|
+
const { getDocument } = await resolvePDFJS()
|
|
89
|
+
const doc = await getDocument(data).promise
|
|
90
|
+
|
|
91
|
+
// Get metadata and initialize output object
|
|
92
|
+
const metadata = await doc.getMetadata()
|
|
93
|
+
const output = {
|
|
94
|
+
metadata,
|
|
95
|
+
pages: []
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Iterate through each page and fetch the text content
|
|
99
|
+
for (let i = 1; i <= doc.numPages; i++) {
|
|
100
|
+
const page = await doc.getPage(i)
|
|
101
|
+
const textContent = await page.getTextContent()
|
|
102
|
+
const contents = textContent.items.map(item => item.str).join(' ')
|
|
103
|
+
|
|
104
|
+
// Add page content to output
|
|
105
|
+
output.pages.push({
|
|
106
|
+
pageNumber: i,
|
|
107
|
+
content: contents
|
|
108
|
+
})
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// Return the results as JSON
|
|
112
|
+
return new Response(JSON.stringify(output), {
|
|
113
|
+
headers: { 'Content-Type': 'application/json' }
|
|
114
|
+
})
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## How It Works
|
|
119
|
+
|
|
120
|
+
First, some string replacements of the `PDF.js` library is necessary, i.e. removing browser context references and checks like `typeof window`. Additionally, we enforce Node.js compatibility (might sound paradox at first, bear with me), i.e. mocking the `canvas` module and setting the `isNodeJS` flag to `true`.
|
|
121
|
+
|
|
122
|
+
PDF.js uses a worker to parse and work with 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.
|
|
123
|
+
|
|
124
|
+
To achieve the final nodeless build, [`unenv`](https://github.com/unjs/unenv) does the heavy lifting by converting Node.js specific code to be platform-agnostic. This ensures that Node.js built-in modules like `fs` are mocked.
|
|
125
|
+
|
|
126
|
+
See the [`rollup.config.ts`](./rollup.config.ts) file for more information.
|
|
127
|
+
|
|
57
128
|
## Inspiration
|
|
58
129
|
|
|
59
130
|
- [`pdf.mjs`](https://github.com/bru02/pdf.mjs), a nodeless build of PDF.js v2.
|
package/dist/index.d.mts
CHANGED
package/dist/index.d.ts
CHANGED