pdfjs-serverless 0.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/LICENSE +21 -0
- package/README.md +75 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.mjs +79716 -0
- package/package.json +58 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023-PRESENT Johann Schopplich
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# pdfjs-serverless
|
|
2
|
+
|
|
3
|
+
> **Note**
|
|
4
|
+
>
|
|
5
|
+
> Work in progress.
|
|
6
|
+
|
|
7
|
+
A redistribution of Mozilla's [PDF.js](https://github.com/mozilla/pdf.js) for serverless enviroments, like Deno Deploy and Cloudflare Workers.
|
|
8
|
+
|
|
9
|
+
## How It Works
|
|
10
|
+
|
|
11
|
+
First, some string replacements of the `pdf.js` library is necessary, i.e. removing browser context references. Additionally, we enforce Node.js (might sound paradox at first, bear with me) compatibility, i.e. polyfilling the `canvas` module and setting the `isNodeJS` flag to `true`.
|
|
12
|
+
|
|
13
|
+
To achieve a nodeless build, [`unenv`](https://github.com/unjs/unenv) does the heavy lifting by converting Node.js specific code to be platform-agnostic.
|
|
14
|
+
|
|
15
|
+
See the [`rollup.config.js`](./rollup.config.js) file for more information.
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
Run the following command to add `pdfjs-serverless` to your project.
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
# pnpm
|
|
23
|
+
pnpm add pdfjs-serverless
|
|
24
|
+
|
|
25
|
+
# npm
|
|
26
|
+
npm install pdfjs-serverless
|
|
27
|
+
|
|
28
|
+
# yarn
|
|
29
|
+
yarn add pdfjs-serverless
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Usage
|
|
33
|
+
|
|
34
|
+
### Deno
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
import { getDocument } from 'https://esm.sh/pdfjs-serverless'
|
|
38
|
+
|
|
39
|
+
const data = Deno.readFileSync('./dummy.pdf')
|
|
40
|
+
const doc = await getDocument(data).promise
|
|
41
|
+
|
|
42
|
+
console.log(await doc.getMetadata())
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### In Combination With `unpdf`
|
|
46
|
+
|
|
47
|
+
The following example shows how to use `pdfjs-serverless` with [`unpdf`](https://github.com/johannschopplich/unpdf):
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
import { defineUnPDFConfig, extractPDFText } from 'unpdf'
|
|
51
|
+
|
|
52
|
+
// Use the serverless version of PDF.js
|
|
53
|
+
defineUnPDFConfig({
|
|
54
|
+
pdfjs: () => import('pdfjs-serverless')
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
// Now, you can use the other methods provided by unpdf
|
|
58
|
+
|
|
59
|
+
// Fetch a PDF file from the web
|
|
60
|
+
const pdf = await fetch('https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf')
|
|
61
|
+
.then(res => res.arrayBuffer())
|
|
62
|
+
|
|
63
|
+
// Pass the PDF buffer to the relevant method
|
|
64
|
+
const { totalPages, text } = await extractPDFText(
|
|
65
|
+
new Uint8Array(pdf), { mergePages: true }
|
|
66
|
+
)
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Inspiration
|
|
70
|
+
|
|
71
|
+
- [`pdf.mjs`](https://github.com/bru02/pdf.mjs)
|
|
72
|
+
|
|
73
|
+
## License
|
|
74
|
+
|
|
75
|
+
[MIT](./LICENSE) License © 2023-PRESENT [Johann Schopplich](https://github.com/johannschopplich)
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from 'pdfjs-dist/types/src/pdf.d.ts'
|