pdfjs-serverless 0.3.2 → 0.4.1

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.
Files changed (32) hide show
  1. package/README.md +90 -12
  2. package/dist/index.d.mts +3 -1
  3. package/dist/index.d.ts +3 -1
  4. package/dist/index.mjs +1 -1
  5. package/dist/types/src/display/api.d.ts +8 -7
  6. package/dist/types/src/display/display_utils.d.ts +3 -2
  7. package/dist/types/src/display/draw_layer.d.ts +40 -0
  8. package/dist/types/src/display/editor/alt_text.d.ts +21 -0
  9. package/dist/types/src/display/editor/annotation_editor_layer.d.ts +23 -2
  10. package/dist/types/src/display/editor/color_picker.d.ts +19 -0
  11. package/dist/types/src/display/editor/editor.d.ts +29 -15
  12. package/dist/types/src/display/editor/highlight.d.ts +54 -0
  13. package/dist/types/src/display/editor/ink.d.ts +3 -3
  14. package/dist/types/src/display/editor/outliner.d.ts +26 -0
  15. package/dist/types/src/display/editor/toolbar.d.ts +11 -0
  16. package/dist/types/src/display/editor/tools.d.ts +3 -1
  17. package/dist/types/src/pdf.d.ts +5 -1
  18. package/dist/types/src/shared/util.d.ts +5 -1
  19. package/dist/types/web/annotation_editor_layer_builder.d.ts +4 -0
  20. package/dist/types/web/draw_layer_builder.d.ts +23 -0
  21. package/dist/types/web/genericl10n.d.ts +3 -3
  22. package/dist/types/web/interfaces.d.ts +3 -3
  23. package/dist/types/web/l10n.d.ts +4 -2
  24. package/dist/types/web/pdf_link_service.d.ts +5 -5
  25. package/dist/types/web/pdf_page_view.d.ts +2 -1
  26. package/dist/types/web/pdf_scripting_manager.d.ts +6 -6
  27. package/dist/types/web/pdf_single_page_viewer.d.ts +2 -2
  28. package/dist/types/web/pdf_thumbnail_view.d.ts +1 -1
  29. package/dist/types/web/pdf_thumbnail_viewer.d.ts +1 -1
  30. package/dist/types/web/pdf_viewer.d.ts +52 -13
  31. package/dist/types/web/ui_utils.d.ts +1 -1
  32. package/package.json +16 -11
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. All named exports of the `PDF.js` library are available at roughly 1.4 MB (minified).
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,48 @@ npm install pdfjs-serverless
24
22
  yarn add pdfjs-serverless
25
23
  ```
26
24
 
27
- ## How It Works
25
+ ## Usage
28
26
 
29
- 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`.
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
- 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.
29
+ ```ts
30
+ declare function resolvePDFJS(): Promise<typeof PDFJS>
31
+ ```
32
32
 
33
- 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.
33
+ So, instead of importing the named exports directly:
34
34
 
35
- See the [`rollup.config.ts`](./rollup.config.ts) file for more information.
35
+ ```ts
36
+ // This will NOT work at the moment
37
+ import { getDocument } from 'pdfjs-serverless'
38
+ ```
39
+
40
+ We have to use the `resolvePDFJS` function to get the named exports:
41
+
42
+ ```ts
43
+ import { resolvePDFJS } from 'pdfjs-serverless'
44
+
45
+ const { getDocument } = await resolvePDFJS()
46
+ ```
36
47
 
37
- ## Example Usage
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.
38
50
 
39
51
  ### 🦕 Deno
40
52
 
41
53
  ```ts
42
- import { getDocument } from 'https://esm.sh/pdfjs-serverless'
54
+ import { resolvePDFJS } from 'https://esm.sh/pdfjs-serverless'
43
55
 
44
- const data = Deno.readFileSync('dummy.pdf')
45
- const doc = await getDocument(data).promise
56
+ // Initialize PDF.js
57
+ const { getDocument } = await resolvePDFJS()
58
+ const data = Deno.readFileSync('sample.pdf')
59
+ const doc = await getDocument({
60
+ data,
61
+ useSystemFonts: true,
62
+ }).promise
46
63
 
47
64
  console.log(await doc.getMetadata())
48
65
 
66
+ // Iterate through each page and fetch the text content
49
67
  for (let i = 1; i <= doc.numPages; i++) {
50
68
  const page = await doc.getPage(i)
51
69
  const textContent = await page.getTextContent()
@@ -54,6 +72,66 @@ for (let i = 1; i <= doc.numPages; i++) {
54
72
  }
55
73
  ```
56
74
 
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
+ ## How It Works
126
+
127
+ 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`.
128
+
129
+ 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.
130
+
131
+ 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.
132
+
133
+ See the [`rollup.config.ts`](./rollup.config.ts) file for more information.
134
+
57
135
  ## Inspiration
58
136
 
59
137
  - [`pdf.mjs`](https://github.com/bru02/pdf.mjs), a nodeless build of PDF.js v2.
package/dist/index.d.mts CHANGED
@@ -1 +1,3 @@
1
- export * from './types/src/pdf.d.ts'
1
+ import * as PDFJS from './types/src/pdf'
2
+ declare function resolvePDFJS(): Promise<typeof PDFJS>
3
+ export { resolvePDFJS }
package/dist/index.d.ts CHANGED
@@ -1 +1,3 @@
1
- export * from './types/src/pdf.d.ts'
1
+ import * as PDFJS from './types/src/pdf'
2
+ declare function resolvePDFJS(): Promise<typeof PDFJS>
3
+ export { resolvePDFJS }