pdfjs-serverless 1.1.0 → 1.2.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 (82) hide show
  1. package/README.md +14 -44
  2. package/dist/index.d.mts +2 -1
  3. package/dist/index.mjs +1 -1
  4. package/dist/types/src/display/annotation_layer.d.ts +42 -17
  5. package/dist/types/src/display/annotation_storage.d.ts +10 -8
  6. package/dist/types/src/display/api.d.ts +93 -63
  7. package/dist/types/src/display/api_utils.d.ts +1 -1
  8. package/dist/types/src/display/binary_data_factory.d.ts +26 -0
  9. package/dist/types/src/display/canvas.d.ts +19 -16
  10. package/dist/types/src/display/canvas_dependency_tracker.d.ts +99 -27
  11. package/dist/types/src/display/canvas_factory.d.ts +3 -1
  12. package/dist/types/src/display/display_utils.d.ts +25 -2
  13. package/dist/types/src/display/draw_layer.d.ts +2 -6
  14. package/dist/types/src/display/editor/alt_text.d.ts +1 -1
  15. package/dist/types/src/display/editor/annotation_editor_layer.d.ts +11 -14
  16. package/dist/types/src/display/editor/color_picker.d.ts +2 -2
  17. package/dist/types/src/display/editor/comment.d.ts +32 -5
  18. package/dist/types/src/display/editor/draw.d.ts +3 -7
  19. package/dist/types/src/display/editor/drawers/freedraw.d.ts +4 -3
  20. package/dist/types/src/display/editor/drawers/highlight.d.ts +2 -1
  21. package/dist/types/src/display/editor/drawers/signaturedraw.d.ts +13 -13
  22. package/dist/types/src/display/editor/editor.d.ts +81 -26
  23. package/dist/types/src/display/editor/freetext.d.ts +3 -3
  24. package/dist/types/src/display/editor/highlight.d.ts +6 -4
  25. package/dist/types/src/display/editor/ink.d.ts +2 -1
  26. package/dist/types/src/display/editor/signature.d.ts +0 -5
  27. package/dist/types/src/display/editor/toolbar.d.ts +5 -3
  28. package/dist/types/src/display/editor/tools.d.ts +58 -4
  29. package/dist/types/src/display/fetch_stream.d.ts +3 -56
  30. package/dist/types/src/display/font_loader.d.ts +28 -1
  31. package/dist/types/src/display/network.d.ts +14 -80
  32. package/dist/types/src/display/network_stream.d.ts +2 -0
  33. package/dist/types/src/display/network_utils.d.ts +3 -3
  34. package/dist/types/src/display/node_stream.d.ts +2 -52
  35. package/dist/types/src/display/node_utils.d.ts +5 -19
  36. package/dist/types/src/display/obj_bin_transform_display.d.ts +67 -0
  37. package/dist/types/src/display/pages_mapper.d.ts +72 -0
  38. package/dist/types/src/display/pattern_helper.d.ts +9 -4
  39. package/dist/types/src/display/text_layer.d.ts +16 -10
  40. package/dist/types/src/display/text_layer_images.d.ts +10 -0
  41. package/dist/types/src/display/transport_stream.d.ts +8 -64
  42. package/dist/types/src/display/webgpu_mesh.d.ts +3 -0
  43. package/dist/types/src/display/worker_options.d.ts +2 -2
  44. package/dist/types/src/display/xfa_layer.d.ts +3 -3
  45. package/dist/types/src/pdf.d.ts +9 -1
  46. package/dist/types/src/{interfaces.d.ts → shared/base_pdf_stream.d.ts} +26 -27
  47. package/dist/types/src/shared/obj_bin_transform_utils.d.ts +26 -0
  48. package/dist/types/src/shared/util.d.ts +14 -5
  49. package/dist/types/web/annotation_editor_layer_builder.d.ts +7 -8
  50. package/dist/types/web/annotation_layer_builder.d.ts +15 -23
  51. package/dist/types/web/app_options.d.ts +1 -1
  52. package/dist/types/web/autolinker.d.ts +3 -2
  53. package/dist/types/web/base_download_manager.d.ts +24 -0
  54. package/dist/types/web/base_pdf_page_view.d.ts +6 -6
  55. package/dist/types/web/comment_manager.d.ts +20 -0
  56. package/dist/types/web/download_manager.d.ts +3 -12
  57. package/dist/types/web/draw_layer_builder.d.ts +0 -12
  58. package/dist/types/web/genericl10n.d.ts +6 -10
  59. package/dist/types/web/l10n.d.ts +3 -6
  60. package/dist/types/web/menu.d.ts +11 -0
  61. package/dist/types/web/pdf_find_controller.d.ts +9 -11
  62. package/dist/types/web/pdf_history.d.ts +3 -3
  63. package/dist/types/web/pdf_link_service.d.ts +4 -8
  64. package/dist/types/web/pdf_page_detail_view.d.ts +1 -7
  65. package/dist/types/web/pdf_page_view.d.ts +23 -12
  66. package/dist/types/web/pdf_rendering_queue.d.ts +10 -14
  67. package/dist/types/web/pdf_thumbnail_view.d.ts +18 -42
  68. package/dist/types/web/pdf_thumbnail_viewer.d.ts +57 -13
  69. package/dist/types/web/pdf_viewer.component.d.ts +1 -1
  70. package/dist/types/web/pdf_viewer.d.ts +40 -28
  71. package/dist/types/web/renderable_view.d.ts +33 -0
  72. package/dist/types/web/sidebar.d.ts +65 -0
  73. package/dist/types/web/struct_tree_layer_builder.d.ts +1 -1
  74. package/dist/types/web/text_accessibility.d.ts +1 -1
  75. package/dist/types/web/text_layer_builder.d.ts +11 -6
  76. package/dist/types/web/ui_utils.d.ts +3 -11
  77. package/dist/types/web/xfa_layer_builder.d.ts +4 -4
  78. package/package.json +18 -15
  79. package/dist/types/src/display/cmap_reader_factory.d.ts +0 -25
  80. package/dist/types/src/display/standard_fontdata_factory.d.ts +0 -20
  81. package/dist/types/src/display/wasm_factory.d.ts +0 -20
  82. package/dist/types/web/interfaces.d.ts +0 -174
package/README.md CHANGED
@@ -1,13 +1,8 @@
1
1
  # pdfjs-serverless
2
2
 
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.
3
+ A redistribution of Mozilla's [PDF.js](https://github.com/mozilla/pdf.js) as a single bundle for edge and serverless runtimes, like Cloudflare Workers. Use it as a drop-in replacement for `pdfjs-dist` to parse PDF documents and extract text content – no extra dependencies needed.
4
4
 
5
- This package comes with zero dependencies. The whole export is about 1.4 MB (minified).
6
-
7
- ## PDF.js Compatibility
8
-
9
- > [!NOTE]
10
- > `pdfjs-serverless` is currently built from PDF.js v5.4.149.
5
+ The whole export is about 1.6 MB (minified).
11
6
 
12
7
  ## Installation
13
8
 
@@ -15,13 +10,10 @@ Run the following command to add `pdfjs-serverless` to your project.
15
10
 
16
11
  ```bash
17
12
  # pnpm
18
- pnpm add -D pdfjs-serverless
13
+ pnpm add pdfjs-serverless
19
14
 
20
15
  # npm
21
- npm install -D pdfjs-serverless
22
-
23
- # yarn
24
- yarn add -D pdfjs-serverless
16
+ npm install pdfjs-serverless
25
17
  ```
26
18
 
27
19
  ## Usage
@@ -36,18 +28,16 @@ yarn add -D pdfjs-serverless
36
28
  + import { getDocument } from 'pdfjs-serverless'
37
29
  ```
38
30
 
39
- ## Examples
40
-
41
- ### 🌩 Cloudflare Workers
31
+ Here's a full Cloudflare Workers example that accepts a PDF via POST and returns the extracted text as JSON:
42
32
 
43
33
  ```ts
34
+ import { getDocument } from 'pdfjs-serverless'
35
+
44
36
  export default {
45
37
  async fetch(request) {
46
38
  if (request.method !== 'POST')
47
39
  return new Response('Method Not Allowed', { status: 405 })
48
40
 
49
- const { getDocument } = await import('pdfjs-serverless')
50
-
51
41
  // Get the PDF file from the POST request body as a buffer
52
42
  const data = await request.arrayBuffer()
53
43
 
@@ -84,37 +74,17 @@ export default {
84
74
  }
85
75
  ```
86
76
 
87
- ### 🦕 Deno
88
-
89
- ```ts
90
- import { getDocument } from 'https://esm.sh/pdfjs-serverless'
91
-
92
- const data = Deno.readFileSync('sample.pdf')
93
- const document = await getDocument({
94
- data,
95
- useSystemFonts: true,
96
- }).promise
97
-
98
- console.log(await document.getMetadata())
99
-
100
- // Iterate through each page and fetch the text content
101
- for (let i = 1; i <= document.numPages; i++) {
102
- const page = await document.getPage(i)
103
- const textContent = await page.getTextContent()
104
- const contents = textContent.items.map(item => item.str).join(' ')
105
- console.log(contents)
106
- }
107
- ```
108
-
109
77
  ## How It Works
110
78
 
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.
112
-
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`.
79
+ > [!NOTE]
80
+ > `pdfjs-serverless` is currently built from PDF.js v5.6.205.
114
81
 
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.
82
+ Heart and soul of this package is the [`rollup.config.ts`](./rollup.config.ts) file. It uses [Rollup](https://rollupjs.org/) to bundle PDF.js into a single file for serverless environments. The key techniques:
116
83
 
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.
84
+ - **String replacements** strip browser-specific references (e.g. `typeof window`) from the PDF.js source.
85
+ - **Node.js compatibility** is enforced by mocking `@napi-rs/canvas` and setting `isNodeJS` to `true` – paradoxical, but it unlocks the right code paths.
86
+ - **Worker inlining** embeds the PDF.js worker directly into the main bundle, since serverless runtimes can't load separate worker files.
87
+ - **Global polyfills** provide missing APIs like `FinalizationRegistry` (unavailable in Cloudflare Workers).
118
88
 
119
89
  ## Inspiration
120
90
 
package/dist/index.d.mts CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as PDFJS from './types/src/pdf'
2
2
 
3
3
  /**
4
- * @deprecated Import from `pdfjs-serverless` instead.
4
+ * @deprecated Import from `pdfjs-serverless` instead. Will be removed in v2.
5
5
  * @example
6
6
  * import { getDocument } from 'pdfjs-serverless'
7
7
  *
@@ -19,3 +19,4 @@ declare function resolvePDFJS(): Promise<typeof PDFJS>
19
19
 
20
20
  export { resolvePDFJS }
21
21
  export * from './types/src/pdf'
22
+ export type { TextContent, TextItem, TextMarkedContent, TextStyle } from './types/src/display/api'