pdfjs-serverless 1.3.0 → 2.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 (106) hide show
  1. package/README.md +52 -38
  2. package/dist/image-decoders.d.mts +1 -0
  3. package/dist/image-decoders.mjs +1 -0
  4. package/dist/index.d.mts +3 -3
  5. package/dist/index.mjs +41 -40
  6. package/dist/licenses/LICENSE_JBIG2 +196 -0
  7. package/dist/licenses/LICENSE_OPENJPEG +39 -0
  8. package/dist/licenses/LICENSE_PDFJS +177 -0
  9. package/dist/licenses/LICENSE_PDFJS_JBIG2 +13 -0
  10. package/dist/licenses/LICENSE_PDFJS_OPENJPEG +22 -0
  11. package/dist/pdfjs-decoders-workerd.mjs +1 -0
  12. package/dist/pdfjs-decoders.mjs +1 -0
  13. package/dist/pdfjs-jbig2.mjs +1 -0
  14. package/dist/pdfjs-jbig2_nowasm_fallback.mjs +1 -0
  15. package/dist/pdfjs-loader.mjs +1 -0
  16. package/dist/pdfjs-openjpeg.mjs +1 -0
  17. package/dist/pdfjs-openjpeg_nowasm_fallback.mjs +2 -0
  18. package/dist/pdfjs-rolldown-runtime.mjs +1 -0
  19. package/dist/types/external/qcms/qcms.d.ts +11 -7
  20. package/dist/types/external/qcms/qcms_utils.d.ts +1 -6
  21. package/dist/types/src/core/base_stream.d.ts +8 -1
  22. package/dist/types/src/core/catalog.d.ts +12 -12
  23. package/dist/types/src/core/colorspace.d.ts +6 -15
  24. package/dist/types/src/core/core_utils.d.ts +18 -14
  25. package/dist/types/src/core/crypto.d.ts +0 -1
  26. package/dist/types/src/core/file_spec.d.ts +4 -9
  27. package/dist/types/src/core/icc_colorspace.d.ts +0 -1
  28. package/dist/types/src/core/image_resizer.d.ts +24 -0
  29. package/dist/types/src/core/image_utils.d.ts +4 -4
  30. package/dist/types/src/core/jpeg_stream.d.ts +1 -1
  31. package/dist/types/src/core/jpg.d.ts +9 -13
  32. package/dist/types/src/core/parser.d.ts +0 -2
  33. package/dist/types/src/core/pattern.d.ts +0 -2
  34. package/dist/types/src/core/primitives.d.ts +30 -16
  35. package/dist/types/src/core/sasl_prep.d.ts +1 -0
  36. package/dist/types/src/core/sound.d.ts +39 -0
  37. package/dist/types/src/core/string_utils.d.ts +1 -1
  38. package/dist/types/src/core/struct_tree.d.ts +3 -3
  39. package/dist/types/src/core/xml_parser.d.ts +1 -2
  40. package/dist/types/src/core/xref.d.ts +2 -15
  41. package/dist/types/src/display/annotation_layer.d.ts +11 -22
  42. package/dist/types/src/display/annotation_storage.d.ts +7 -7
  43. package/dist/types/src/display/api.d.ts +155 -108
  44. package/dist/types/src/display/canvas.d.ts +2 -1
  45. package/dist/types/src/display/canvas_dependency_tracker.d.ts +8 -3
  46. package/dist/types/src/display/content_disposition.d.ts +0 -1
  47. package/dist/types/src/display/display_utils.d.ts +2 -2
  48. package/dist/types/src/display/draw_layer.d.ts +2 -6
  49. package/dist/types/src/display/editor/annotation_editor_layer.d.ts +18 -19
  50. package/dist/types/src/display/editor/color_picker.d.ts +1 -1
  51. package/dist/types/src/display/editor/comment.d.ts +1 -1
  52. package/dist/types/src/display/editor/draw.d.ts +78 -27
  53. package/dist/types/src/display/editor/drawers/freedraw.d.ts +11 -9
  54. package/dist/types/src/display/editor/drawers/highlight.d.ts +99 -15
  55. package/dist/types/src/display/editor/drawers/inkdraw.d.ts +7 -2
  56. package/dist/types/src/display/editor/drawers/outline.d.ts +65 -2
  57. package/dist/types/src/display/editor/editor.d.ts +24 -22
  58. package/dist/types/src/display/editor/freetext.d.ts +1 -1
  59. package/dist/types/src/display/editor/highlight.d.ts +44 -35
  60. package/dist/types/src/display/editor/ink.d.ts +7 -2
  61. package/dist/types/src/display/editor/stamp.d.ts +1 -1
  62. package/dist/types/src/display/editor/tools.d.ts +21 -19
  63. package/dist/types/src/display/filter_factory.d.ts +1 -3
  64. package/dist/types/src/display/font_loader.d.ts +1 -15
  65. package/dist/types/src/display/network_utils.d.ts +1 -0
  66. package/dist/types/src/display/obj_bin_transform_display.d.ts +25 -38
  67. package/dist/types/src/display/page_viewport.d.ts +5 -5
  68. package/dist/types/src/display/pages_mapper.d.ts +10 -4
  69. package/dist/types/src/display/pattern_helper.d.ts +4 -3
  70. package/dist/types/src/display/pdf_objects.d.ts +1 -3
  71. package/dist/types/src/display/text_layer.d.ts +6 -4
  72. package/dist/types/src/display/touch_manager.d.ts +46 -9
  73. package/dist/types/src/display/worker_options.d.ts +2 -2
  74. package/dist/types/src/display/xfa_layer.d.ts +9 -14
  75. package/dist/types/src/display/xfa_text.d.ts +4 -7
  76. package/dist/types/src/pdf.d.ts +8 -7
  77. package/dist/types/src/shared/base_pdf_stream.d.ts +6 -7
  78. package/dist/types/src/shared/css_utils.d.ts +8 -0
  79. package/dist/types/src/shared/message_handler.d.ts +2 -11
  80. package/dist/types/src/shared/obj_bin_transform_utils.d.ts +4 -3
  81. package/dist/types/src/shared/util.d.ts +4 -6
  82. package/dist/types/web/annotation_editor_layer_builder.d.ts +10 -8
  83. package/dist/types/web/annotation_layer_builder.d.ts +17 -21
  84. package/dist/types/web/draw_layer_builder.d.ts +7 -9
  85. package/dist/types/web/event_utils.d.ts +12 -13
  86. package/dist/types/web/genericl10n.d.ts +2 -2
  87. package/dist/types/web/l10n.d.ts +1 -1
  88. package/dist/types/web/pdf_find_controller.d.ts +9 -10
  89. package/dist/types/web/pdf_find_utils.d.ts +8 -14
  90. package/dist/types/web/pdf_history.d.ts +2 -2
  91. package/dist/types/web/pdf_link_service.d.ts +7 -9
  92. package/dist/types/web/pdf_page_view.d.ts +15 -15
  93. package/dist/types/web/pdf_rendering_queue.d.ts +7 -8
  94. package/dist/types/web/pdf_scripting_manager.d.ts +5 -5
  95. package/dist/types/web/pdf_thumbnail_view.d.ts +11 -11
  96. package/dist/types/web/pdf_thumbnail_viewer.d.ts +21 -22
  97. package/dist/types/web/pdf_viewer.d.ts +49 -29
  98. package/dist/types/web/renderable_view.d.ts +3 -3
  99. package/dist/types/web/sidebar.d.ts +3 -3
  100. package/dist/types/web/struct_tree_layer_builder.d.ts +21 -7
  101. package/dist/types/web/text_accessibility.d.ts +4 -2
  102. package/dist/types/web/text_highlighter.d.ts +7 -8
  103. package/dist/types/web/text_layer_builder.d.ts +11 -11
  104. package/dist/types/web/ui_utils.d.ts +8 -13
  105. package/dist/types/web/xfa_layer_builder.d.ts +13 -13
  106. package/package.json +27 -16
package/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # pdfjs-serverless
2
2
 
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.
3
+ A redistribution of Mozilla's [PDF.js](https://github.com/mozilla/pdf.js) as a 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
- The whole export is about 1.6 MB (minified) and ships the complete PDF.js type declarations.
5
+ The main bundle is about 1.7 MB (minified) and leaves the image decoders to an [opt-in import](#decoding-ccitt-jbig2-and-jpeg-2000-images). It ships the complete PDF.js type declarations and is tested in Node.js 24+, Bun, and Cloudflare Workers.
6
6
 
7
7
  ## Installation
8
8
 
@@ -19,7 +19,7 @@ npm install pdfjs-serverless
19
19
  > [!TIP]
20
20
  > 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.
21
21
 
22
- `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`:
22
+ Swap the import from `pdfjs-dist` to `pdfjs-serverless`:
23
23
 
24
24
  ```diff
25
25
  - import { getDocument } from 'pdfjs-dist'
@@ -36,54 +36,68 @@ export default {
36
36
  if (request.method !== 'POST')
37
37
  return new Response('Method Not Allowed', { status: 405 })
38
38
 
39
- // Get the PDF file from the POST request body as a buffer
40
- const data = await request.arrayBuffer()
39
+ const data = new Uint8Array(await request.arrayBuffer())
40
+ const document = await getDocument({ data, useSystemFonts: true }).promise
41
41
 
42
- const document = await getDocument({
43
- data: new Uint8Array(data),
44
- useSystemFonts: true,
45
- }).promise
46
-
47
- // Get metadata and initialize output object
48
- const metadata = await document.getMetadata()
49
- const output = {
50
- metadata,
51
- pages: []
52
- }
53
-
54
- // Iterate through each page and fetch the text content
55
- for (let i = 1; i <= document.numPages; i++) {
56
- const page = await document.getPage(i)
57
- const textContent = await page.getTextContent()
58
- const contents = textContent.items.map(item => item.str).join(' ')
59
-
60
- // Add page content to output
61
- output.pages.push({
62
- pageNumber: i,
63
- content: contents
64
- })
42
+ const pages = []
43
+ for (let pageNumber = 1; pageNumber <= document.numPages; pageNumber++) {
44
+ const page = await document.getPage(pageNumber)
45
+ const { items } = await page.getTextContent()
46
+ pages.push({ pageNumber, content: items.map(item => item.str).join(' ') })
65
47
  }
66
48
 
67
- // Return the results as JSON
68
- return new Response(JSON.stringify(output), {
69
- headers: { 'Content-Type': 'application/json' }
70
- })
71
- }
49
+ return Response.json({ metadata: await document.getMetadata(), pages })
50
+ },
72
51
  }
73
52
  ```
74
53
 
75
- Ready-to-run examples for Cloudflare Workers and Pages Functions live in [`examples/`](./examples).
54
+ A ready-to-run Cloudflare Workers example lives in [`examples/cloudflare`](./examples/cloudflare).
55
+
56
+ ### Rendering Pages
57
+
58
+ Rendering needs a canvas implementation, which serverless runtimes like Cloudflare Workers lack. In Node.js, hand [`@napi-rs/canvas`](https://github.com/Brooooooklyn/canvas) to the bundle through a well-known global symbol and expose its `Path2D`:
59
+
60
+ ```ts
61
+ import { writeFile } from 'node:fs/promises'
62
+ import * as canvas from '@napi-rs/canvas'
63
+ import { getDocument } from 'pdfjs-serverless'
64
+ import 'pdfjs-serverless/image-decoders'
65
+
66
+ globalThis[Symbol.for('pdfjs-serverless.canvasModule')] = canvas
67
+ globalThis.Path2D ??= canvas.Path2D
68
+
69
+ const document = await getDocument({ data }).promise
70
+ const page = await document.getPage(1)
71
+ const viewport = page.getViewport({ scale: 2 })
72
+ const target = canvas.createCanvas(viewport.width, viewport.height)
73
+
74
+ await page.render({ canvas: target, canvasContext: target.getContext('2d'), viewport }).promise
75
+ await writeFile('page-1.png', await target.encode('png'))
76
+ ```
77
+
78
+ Without a canvas module, rendering throws a descriptive error.
79
+
80
+ ### Decoding CCITT, JBIG2, and JPEG 2000 Images
81
+
82
+ Rendering a page or reading its image data needs PDF.js' JBIG2 and OpenJPEG decoders for images in these formats. Text extraction doesn't, so your bundle leaves them out unless you import them once:
83
+
84
+ ```ts
85
+ import 'pdfjs-serverless/image-decoders'
86
+ ```
87
+
88
+ The decoders then load on the first such image. Without the import, PDF.js skips these images and warns that the import is missing.
76
89
 
77
90
  ## How It Works
78
91
 
79
92
  > [!NOTE]
80
- > `pdfjs-serverless` is currently built from PDF.js v5.6.205.
93
+ > `pdfjs-serverless` is built from PDF.js v6.4.299. It requires the global `Iterator` constructor (Safari 18.4+); without it, importing the bundle throws.
81
94
 
82
- Heart and soul of this package is the [`rolldown.config.ts`](./rolldown.config.ts) file. It uses [Rolldown](https://rolldown.rs/) to bundle PDF.js into a single file for serverless environments. The key techniques:
95
+ Heart and soul of this package is the [`rolldown.config.ts`](./rolldown.config.ts) file. It uses [Rolldown](https://rolldown.rs/) to bundle PDF.js for serverless environments. The key techniques:
83
96
 
84
- - **String replacements** rewrite the environment detection: `isNodeJS` becomes `typeof window === "undefined"`, so serverless runtimes take the Node.js code paths – paradoxical, but it unlocks the right branches. The `@napi-rs/canvas` import is bridged to `globalThis[Symbol.for("pdfjs-serverless.canvasModule")]` – assign an API-compatible canvas module to that symbol to enable rendering, otherwise accessing it throws a descriptive error.
97
+ - **String replacements** rewrite the environment detection: `isNodeJS` becomes `typeof window === "undefined"`, so serverless runtimes take the Node.js code paths – paradoxical, but it unlocks the right branches. The `@napi-rs/canvas` import is bridged to a global symbol (see [Rendering Pages](#rendering-pages)).
85
98
  - **Worker inlining** embeds the PDF.js worker directly into the main bundle, since serverless runtimes can't load separate worker files.
86
- - **Mocks and polyfills** provide missing globals: `DOMMatrix`, `navigator` and `FinalizationRegistry` are stubbed in [`src/mocks.mjs`](./src/mocks.mjs); `Promise.withResolvers`, `Uint8Array.prototype.toHex` and friends are polyfilled in [`src/polyfills.mjs`](./src/polyfills.mjs).
99
+ - **Bundled image decoders** ship PDF.js' JBIG2 and OpenJPEG decoders, WASM and JS fallback, as lazy chunks behind [`pdfjs-serverless/image-decoders`](#decoding-ccitt-jbig2-and-jpeg-2000-images), so CCITT, JBIG2, and JPEG 2000 images decode without `pdfjs-dist` or a `wasmUrl`. Where the runtime can't compile WASM, as in Cloudflare Workers, the bundle turns `useWasm` off and the JS decoders take over. Bundlers that resolve the `workerd` condition, like Wrangler, ship only the JS fallbacks.
100
+ - **Mocks and polyfills** provide missing globals: `DOMMatrix`, `navigator` and `FinalizationRegistry` are stubbed in [`src/mocks.mjs`](./src/mocks.mjs); `Promise.try`, `Map.prototype.getOrInsert`, `Uint8Array.prototype.toHex` and friends are polyfilled in [`src/polyfills.mjs`](./src/polyfills.mjs).
87
101
 
88
102
  ## Inspiration
89
103
 
@@ -0,0 +1 @@
1
+ export {}
@@ -0,0 +1 @@
1
+ globalThis[Symbol.for(`pdfjs-serverless.loadImageDecoder`)]??=async e=>{let{loadDecoder:t}=await import(`#pdfjs-decoders`);return await t(e)};
package/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import * as PDFJS from './types/src/pdf'
1
+ import * as PDFJS from './types/src/pdf.js'
2
2
 
3
3
  /**
4
4
  * @deprecated Import from `pdfjs-serverless` instead. Will be removed in v2.
@@ -18,5 +18,5 @@ import * as PDFJS from './types/src/pdf'
18
18
  declare function resolvePDFJS(): Promise<typeof PDFJS>
19
19
 
20
20
  export { resolvePDFJS }
21
- export * from './types/src/pdf'
22
- export type { TextContent, TextItem, TextMarkedContent, TextStyle } from './types/src/display/api'
21
+ export * from './types/src/pdf.js'
22
+ export type { TextContent, TextItem, TextMarkedContent, TextStyle } from './types/src/display/api.js'