pdfjs-serverless 0.6.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.
Files changed (77) hide show
  1. package/README.md +66 -82
  2. package/dist/index.d.mts +18 -0
  3. package/dist/index.mjs +1 -1
  4. package/dist/types/src/display/annotation_layer.d.ts +25 -1
  5. package/dist/types/src/display/annotation_storage.d.ts +1 -8
  6. package/dist/types/src/display/api.d.ts +147 -57
  7. package/dist/types/src/display/canvas.d.ts +21 -28
  8. package/dist/types/src/display/canvas_factory.d.ts +27 -0
  9. package/dist/types/src/display/cmap_reader_factory.d.ts +25 -0
  10. package/dist/types/src/display/display_utils.d.ts +40 -60
  11. package/dist/types/src/display/draw_layer.d.ts +7 -19
  12. package/dist/types/src/display/editor/alt_text.d.ts +3 -2
  13. package/dist/types/src/display/editor/annotation_editor_layer.d.ts +16 -11
  14. package/dist/types/src/display/editor/color_picker.d.ts +1 -0
  15. package/dist/types/src/display/editor/draw.d.ts +121 -0
  16. package/dist/types/src/display/editor/drawers/contour.d.ts +3 -0
  17. package/dist/types/src/display/editor/drawers/freedraw.d.ts +31 -0
  18. package/dist/types/src/display/editor/drawers/highlight.d.ts +36 -0
  19. package/dist/types/src/display/editor/drawers/inkdraw.d.ts +129 -0
  20. package/dist/types/src/display/editor/drawers/outline.d.ts +19 -0
  21. package/dist/types/src/display/editor/drawers/signaturedraw.d.ts +79 -0
  22. package/dist/types/src/display/editor/editor.d.ts +55 -13
  23. package/dist/types/src/display/editor/freetext.d.ts +5 -16
  24. package/dist/types/src/display/editor/highlight.d.ts +8 -25
  25. package/dist/types/src/display/editor/ink.d.ts +21 -74
  26. package/dist/types/src/display/editor/signature.d.ts +80 -0
  27. package/dist/types/src/display/editor/stamp.d.ts +16 -22
  28. package/dist/types/src/display/editor/toolbar.d.ts +5 -2
  29. package/dist/types/src/display/editor/tools.d.ts +17 -9
  30. package/dist/types/src/display/fetch_stream.d.ts +6 -13
  31. package/dist/types/src/display/filter_factory.d.ts +29 -0
  32. package/dist/types/src/display/font_loader.d.ts +3 -6
  33. package/dist/types/src/display/metadata.d.ts +1 -2
  34. package/dist/types/src/display/network.d.ts +11 -7
  35. package/dist/types/src/display/network_utils.d.ts +7 -6
  36. package/dist/types/src/display/node_stream.d.ts +6 -22
  37. package/dist/types/src/display/node_utils.d.ts +15 -11
  38. package/dist/types/src/display/optional_content_config.d.ts +2 -2
  39. package/dist/types/src/display/pattern_helper.d.ts +4 -2
  40. package/dist/types/src/display/standard_fontdata_factory.d.ts +20 -0
  41. package/dist/types/src/display/svg_factory.d.ts +14 -0
  42. package/dist/types/src/display/text_layer.d.ts +12 -9
  43. package/dist/types/src/display/touch_manager.d.ts +18 -0
  44. package/dist/types/src/display/transport_stream.d.ts +1 -1
  45. package/dist/types/src/display/wasm_factory.d.ts +20 -0
  46. package/dist/types/src/display/worker_options.d.ts +2 -2
  47. package/dist/types/src/interfaces.d.ts +4 -0
  48. package/dist/types/src/pdf.d.ts +13 -5
  49. package/dist/types/src/shared/message_handler.d.ts +9 -4
  50. package/dist/types/src/shared/util.d.ts +137 -129
  51. package/dist/types/web/annotation_editor_layer_builder.d.ts +20 -5
  52. package/dist/types/web/annotation_layer_builder.d.ts +35 -4
  53. package/dist/types/web/app_options.d.ts +1 -1
  54. package/dist/types/web/autolinker.d.ts +28 -0
  55. package/dist/types/web/base_pdf_page_view.d.ts +27 -0
  56. package/dist/types/web/draw_layer_builder.d.ts +13 -2
  57. package/dist/types/web/event_utils.d.ts +2 -2
  58. package/dist/types/web/genericl10n.d.ts +5 -5
  59. package/dist/types/web/interfaces.d.ts +5 -5
  60. package/dist/types/web/l10n.d.ts +2 -2
  61. package/dist/types/web/pdf_find_controller.d.ts +2 -0
  62. package/dist/types/web/pdf_link_service.d.ts +1 -2
  63. package/dist/types/web/pdf_page_detail_view.d.ts +32 -0
  64. package/dist/types/web/pdf_page_view.d.ts +44 -23
  65. package/dist/types/web/pdf_rendering_queue.d.ts +2 -1
  66. package/dist/types/web/pdf_thumbnail_view.d.ts +23 -3
  67. package/dist/types/web/pdf_thumbnail_viewer.d.ts +21 -1
  68. package/dist/types/web/pdf_viewer.d.ts +44 -16
  69. package/dist/types/web/struct_tree_layer_builder.d.ts +20 -2
  70. package/dist/types/web/text_accessibility.d.ts +1 -1
  71. package/dist/types/web/text_layer_builder.d.ts +25 -17
  72. package/dist/types/web/ui_utils.d.ts +3 -19
  73. package/dist/types/web/xfa_layer_builder.d.ts +14 -3
  74. package/package.json +16 -24
  75. package/dist/index.d.ts +0 -3
  76. package/dist/types/src/display/base_factory.d.ts +0 -60
  77. package/dist/types/src/display/editor/outliner.d.ts +0 -70
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 serverless environments, like Deno Deploy and Cloudflare Workers with zero dependencies. The whole export is about 1.4 MB (minified).
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
- > This package is currently using PDF.js v4.6.82.
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
- 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:
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
- So, instead of importing the named exports directly:
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
- ```ts
36
- // This will NOT work at the moment
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
- We have to use the `resolvePDFJS` function to get the named exports:
39
+ ## Examples
41
40
 
42
- ```ts
43
- import { resolvePDFJS } from 'pdfjs-serverless'
41
+ ### 🌩 Cloudflare Workers
44
42
 
45
- const { getDocument } = await resolvePDFJS()
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 { resolvePDFJS } from 'https://esm.sh/pdfjs-serverless'
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 doc = await getDocument({
93
+ const document = await getDocument({
60
94
  data,
61
95
  useSystemFonts: true,
62
96
  }).promise
63
97
 
64
- console.log(await doc.getMetadata())
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 <= doc.numPages; i++) {
68
- const page = await doc.getPage(i)
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
- 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`.
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
- 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.
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
- 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.
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
- See the [`rollup.config.ts`](./rollup.config.ts) file for more information.
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,3 +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 }
21
+ export * from './types/src/pdf'