unpdf 1.6.2 → 1.8.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 (122) hide show
  1. package/README.md +61 -25
  2. package/dist/index.cjs +386 -353
  3. package/dist/index.d.cts +101 -91
  4. package/dist/index.d.mts +101 -91
  5. package/dist/index.mjs +385 -351
  6. package/dist/pdfjs.d.mts +1 -1
  7. package/dist/pdfjs.d.ts +1 -1
  8. package/dist/pdfjs.mjs +52 -59
  9. package/dist/types/external/brotli/decode.d.ts +5 -0
  10. package/dist/types/external/jbig2/jbig2.d.ts +2 -0
  11. package/dist/types/external/openjpeg/openjpeg.d.ts +2 -0
  12. package/dist/types/external/qcms/qcms.d.ts +92 -0
  13. package/dist/types/external/qcms/qcms_utils.d.ts +14 -0
  14. package/dist/types/src/core/ascii_85_stream.d.ts +7 -0
  15. package/dist/types/src/core/ascii_hex_stream.d.ts +7 -0
  16. package/dist/types/src/core/base_stream.d.ts +45 -0
  17. package/dist/types/src/core/brotli_stream.d.ts +9 -0
  18. package/dist/types/src/core/calculate_md5.d.ts +1 -0
  19. package/dist/types/src/core/calculate_sha256.d.ts +1 -0
  20. package/dist/types/src/core/calculate_sha_other.d.ts +2 -0
  21. package/dist/types/src/core/catalog.d.ts +169 -0
  22. package/dist/types/src/core/ccitt_stream.d.ts +18 -0
  23. package/dist/types/src/core/cleanup_helper.d.ts +1 -0
  24. package/dist/types/src/core/colorspace.d.ts +177 -0
  25. package/dist/types/src/core/colorspace_utils.d.ts +21 -0
  26. package/dist/types/src/core/core_utils.d.ts +132 -0
  27. package/dist/types/src/core/crypto.d.ts +136 -0
  28. package/dist/types/src/core/decode_stream.d.ts +32 -0
  29. package/dist/types/src/core/decrypt_stream.d.ts +9 -0
  30. package/dist/types/src/core/file_spec.d.ts +72 -0
  31. package/dist/types/src/core/flate_stream.d.ts +14 -0
  32. package/dist/types/src/core/icc_colorspace.d.ts +22 -0
  33. package/dist/types/src/core/image_utils.d.ts +52 -0
  34. package/dist/types/src/core/jbig2_ccittFax.d.ts +13 -0
  35. package/dist/types/src/core/jbig2_stream.d.ts +17 -0
  36. package/dist/types/src/core/jpeg_stream.d.ts +24 -0
  37. package/dist/types/src/core/jpg.d.ts +62 -0
  38. package/dist/types/src/core/jpx.d.ts +24 -0
  39. package/dist/types/src/core/jpx_stream.d.ts +15 -0
  40. package/dist/types/src/core/lzw_stream.d.ts +19 -0
  41. package/dist/types/src/core/metadata_parser.d.ts +13 -0
  42. package/dist/types/src/core/name_number_tree.d.ts +21 -0
  43. package/dist/types/src/core/parser.d.ts +121 -0
  44. package/dist/types/src/core/pattern.d.ts +63 -0
  45. package/dist/types/src/core/predictor_stream.d.ts +14 -0
  46. package/dist/types/src/core/primitives.d.ts +81 -0
  47. package/dist/types/src/core/run_length_stream.d.ts +6 -0
  48. package/dist/types/src/core/stream.d.ts +20 -0
  49. package/dist/types/src/core/string_utils.d.ts +5 -0
  50. package/dist/types/src/core/struct_tree.d.ts +93 -0
  51. package/dist/types/src/core/unicode.d.ts +5 -0
  52. package/dist/types/src/core/wasm_image.d.ts +21 -0
  53. package/dist/types/src/core/xml_parser.d.ts +79 -0
  54. package/dist/types/src/core/xref.d.ts +52 -0
  55. package/dist/types/src/display/annotation_layer.d.ts +11 -9
  56. package/dist/types/src/display/api.d.ts +92 -65
  57. package/dist/types/src/display/binary_data_factory.d.ts +0 -4
  58. package/dist/types/src/display/canvas.d.ts +58 -12
  59. package/dist/types/src/display/canvas_dependency_tracker.d.ts +2 -2
  60. package/dist/types/src/display/display_utils.d.ts +4 -156
  61. package/dist/types/src/display/draw_layer.d.ts +127 -2
  62. package/dist/types/src/display/editor/alt_text.d.ts +1 -1
  63. package/dist/types/src/display/editor/annotation_editor_layer.d.ts +3 -3
  64. package/dist/types/src/display/editor/color_picker.d.ts +3 -2
  65. package/dist/types/src/display/editor/draw.d.ts +10 -3
  66. package/dist/types/src/display/editor/drawers/freedraw.d.ts +3 -3
  67. package/dist/types/src/display/editor/drawers/highlight.d.ts +0 -1
  68. package/dist/types/src/display/editor/drawers/outline.d.ts +0 -1
  69. package/dist/types/src/display/editor/drawers/signaturedraw.d.ts +13 -13
  70. package/dist/types/src/display/editor/editor.d.ts +2 -1
  71. package/dist/types/src/display/editor/freetext.d.ts +2 -2
  72. package/dist/types/src/display/editor/highlight.d.ts +3 -3
  73. package/dist/types/src/display/editor/ink.d.ts +2 -0
  74. package/dist/types/src/display/editor/toolbar.d.ts +2 -2
  75. package/dist/types/src/display/editor/tools.d.ts +31 -14
  76. package/dist/types/src/display/filter_factory.d.ts +26 -0
  77. package/dist/types/src/display/node_utils.d.ts +2 -2
  78. package/dist/types/src/display/obj_bin_transform_display.d.ts +2 -6
  79. package/dist/types/src/display/optional_content_config.d.ts +13 -3
  80. package/dist/types/src/display/page_viewport.d.ts +137 -0
  81. package/dist/types/src/display/pattern_helper.d.ts +11 -4
  82. package/dist/types/src/display/text_layer.d.ts +13 -13
  83. package/dist/types/src/display/text_layer_images.d.ts +1 -1
  84. package/dist/types/src/display/webgpu.d.ts +11 -0
  85. package/dist/types/src/display/worker_options.d.ts +2 -2
  86. package/dist/types/src/display/xfa_layer.d.ts +10 -3
  87. package/dist/types/src/display/xfa_text.d.ts +2 -2
  88. package/dist/types/src/pdf.d.ts +10 -9
  89. package/dist/types/src/shared/image_utils.d.ts +11 -0
  90. package/dist/types/src/shared/internal_evt.d.ts +7 -0
  91. package/dist/types/src/shared/math_clamp.d.ts +1 -0
  92. package/dist/types/src/shared/util.d.ts +24 -13
  93. package/dist/types/web/annotation_editor_layer_builder.d.ts +5 -5
  94. package/dist/types/web/annotation_layer_builder.d.ts +14 -11
  95. package/dist/types/web/app_options.d.ts +1 -1
  96. package/dist/types/web/autolinker.d.ts +3 -3
  97. package/dist/types/web/base_pdf_page_view.d.ts +1 -0
  98. package/dist/types/web/comment_manager.d.ts +1 -1
  99. package/dist/types/web/draw_layer_builder.d.ts +44 -0
  100. package/dist/types/web/event_utils.d.ts +0 -8
  101. package/dist/types/web/generic_scripting.d.ts +1 -1
  102. package/dist/types/web/genericl10n.d.ts +5 -5
  103. package/dist/types/web/internal_evt.d.ts +7 -0
  104. package/dist/types/web/l10n.d.ts +2 -2
  105. package/dist/types/web/pdf_find_controller.d.ts +16 -5
  106. package/dist/types/web/pdf_history.d.ts +1 -1
  107. package/dist/types/web/pdf_link_service.d.ts +10 -2
  108. package/dist/types/web/pdf_page_view.d.ts +19 -12
  109. package/dist/types/web/pdf_rendering_queue.d.ts +3 -3
  110. package/dist/types/web/pdf_scripting_manager.d.ts +1 -1
  111. package/dist/types/web/pdf_thumbnail_view.d.ts +9 -9
  112. package/dist/types/web/pdf_thumbnail_viewer.d.ts +38 -19
  113. package/dist/types/web/pdf_viewer.d.ts +28 -14
  114. package/dist/types/web/renderable_view.d.ts +2 -2
  115. package/dist/types/web/struct_tree_layer_builder.d.ts +1 -1
  116. package/dist/types/web/text_accessibility.d.ts +1 -1
  117. package/dist/types/web/text_highlighter.d.ts +6 -6
  118. package/dist/types/web/text_layer_builder.d.ts +10 -10
  119. package/dist/types/web/xfa_layer_builder.d.ts +8 -8
  120. package/package.json +25 -24
  121. package/dist/index.d.ts +0 -182
  122. package/dist/types/src/display/webgpu_mesh.d.ts +0 -3
package/README.md CHANGED
@@ -7,7 +7,7 @@ Ships with a serverless build of Mozilla's [PDF.js](https://github.com/mozilla/p
7
7
  ## Features
8
8
 
9
9
  - 🏗️ Works in Node.js, browser and serverless environments
10
- - 🪭 Includes serverless build of PDF.js ([`unpdf/pdfjs`](./package.json#L34))
10
+ - 🪭 Includes serverless build of PDF.js ([`unpdf/pdfjs`](./package.json))
11
11
  - 💬 Extract [text](#extract-text-from-pdf), [links](#extractlinks), and [images](#extractimages) from PDF files
12
12
  - 🧠 Perfect for AI applications and PDF summarization
13
13
  - 🧱 Opt-in to official or legacy PDF.js build
@@ -93,11 +93,11 @@ console.log(await document.getMetadata())
93
93
  > [!NOTE]
94
94
  > The serverless PDF.js bundle is built from PDF.js v5.6.205.
95
95
 
96
- Heart and soul of this package is the [`pdfjs.rollup.config.ts`](./pdfjs.rollup.config.ts) file. It uses [Rollup](https://rollupjs.org/) to bundle PDF.js into a single file for serverless environments. The key techniques:
96
+ Heart and soul of this package is the [`pdfjs.rolldown.config.ts`](./pdfjs.rolldown.config.ts) file. It uses [Rolldown](https://rolldown.rs/) to bundle PDF.js into a single file for serverless environments. The key techniques:
97
97
 
98
98
  - **String replacements** strip browser-specific references from the PDF.js source.
99
99
  - **Worker inlining** embeds the PDF.js worker directly into the main bundle, since serverless runtimes can't load separate worker files.
100
- - **Global polyfills** provide missing APIs like `FinalizationRegistry` (unavailable in Cloudflare Workers).
100
+ - **Global polyfills** provide missing APIs like `Promise.withResolvers` and `FinalizationRegistry` (unavailable in Node.js < 22 and Cloudflare Workers, respectively).
101
101
 
102
102
  ## API
103
103
 
@@ -121,6 +121,21 @@ Returns the resolved PDF.js module. If no other PDF.js build was defined, the se
121
121
  function getResolvedPDFJS(): Promise<PDFJS>
122
122
  ```
123
123
 
124
+ ### `getDocumentProxy`
125
+
126
+ Creates a `PDFDocumentProxy` from binary PDF data. Every extraction method accepts either raw data or an existing proxy – use this when you want to reuse one document across multiple calls.
127
+
128
+ Applies sensible defaults: `useSystemFonts: true`; in Node.js additionally `disableFontFace: true`, plus `standardFontDataUrl` and `cMapUrl`/`cMapPacked` resolved from the local `pdfjs-dist` package for standard font and CJK character map support (see the font rendering tip in [`renderPageAsImage`](#renderpageasimage)).
129
+
130
+ **Type Declaration**
131
+
132
+ ```ts
133
+ function getDocumentProxy(
134
+ data: DocumentInitParameters['data'],
135
+ options?: DocumentInitParameters,
136
+ ): Promise<PDFDocumentProxy>
137
+ ```
138
+
124
139
  ### `getMeta`
125
140
 
126
141
  Extracts metadata from a PDF. If `parseDates` is set to `true`, the date properties will be parsed into `Date` objects.
@@ -141,7 +156,7 @@ function getMeta(
141
156
 
142
157
  ### `extractText`
143
158
 
144
- Extracts all text from a PDF. If `mergePages` is set to `true`, the text of all pages will be merged into a single string. Otherwise, an array of strings for each page will be returned.
159
+ Extracts all text from a PDF. If `mergePages` is set to `true`, the text of all pages will be merged into a single string with line breaks preserved and at most one blank line in a row. Otherwise, an array of strings for each page will be returned.
145
160
 
146
161
  **Type Declaration**
147
162
 
@@ -149,20 +164,42 @@ Extracts all text from a PDF. If `mergePages` is set to `true`, the text of all
149
164
  function extractText(
150
165
  data: DocumentInitParameters['data'] | PDFDocumentProxy,
151
166
  options?: {
152
- mergePages?: false
167
+ mergePages?: boolean
153
168
  }
154
169
  ): Promise<{
155
170
  totalPages: number
156
- text: string[]
171
+ text: string | string[]
157
172
  }>
158
- function extractText(
173
+ ```
174
+
175
+ ### `extractTextItems`
176
+
177
+ Extracts text with layout information – one array of positioned items per page. Useful when plain text is not enough and you need coordinates, font sizes, or reading direction, e.g. for table detection or positional parsing.
178
+
179
+ **Type Declaration**
180
+
181
+ ```ts
182
+ interface StructuredTextItem {
183
+ str: string
184
+ /** X position in PDF coordinate space (origin: bottom-left). */
185
+ x: number
186
+ /** Y position in PDF coordinate space (origin: bottom-left). */
187
+ y: number
188
+ width: number
189
+ height: number
190
+ fontSize: number
191
+ fontFamily: string
192
+ /** Text direction: `"ltr"`, `"rtl"`, or `"ttb"`. */
193
+ dir: string
194
+ /** Whether the text item is followed by a line break. */
195
+ hasEOL: boolean
196
+ }
197
+
198
+ function extractTextItems(
159
199
  data: DocumentInitParameters['data'] | PDFDocumentProxy,
160
- options: {
161
- mergePages: true
162
- }
163
200
  ): Promise<{
164
201
  totalPages: number
165
- text: string
202
+ items: StructuredTextItem[][]
166
203
  }>
167
204
  ```
168
205
 
@@ -294,26 +331,15 @@ function renderPageAsImage(
294
331
  scale?: number
295
332
  width?: number
296
333
  height?: number
297
- toDataURL?: false
334
+ toDataURL?: boolean
298
335
  },
299
- ): Promise<ArrayBuffer>
300
- function renderPageAsImage(
301
- data: DocumentInitParameters['data'] | PDFDocumentProxy,
302
- pageNumber: number,
303
- options: {
304
- canvasImport?: () => Promise<typeof import('@napi-rs/canvas')>
305
- /** @default 1.0 */
306
- scale?: number
307
- width?: number
308
- height?: number
309
- toDataURL: true
310
- },
311
- ): Promise<string>
336
+ ): Promise<ArrayBuffer | string>
312
337
  ```
313
338
 
314
339
  **Examples**
315
340
 
316
341
  ```ts
342
+ import { readFile, writeFile } from 'node:fs/promises'
317
343
  import { definePDFJSModule, renderPageAsImage } from 'unpdf'
318
344
 
319
345
  // Use the official PDF.js build
@@ -331,6 +357,7 @@ await writeFile('dummy-page-1.png', new Uint8Array(result))
331
357
  ```
332
358
 
333
359
  ```ts
360
+ import { readFile, writeFile } from 'node:fs/promises'
334
361
  import { definePDFJSModule, renderPageAsImage } from 'unpdf'
335
362
 
336
363
  await definePDFJSModule(() => import('pdfjs-dist'))
@@ -360,6 +387,15 @@ const html = `<!DOCTYPE html>
360
387
  await writeFile('dummy-page-1.html', html)
361
388
  ```
362
389
 
390
+ ## Processing Untrusted PDFs
391
+
392
+ PDF.js does not execute scripts embedded in PDFs, but resource limits are your job:
393
+
394
+ - **Image decoding:** `maxImageSize` is unlimited by default – pass e.g. `maxImageSize: 16_777_216` (~16 MP) so a single declared image can't allocate gigabytes.
395
+ - **Page fan-out:** `extractText`, `extractTextItems`, and `extractLinks` process all pages in one call – check `pdf.numPages` against a limit first.
396
+ - **Timeouts:** with the serverless build, parsing runs on your event loop (no worker) – race extraction calls against a timeout.
397
+ - The raw `getDocument` from `unpdf/pdfjs` applies none of these defaults.
398
+
363
399
  ## License
364
400
 
365
401
  [MIT](./LICENSE) License © 2023-PRESENT [Johann Schopplich](https://github.com/johannschopplich)