unpdf 1.8.1 → 1.9.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 (94) hide show
  1. package/README.md +60 -145
  2. package/dist/index.cjs +17 -30
  3. package/dist/index.d.cts +20 -32
  4. package/dist/index.d.mts +20 -32
  5. package/dist/index.mjs +17 -30
  6. package/dist/pdfjs.mjs +37 -37
  7. package/dist/types/external/qcms/qcms.d.ts +11 -7
  8. package/dist/types/external/qcms/qcms_utils.d.ts +1 -6
  9. package/dist/types/src/core/base_stream.d.ts +8 -1
  10. package/dist/types/src/core/catalog.d.ts +12 -12
  11. package/dist/types/src/core/colorspace.d.ts +6 -15
  12. package/dist/types/src/core/core_utils.d.ts +18 -14
  13. package/dist/types/src/core/crypto.d.ts +0 -1
  14. package/dist/types/src/core/file_spec.d.ts +4 -9
  15. package/dist/types/src/core/icc_colorspace.d.ts +0 -1
  16. package/dist/types/src/core/image_resizer.d.ts +24 -0
  17. package/dist/types/src/core/image_utils.d.ts +4 -4
  18. package/dist/types/src/core/jpeg_stream.d.ts +1 -1
  19. package/dist/types/src/core/jpg.d.ts +9 -13
  20. package/dist/types/src/core/parser.d.ts +0 -2
  21. package/dist/types/src/core/pattern.d.ts +0 -2
  22. package/dist/types/src/core/primitives.d.ts +30 -16
  23. package/dist/types/src/core/sasl_prep.d.ts +1 -0
  24. package/dist/types/src/core/sound.d.ts +39 -0
  25. package/dist/types/src/core/string_utils.d.ts +1 -1
  26. package/dist/types/src/core/struct_tree.d.ts +3 -3
  27. package/dist/types/src/core/xml_parser.d.ts +1 -2
  28. package/dist/types/src/core/xref.d.ts +2 -15
  29. package/dist/types/src/display/annotation_layer.d.ts +9 -20
  30. package/dist/types/src/display/annotation_storage.d.ts +7 -7
  31. package/dist/types/src/display/api.d.ts +155 -108
  32. package/dist/types/src/display/canvas.d.ts +2 -1
  33. package/dist/types/src/display/canvas_dependency_tracker.d.ts +8 -3
  34. package/dist/types/src/display/content_disposition.d.ts +0 -1
  35. package/dist/types/src/display/display_utils.d.ts +2 -2
  36. package/dist/types/src/display/draw_layer.d.ts +2 -6
  37. package/dist/types/src/display/editor/annotation_editor_layer.d.ts +18 -19
  38. package/dist/types/src/display/editor/color_picker.d.ts +1 -1
  39. package/dist/types/src/display/editor/comment.d.ts +1 -1
  40. package/dist/types/src/display/editor/draw.d.ts +78 -27
  41. package/dist/types/src/display/editor/drawers/freedraw.d.ts +11 -9
  42. package/dist/types/src/display/editor/drawers/highlight.d.ts +99 -15
  43. package/dist/types/src/display/editor/drawers/inkdraw.d.ts +7 -2
  44. package/dist/types/src/display/editor/drawers/outline.d.ts +65 -2
  45. package/dist/types/src/display/editor/editor.d.ts +24 -22
  46. package/dist/types/src/display/editor/freetext.d.ts +1 -1
  47. package/dist/types/src/display/editor/highlight.d.ts +44 -35
  48. package/dist/types/src/display/editor/ink.d.ts +7 -2
  49. package/dist/types/src/display/editor/stamp.d.ts +1 -1
  50. package/dist/types/src/display/editor/tools.d.ts +21 -19
  51. package/dist/types/src/display/filter_factory.d.ts +1 -3
  52. package/dist/types/src/display/font_loader.d.ts +1 -15
  53. package/dist/types/src/display/network_utils.d.ts +1 -0
  54. package/dist/types/src/display/obj_bin_transform_display.d.ts +25 -38
  55. package/dist/types/src/display/page_viewport.d.ts +5 -5
  56. package/dist/types/src/display/pages_mapper.d.ts +10 -4
  57. package/dist/types/src/display/pattern_helper.d.ts +4 -3
  58. package/dist/types/src/display/pdf_objects.d.ts +1 -3
  59. package/dist/types/src/display/text_layer.d.ts +2 -0
  60. package/dist/types/src/display/touch_manager.d.ts +46 -9
  61. package/dist/types/src/display/worker_options.d.ts +2 -2
  62. package/dist/types/src/display/xfa_layer.d.ts +7 -12
  63. package/dist/types/src/display/xfa_text.d.ts +2 -5
  64. package/dist/types/src/pdf.d.ts +2 -1
  65. package/dist/types/src/shared/base_pdf_stream.d.ts +6 -7
  66. package/dist/types/src/shared/css_utils.d.ts +8 -0
  67. package/dist/types/src/shared/message_handler.d.ts +2 -11
  68. package/dist/types/src/shared/obj_bin_transform_utils.d.ts +4 -3
  69. package/dist/types/src/shared/util.d.ts +4 -6
  70. package/dist/types/web/annotation_editor_layer_builder.d.ts +5 -3
  71. package/dist/types/web/annotation_layer_builder.d.ts +8 -12
  72. package/dist/types/web/draw_layer_builder.d.ts +7 -9
  73. package/dist/types/web/event_utils.d.ts +12 -13
  74. package/dist/types/web/genericl10n.d.ts +2 -2
  75. package/dist/types/web/l10n.d.ts +1 -1
  76. package/dist/types/web/pdf_find_controller.d.ts +5 -6
  77. package/dist/types/web/pdf_find_utils.d.ts +8 -14
  78. package/dist/types/web/pdf_history.d.ts +1 -1
  79. package/dist/types/web/pdf_link_service.d.ts +5 -7
  80. package/dist/types/web/pdf_page_view.d.ts +3 -3
  81. package/dist/types/web/pdf_rendering_queue.d.ts +4 -5
  82. package/dist/types/web/pdf_scripting_manager.d.ts +4 -4
  83. package/dist/types/web/pdf_thumbnail_view.d.ts +2 -2
  84. package/dist/types/web/pdf_thumbnail_viewer.d.ts +14 -15
  85. package/dist/types/web/pdf_viewer.d.ts +36 -16
  86. package/dist/types/web/renderable_view.d.ts +1 -1
  87. package/dist/types/web/sidebar.d.ts +3 -3
  88. package/dist/types/web/struct_tree_layer_builder.d.ts +20 -6
  89. package/dist/types/web/text_accessibility.d.ts +4 -2
  90. package/dist/types/web/text_highlighter.d.ts +1 -2
  91. package/dist/types/web/text_layer_builder.d.ts +5 -5
  92. package/dist/types/web/ui_utils.d.ts +8 -13
  93. package/dist/types/web/xfa_layer_builder.d.ts +7 -7
  94. package/package.json +12 -11
package/README.md CHANGED
@@ -1,16 +1,17 @@
1
1
  # unpdf
2
2
 
3
- Utilities for PDF extraction and rendering across all JavaScript runtimes – Node.js, Deno, Bun, the browser, and serverless environments like Cloudflare Workers. Especially useful for AI applications that need to summarize or analyze PDF documents.
3
+ Utilities for PDF extraction and rendering across all JavaScript runtimes – Node.js, Bun, the browser, and serverless environments like Cloudflare Workers. Especially useful for AI applications that need to summarize or analyze PDF documents.
4
4
 
5
5
  Ships with a serverless build of Mozilla's [PDF.js](https://github.com/mozilla/pdf.js), optimized for edge environments. If you're coming from [`pdf-parse`](https://www.npmjs.com/package/pdf-parse), `unpdf` is a modern, actively maintained alternative with broader runtime support.
6
6
 
7
7
  ## Features
8
8
 
9
- - 🏗️ Works in Node.js, browser and serverless environments
10
- - 🪭 Includes serverless build of PDF.js ([`unpdf/pdfjs`](./package.json))
11
- - 💬 Extract [text](#extract-text-from-pdf), [links](#extractlinks), and [images](#extractimages) from PDF files
9
+ - 🏗️ Works in Node.js 22+, Bun, browsers, and serverless environments ([Cloudflare Workers example](./examples/cloudflare))
10
+ - 🪭 Includes serverless build of PDF.js ([`unpdf/pdfjs`](#how-it-works))
11
+ - 💬 Extract [text](#extracttext), [positioned text](#extracttextitems), [links](#extractlinks), [images](#extractimages), and [metadata](#getmeta) from PDF files
12
+ - 🖼️ [Render pages as images](#renderpageasimage)
12
13
  - 🧠 Perfect for AI applications and PDF summarization
13
- - 🧱 Opt-in to official or legacy PDF.js build
14
+ - 🧱 Opt-in to a [custom PDF.js build](#custom-pdfjs-build)
14
15
 
15
16
  ## Installation
16
17
 
@@ -24,8 +25,6 @@ npm install unpdf
24
25
 
25
26
  ## Usage
26
27
 
27
- ### Extract Text From PDF
28
-
29
28
  ```ts
30
29
  import { extractText, getDocumentProxy } from 'unpdf'
31
30
 
@@ -40,70 +39,22 @@ console.log(`Total pages: ${totalPages}`)
40
39
  console.log(text)
41
40
  ```
42
41
 
43
- ### Official or Legacy PDF.js Build
44
-
45
- Usually you don't need to worry about the PDF.js build. `unpdf` ships with a serverless build of the latest PDF.js version. However, if you want to use the official PDF.js version or the legacy build, you can define a custom PDF.js module.
46
-
47
- > [!WARNING]
48
- > PDF.js v5.x uses `Promise.withResolvers`, which may not be supported in all environments, such as Node < 22. Consider using the bundled serverless build, which includes a polyfill, or use an older version of PDF.js.
49
-
50
- For example, if you want to use the official PDF.js build:
51
-
52
- ```ts
53
- import { definePDFJSModule, extractText, getDocumentProxy } from 'unpdf'
54
-
55
- // Define the PDF.js build before using any other unpdf method
56
- await definePDFJSModule(() => import('pdfjs-dist'))
57
-
58
- // Now, you can use all unpdf methods with the official PDF.js build
59
- const pdf = await getDocumentProxy(/* … */)
60
- const { text } = await extractText(pdf)
61
- ```
62
-
63
- ### PDF.js API
64
-
65
- `unpdf` provides helpful [methods](#api) to work with PDF files, such as `extractText` and `extractImages`, which should cover most use cases. However, if you need more control over the PDF.js API, you can use the `getResolvedPDFJS` method to get the resolved PDF.js module.
66
-
67
- Access the PDF.js API directly by calling `getResolvedPDFJS`:
68
-
69
- ```ts
70
- import { getResolvedPDFJS } from 'unpdf'
71
-
72
- const { version } = await getResolvedPDFJS()
73
- ```
74
-
75
- > [!NOTE]
76
- > If no other PDF.js build was defined, the serverless build will always be used.
77
-
78
- For example, you can use the `getDocument` method to load a PDF file and then use the `getMetadata` method to get the metadata of the PDF file:
79
-
80
- ```ts
81
- import { readFile } from 'node:fs/promises'
82
- import { getResolvedPDFJS } from 'unpdf'
83
-
84
- const { getDocument } = await getResolvedPDFJS()
85
- const data = await readFile('./dummy.pdf')
86
- const document = await getDocument(new Uint8Array(data)).promise
87
-
88
- console.log(await document.getMetadata())
89
- ```
90
-
91
42
  ## How It Works
92
43
 
93
44
  > [!NOTE]
94
- > The serverless PDF.js bundle is built from PDF.js v5.6.205.
45
+ > The serverless PDF.js bundle is built from PDF.js v6.4.299. It requires the global `Iterator` constructor (Safari 18.4+); without it, importing the bundle throws.
95
46
 
96
47
  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
48
 
98
49
  - **String replacements** strip browser-specific references from the PDF.js source.
99
50
  - **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 `Promise.withResolvers` and `FinalizationRegistry` (unavailable in Node.js < 22 and Cloudflare Workers, respectively).
51
+ - **Global polyfills** fill in newer APIs like `Promise.try` and `Map.prototype.getOrInsert` for runtimes that lack them, plus `FinalizationRegistry` for Cloudflare Workers.
101
52
 
102
53
  ## API
103
54
 
104
55
  ### `definePDFJSModule`
105
56
 
106
- Allows to define a custom PDF.js build. This method should be called before using any other method. If no custom build is defined, the serverless build will be used.
57
+ Sets the PDF.js build every other method uses – call it before any of them. See [Custom PDF.js Build](#custom-pdfjs-build).
107
58
 
108
59
  **Type Declaration**
109
60
 
@@ -113,7 +64,7 @@ function definePDFJSModule(pdfjs: () => Promise<PDFJS>): Promise<void>
113
64
 
114
65
  ### `getResolvedPDFJS`
115
66
 
116
- Returns the resolved PDF.js module. If no other PDF.js build was defined, the serverless build will be used. This method is useful if you want to use the PDF.js API directly.
67
+ Returns the resolved PDF.js module – the serverless build, unless `definePDFJSModule` set another. Use it to call the PDF.js API directly.
117
68
 
118
69
  **Type Declaration**
119
70
 
@@ -121,11 +72,19 @@ Returns the resolved PDF.js module. If no other PDF.js build was defined, the se
121
72
  function getResolvedPDFJS(): Promise<PDFJS>
122
73
  ```
123
74
 
75
+ **Example**
76
+
77
+ ```ts
78
+ import { getResolvedPDFJS } from 'unpdf'
79
+
80
+ const { version } = await getResolvedPDFJS()
81
+ ```
82
+
124
83
  ### `getDocumentProxy`
125
84
 
126
85
  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
86
 
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)).
87
+ 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. Without `pdfjs-dist` installed, the Node.js defaults are skipped.
129
88
 
130
89
  **Type Declaration**
131
90
 
@@ -136,6 +95,17 @@ function getDocumentProxy(
136
95
  ): Promise<PDFDocumentProxy>
137
96
  ```
138
97
 
98
+ **Example**
99
+
100
+ Your options override the defaults:
101
+
102
+ ```ts
103
+ const pdf = await getDocumentProxy(buffer, {
104
+ disableFontFace: false,
105
+ standardFontDataUrl: 'https://unpkg.com/pdfjs-dist@latest/standard_fonts/',
106
+ })
107
+ ```
108
+
139
109
  ### `getMeta`
140
110
 
141
111
  Extracts metadata from a PDF. If `parseDates` is set to `true`, the date properties will be parsed into `Date` objects.
@@ -238,7 +208,7 @@ for (const link of links) console.log(link)
238
208
 
239
209
  ### `extractImages`
240
210
 
241
- Extracts images from a specific page of a PDF document, including necessary metadata such as width, height, and calculated color channels. Works with both the serverless and official PDF.js build.
211
+ Extracts images from a specific page of a PDF document, including necessary metadata such as width, height, and calculated color channels.
242
212
 
243
213
  **Type Declaration**
244
214
 
@@ -259,65 +229,31 @@ function extractImages(
259
229
 
260
230
  **Example**
261
231
 
262
- > [!NOTE]
263
- > The following example uses the [sharp](https://github.com/lovell/sharp) library to process and save the extracted images. You will need to install it with your preferred package manager.
232
+ Saves each image as PNG with [sharp](https://github.com/lovell/sharp), installed separately:
264
233
 
265
234
  ```ts
266
- import { readFile, writeFile } from 'node:fs/promises'
235
+ import { readFile } from 'node:fs/promises'
267
236
  import sharp from 'sharp'
268
237
  import { extractImages, getDocumentProxy } from 'unpdf'
269
238
 
270
- async function extractPdfImages() {
271
- const buffer = await readFile('./document.pdf')
272
- const pdf = await getDocumentProxy(new Uint8Array(buffer))
273
-
274
- // Extract images from page 1
275
- const imagesData = await extractImages(pdf, 1)
276
- console.log(`Found ${imagesData.length} images on page 1`)
277
-
278
- // Process each image with sharp (optional)
279
- let totalImagesProcessed = 0
280
- for (const imgData of imagesData) {
281
- const imageIndex = ++totalImagesProcessed
282
-
283
- await sharp(imgData.data, {
284
- raw: {
285
- width: imgData.width,
286
- height: imgData.height,
287
- channels: imgData.channels
288
- }
289
- })
290
- .png()
291
- .toFile(`image-${imageIndex}.png`)
292
-
293
- console.log(`Saved image ${imageIndex} (${imgData.width}x${imgData.height}, ${imgData.channels} channels)`)
294
- }
239
+ const buffer = await readFile('./document.pdf')
240
+ const pdf = await getDocumentProxy(new Uint8Array(buffer))
241
+ const images = await extractImages(pdf, 1)
242
+
243
+ for (const [index, image] of images.entries()) {
244
+ await sharp(image.data, {
245
+ raw: { width: image.width, height: image.height, channels: image.channels },
246
+ })
247
+ .png()
248
+ .toFile(`image-${index + 1}.png`)
295
249
  }
296
-
297
- extractPdfImages().catch(console.error)
298
250
  ```
299
251
 
300
252
  ### `renderPageAsImage`
301
253
 
302
- To render a PDF page as an image, you can use the `renderPageAsImage` method. This method will return an `ArrayBuffer` of the rendered image. It can also return a data URL (`string`) if `toDataURL` option is set to `true`.
254
+ Renders a PDF page as PNG and returns its bytes as an `ArrayBuffer`, or a data URL (`string`) with `toDataURL: true`.
303
255
 
304
- > [!NOTE]
305
- > This method will only work in Node.js and browser environments.
306
-
307
- In order to use this method, make sure to meet the following requirements:
308
-
309
- - Use the official PDF.js build (see [Official or Legacy PDF.js Build](#official-or-legacy-pdfjs-build)).
310
- - Install the [`@napi-rs/canvas`](https://github.com/Brooooooklyn/canvas) package if you are using Node.js. This package is required to render the PDF page as an image.
311
-
312
- > [!TIP]
313
- > In Node.js, `getDocumentProxy` automatically sets `disableFontFace: true` and resolves `standardFontDataUrl` from your local `pdfjs-dist` package for correct font rendering. To customize this behavior, pass your own options:
314
- >
315
- > ```ts
316
- > const pdf = await getDocumentProxy(buffer, {
317
- > disableFontFace: false,
318
- > standardFontDataUrl: 'https://unpkg.com/pdfjs-dist@latest/standard_fonts/',
319
- > })
320
- > ```
256
+ Rendering needs a canvas: browsers bring their own, while Node.js and Bun need the [`@napi-rs/canvas`](https://github.com/Brooooooklyn/canvas) package, passed as `canvasImport`. Serverless runtimes like Cloudflare Workers have neither.
321
257
 
322
258
  **Type Declaration**
323
259
 
@@ -336,57 +272,36 @@ function renderPageAsImage(
336
272
  ): Promise<ArrayBuffer | string>
337
273
  ```
338
274
 
339
- **Examples**
275
+ **Example**
340
276
 
341
277
  ```ts
342
278
  import { readFile, writeFile } from 'node:fs/promises'
343
- import { definePDFJSModule, renderPageAsImage } from 'unpdf'
279
+ import { renderPageAsImage } from 'unpdf'
344
280
 
345
- // Use the official PDF.js build
346
- await definePDFJSModule(() => import('pdfjs-dist'))
347
-
348
- const pdf = await readFile('./dummy.pdf')
349
- const buffer = new Uint8Array(pdf)
350
- const pageNumber = 1
351
-
352
- const result = await renderPageAsImage(buffer, pageNumber, {
281
+ const buffer = await readFile('./dummy.pdf')
282
+ const image = await renderPageAsImage(new Uint8Array(buffer), 1, {
353
283
  canvasImport: () => import('@napi-rs/canvas'),
354
284
  scale: 2,
355
285
  })
356
- await writeFile('dummy-page-1.png', new Uint8Array(result))
286
+
287
+ await writeFile('dummy-page-1.png', new Uint8Array(image))
357
288
  ```
358
289
 
359
- ```ts
360
- import { readFile, writeFile } from 'node:fs/promises'
361
- import { definePDFJSModule, renderPageAsImage } from 'unpdf'
290
+ ## Custom PDF.js Build
362
291
 
363
- await definePDFJSModule(() => import('pdfjs-dist'))
292
+ `unpdf` uses its serverless build unless you define another one, e.g. to pin a different PDF.js version. Install `pdfjs-dist` and call `definePDFJSModule` before any other method:
364
293
 
365
- const pdf = await readFile('./dummy.pdf')
366
- const buffer = new Uint8Array(pdf)
367
- const pageNumber = 1
294
+ ```ts
295
+ import { definePDFJSModule, extractText, getDocumentProxy } from 'unpdf'
368
296
 
369
- const result = await renderPageAsImage(buffer, pageNumber, {
370
- canvasImport: () => import('@napi-rs/canvas'),
371
- scale: 2,
372
- toDataURL: true,
373
- })
297
+ await definePDFJSModule(() => import('pdfjs-dist/legacy/build/pdf.mjs'))
374
298
 
375
- const html = `<!DOCTYPE html>
376
- <html lang="en">
377
- <head>
378
- <meta charset="UTF-8">
379
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
380
- <title>Dummy Page</title>
381
- </head>
382
- <body>
383
- <img alt="Example Page" src="${result}">
384
- </body>
385
- </html>`
386
-
387
- await writeFile('dummy-page-1.html', html)
299
+ const pdf = await getDocumentProxy(/* … */)
300
+ const { text } = await extractText(pdf)
388
301
  ```
389
302
 
303
+ In Node.js, import the legacy build as shown. The default `pdfjs-dist` entry targets current browsers – it fails on Node.js 24 and older and warns on newer versions.
304
+
390
305
  ## Processing Untrusted PDFs
391
306
 
392
307
  PDF.js does not execute scripts embedded in PDFs, but resource limits are your job:
package/dist/index.cjs CHANGED
@@ -53,7 +53,7 @@ let resolvedModule;
53
53
  const isNode = globalThis.process?.release?.name === "node";
54
54
  const isBrowser = typeof window !== "undefined";
55
55
  /**
56
- * Returns a `PDFDocumentProxy` instance from a given binary data.
56
+ * Creates a `PDFDocumentProxy` from binary PDF data.
57
57
  *
58
58
  * Applies the following defaults:
59
59
  * - `useSystemFonts: true`
@@ -209,18 +209,6 @@ function injectCanvasConstructors() {
209
209
  /**
210
210
  * Extracts images from a specific page of a PDF document, including necessary metadata,
211
211
  * such as width, height, and calculated color channels.
212
- *
213
- * @example
214
- * const imagesData = await extractImages(pdf, pageNum)
215
- *
216
- * for (const imgData of imagesData) {
217
- * const imageIndex = totalImagesProcessed + 1
218
- * await sharp(imgData.data, {
219
- * raw: { width: imgData.width, height: imgData.height, channels: imgData.channels }
220
- * })
221
- * .png()
222
- * .toFile(`${imageIndex}.png`)
223
- * }
224
212
  */
225
213
  async function extractImages$1(data, pageNumber) {
226
214
  return await withDocument(data, async (pdf) => {
@@ -327,23 +315,23 @@ const XMP_DATE_PROPERTIES = [
327
315
  async function getMeta$1(data, options = {}) {
328
316
  return await withDocument(data, async (pdf) => {
329
317
  const meta = await pdf.getMetadata();
330
- const info = meta?.info || {};
318
+ const info = { ...meta.info };
319
+ let metadata = meta.metadata;
331
320
  if (options.parseDates) {
332
321
  const { PDFDateString } = await getResolvedPDFJS();
333
- if (info?.CreationDate) info.CreationDate = PDFDateString.toDateObject(info?.CreationDate);
334
- if (info?.ModDate) info.ModDate = PDFDateString.toDateObject(info?.ModDate);
335
- if (meta.metadata) meta.metadata = new Proxy(meta.metadata, { get(target, prop) {
336
- if (prop === "get") return (name) => {
337
- const value = target.get(name);
338
- if (XMP_DATE_PROPERTIES.includes(name) && value) return parseISODateString(value);
339
- return value;
340
- };
341
- return target[prop];
342
- } });
322
+ if (info.CreationDate) info.CreationDate = PDFDateString.toDateObject(info.CreationDate);
323
+ if (info.ModDate) info.ModDate = PDFDateString.toDateObject(info.ModDate);
324
+ if (metadata) {
325
+ const Metadata = metadata.constructor;
326
+ metadata = new Metadata({
327
+ parsedData: new Map(Array.from(metadata, ([name, value]) => [name, XMP_DATE_PROPERTIES.includes(name) && value ? parseISODateString(value) : value])),
328
+ rawData: metadata.getRaw()
329
+ });
330
+ }
343
331
  }
344
332
  return {
345
333
  info,
346
- metadata: meta?.metadata || {}
334
+ metadata: metadata || {}
347
335
  };
348
336
  });
349
337
  }
@@ -404,15 +392,14 @@ function normalizeMergedText(texts) {
404
392
  //#endregion
405
393
  //#region src/config.ts
406
394
  /**
407
- * By default, unpdf will use the latest version of PDF.js compiled for
408
- * serverless environments. If you want to use a different version, you can
409
- * provide a custom resolver function.
395
+ * Sets the PDF.js build every other method uses – call it before any of them.
396
+ * Without it, the serverless build is used.
410
397
  *
411
398
  * @example
412
- * // Use the official PDF.js build (make sure to install it first)
399
+ * // In Node.js, the legacy build of `pdfjs-dist` (install it first)
413
400
  * import { definePDFJSModule } from 'unpdf'
414
401
  *
415
- * await definePDFJSModule(() => import('pdfjs-dist'))
402
+ * await definePDFJSModule(() => import('pdfjs-dist/legacy/build/pdf.mjs'))
416
403
  */
417
404
  async function definePDFJSModule(pdfjs) {
418
405
  await resolvePDFJSImport(pdfjs, { reload: true });
package/dist/index.d.cts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { DocumentInitParameters, PDFDocumentProxy } from "./types/src/display/api.js";
2
2
  import { Canvas, SKRSContext2D } from "@napi-rs/canvas";
3
+ import { Metadata } from "./types/src/display/metadata.js";
3
4
  import * as PDFJS from "./types/src/pdf.js";
4
5
  //#region src/_internal/canvas.d.ts
5
6
  interface CanvasFactoryContext {
@@ -55,18 +56,6 @@ interface ExtractedImageObject {
55
56
  /**
56
57
  * Extracts images from a specific page of a PDF document, including necessary metadata,
57
58
  * such as width, height, and calculated color channels.
58
- *
59
- * @example
60
- * const imagesData = await extractImages(pdf, pageNum)
61
- *
62
- * for (const imgData of imagesData) {
63
- * const imageIndex = totalImagesProcessed + 1
64
- * await sharp(imgData.data, {
65
- * raw: { width: imgData.width, height: imgData.height, channels: imgData.channels }
66
- * })
67
- * .png()
68
- * .toFile(`${imageIndex}.png`)
69
- * }
70
59
  */
71
60
  declare function extractImages$1(data: DocumentInitParameters['data'] | PDFDocumentProxy, pageNumber: number): Promise<ExtractedImageObject[]>;
72
61
  declare function renderPageAsImage$1(data: DocumentInitParameters['data'] | PDFDocumentProxy, pageNumber: number, options?: {
@@ -85,7 +74,7 @@ declare function renderPageAsImage$1(data: DocumentInitParameters['data'] | PDFD
85
74
  height?: number;
86
75
  toDataURL: true;
87
76
  }): Promise<string>;
88
- declare function createIsomorphicCanvasFactory(canvasImport?: () => Promise<typeof import('@napi-rs/canvas')>): Promise<typeof DOMCanvasFactory | typeof NodeCanvasFactory>;
77
+ export declare function createIsomorphicCanvasFactory(canvasImport?: () => Promise<typeof import('@napi-rs/canvas')>): Promise<typeof DOMCanvasFactory | typeof NodeCanvasFactory>;
89
78
  //#endregion
90
79
  //#region src/links.d.ts
91
80
  declare function extractLinks$1(data: DocumentInitParameters['data'] | PDFDocumentProxy): Promise<{
@@ -98,7 +87,7 @@ declare function getMeta$1(data: DocumentInitParameters['data'] | PDFDocumentPro
98
87
  parseDates?: boolean;
99
88
  }): Promise<{
100
89
  info: Record<string, any>;
101
- metadata: import("./types/src/display/metadata.js").Metadata;
90
+ metadata: Metadata;
102
91
  }>;
103
92
  //#endregion
104
93
  //#region src/text.d.ts
@@ -147,25 +136,24 @@ declare function extractText$1(data: DocumentInitParameters['data'] | PDFDocumen
147
136
  //#endregion
148
137
  //#region src/config.d.ts
149
138
  /**
150
- * By default, unpdf will use the latest version of PDF.js compiled for
151
- * serverless environments. If you want to use a different version, you can
152
- * provide a custom resolver function.
139
+ * Sets the PDF.js build every other method uses – call it before any of them.
140
+ * Without it, the serverless build is used.
153
141
  *
154
142
  * @example
155
- * // Use the official PDF.js build (make sure to install it first)
143
+ * // In Node.js, the legacy build of `pdfjs-dist` (install it first)
156
144
  * import { definePDFJSModule } from 'unpdf'
157
145
  *
158
- * await definePDFJSModule(() => import('pdfjs-dist'))
146
+ * await definePDFJSModule(() => import('pdfjs-dist/legacy/build/pdf.mjs'))
159
147
  */
160
- declare function definePDFJSModule(pdfjs: () => Promise<any>): Promise<void>;
148
+ export declare function definePDFJSModule(pdfjs: () => Promise<any>): Promise<void>;
161
149
  /** @deprecated Use `definePDFJSModule` instead. Will be removed in v2. */
162
- declare function configureUnPDF(options: {
150
+ export declare function configureUnPDF(options: {
163
151
  pdfjs?: () => Promise<any>;
164
152
  }): Promise<void>;
165
153
  //#endregion
166
154
  //#region src/utils.d.ts
167
155
  /**
168
- * Returns a `PDFDocumentProxy` instance from a given binary data.
156
+ * Creates a `PDFDocumentProxy` from binary PDF data.
169
157
  *
170
158
  * Applies the following defaults:
171
159
  * - `useSystemFonts: true`
@@ -175,18 +163,18 @@ declare function configureUnPDF(options: {
175
163
  * - `standardFontDataUrl` resolved from the local `pdfjs-dist` package
176
164
  * - `cMapUrl` and `cMapPacked` resolved from the local `pdfjs-dist` package
177
165
  */
178
- declare function getDocumentProxy(data: DocumentInitParameters['data'], options?: DocumentInitParameters): Promise<PDFDocumentProxy>;
179
- declare function getResolvedPDFJS(): Promise<typeof PDFJS>;
180
- declare function resolvePDFJSImport(pdfjsResolver?: () => Promise<any>, { reload }?: {
166
+ export declare function getDocumentProxy(data: DocumentInitParameters['data'], options?: DocumentInitParameters): Promise<PDFDocumentProxy>;
167
+ export declare function getResolvedPDFJS(): Promise<typeof PDFJS>;
168
+ export declare function resolvePDFJSImport(pdfjsResolver?: () => Promise<any>, { reload }?: {
181
169
  reload?: boolean | undefined;
182
170
  }): Promise<void>;
183
171
  //#endregion
184
172
  //#region src/index.d.ts
185
- declare const getMeta: typeof getMeta$1;
186
- declare const extractText: typeof extractText$1;
187
- declare const extractTextItems: typeof extractTextItems$1;
188
- declare const extractImages: typeof extractImages$1;
189
- declare const renderPageAsImage: typeof renderPageAsImage$1;
190
- declare const extractLinks: typeof extractLinks$1;
173
+ export declare const getMeta: typeof getMeta$1;
174
+ export declare const extractText: typeof extractText$1;
175
+ export declare const extractTextItems: typeof extractTextItems$1;
176
+ export declare const extractImages: typeof extractImages$1;
177
+ export declare const renderPageAsImage: typeof renderPageAsImage$1;
178
+ export declare const extractLinks: typeof extractLinks$1;
191
179
  //#endregion
192
- export { type StructuredTextItem, configureUnPDF, createIsomorphicCanvasFactory, definePDFJSModule, extractImages, extractLinks, extractText, extractTextItems, getDocumentProxy, getMeta, getResolvedPDFJS, renderPageAsImage, resolvePDFJSImport };
180
+ export type { StructuredTextItem };
package/dist/index.d.mts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { DocumentInitParameters, PDFDocumentProxy } from "./types/src/display/api.js";
2
2
  import { Canvas, SKRSContext2D } from "@napi-rs/canvas";
3
+ import { Metadata } from "./types/src/display/metadata.js";
3
4
  import * as PDFJS from "./types/src/pdf.js";
4
5
  //#region src/_internal/canvas.d.ts
5
6
  interface CanvasFactoryContext {
@@ -55,18 +56,6 @@ interface ExtractedImageObject {
55
56
  /**
56
57
  * Extracts images from a specific page of a PDF document, including necessary metadata,
57
58
  * such as width, height, and calculated color channels.
58
- *
59
- * @example
60
- * const imagesData = await extractImages(pdf, pageNum)
61
- *
62
- * for (const imgData of imagesData) {
63
- * const imageIndex = totalImagesProcessed + 1
64
- * await sharp(imgData.data, {
65
- * raw: { width: imgData.width, height: imgData.height, channels: imgData.channels }
66
- * })
67
- * .png()
68
- * .toFile(`${imageIndex}.png`)
69
- * }
70
59
  */
71
60
  declare function extractImages$1(data: DocumentInitParameters['data'] | PDFDocumentProxy, pageNumber: number): Promise<ExtractedImageObject[]>;
72
61
  declare function renderPageAsImage$1(data: DocumentInitParameters['data'] | PDFDocumentProxy, pageNumber: number, options?: {
@@ -85,7 +74,7 @@ declare function renderPageAsImage$1(data: DocumentInitParameters['data'] | PDFD
85
74
  height?: number;
86
75
  toDataURL: true;
87
76
  }): Promise<string>;
88
- declare function createIsomorphicCanvasFactory(canvasImport?: () => Promise<typeof import('@napi-rs/canvas')>): Promise<typeof DOMCanvasFactory | typeof NodeCanvasFactory>;
77
+ export declare function createIsomorphicCanvasFactory(canvasImport?: () => Promise<typeof import('@napi-rs/canvas')>): Promise<typeof DOMCanvasFactory | typeof NodeCanvasFactory>;
89
78
  //#endregion
90
79
  //#region src/links.d.ts
91
80
  declare function extractLinks$1(data: DocumentInitParameters['data'] | PDFDocumentProxy): Promise<{
@@ -98,7 +87,7 @@ declare function getMeta$1(data: DocumentInitParameters['data'] | PDFDocumentPro
98
87
  parseDates?: boolean;
99
88
  }): Promise<{
100
89
  info: Record<string, any>;
101
- metadata: import("./types/src/display/metadata.js").Metadata;
90
+ metadata: Metadata;
102
91
  }>;
103
92
  //#endregion
104
93
  //#region src/text.d.ts
@@ -147,25 +136,24 @@ declare function extractText$1(data: DocumentInitParameters['data'] | PDFDocumen
147
136
  //#endregion
148
137
  //#region src/config.d.ts
149
138
  /**
150
- * By default, unpdf will use the latest version of PDF.js compiled for
151
- * serverless environments. If you want to use a different version, you can
152
- * provide a custom resolver function.
139
+ * Sets the PDF.js build every other method uses – call it before any of them.
140
+ * Without it, the serverless build is used.
153
141
  *
154
142
  * @example
155
- * // Use the official PDF.js build (make sure to install it first)
143
+ * // In Node.js, the legacy build of `pdfjs-dist` (install it first)
156
144
  * import { definePDFJSModule } from 'unpdf'
157
145
  *
158
- * await definePDFJSModule(() => import('pdfjs-dist'))
146
+ * await definePDFJSModule(() => import('pdfjs-dist/legacy/build/pdf.mjs'))
159
147
  */
160
- declare function definePDFJSModule(pdfjs: () => Promise<any>): Promise<void>;
148
+ export declare function definePDFJSModule(pdfjs: () => Promise<any>): Promise<void>;
161
149
  /** @deprecated Use `definePDFJSModule` instead. Will be removed in v2. */
162
- declare function configureUnPDF(options: {
150
+ export declare function configureUnPDF(options: {
163
151
  pdfjs?: () => Promise<any>;
164
152
  }): Promise<void>;
165
153
  //#endregion
166
154
  //#region src/utils.d.ts
167
155
  /**
168
- * Returns a `PDFDocumentProxy` instance from a given binary data.
156
+ * Creates a `PDFDocumentProxy` from binary PDF data.
169
157
  *
170
158
  * Applies the following defaults:
171
159
  * - `useSystemFonts: true`
@@ -175,18 +163,18 @@ declare function configureUnPDF(options: {
175
163
  * - `standardFontDataUrl` resolved from the local `pdfjs-dist` package
176
164
  * - `cMapUrl` and `cMapPacked` resolved from the local `pdfjs-dist` package
177
165
  */
178
- declare function getDocumentProxy(data: DocumentInitParameters['data'], options?: DocumentInitParameters): Promise<PDFDocumentProxy>;
179
- declare function getResolvedPDFJS(): Promise<typeof PDFJS>;
180
- declare function resolvePDFJSImport(pdfjsResolver?: () => Promise<any>, { reload }?: {
166
+ export declare function getDocumentProxy(data: DocumentInitParameters['data'], options?: DocumentInitParameters): Promise<PDFDocumentProxy>;
167
+ export declare function getResolvedPDFJS(): Promise<typeof PDFJS>;
168
+ export declare function resolvePDFJSImport(pdfjsResolver?: () => Promise<any>, { reload }?: {
181
169
  reload?: boolean | undefined;
182
170
  }): Promise<void>;
183
171
  //#endregion
184
172
  //#region src/index.d.ts
185
- declare const getMeta: typeof getMeta$1;
186
- declare const extractText: typeof extractText$1;
187
- declare const extractTextItems: typeof extractTextItems$1;
188
- declare const extractImages: typeof extractImages$1;
189
- declare const renderPageAsImage: typeof renderPageAsImage$1;
190
- declare const extractLinks: typeof extractLinks$1;
173
+ export declare const getMeta: typeof getMeta$1;
174
+ export declare const extractText: typeof extractText$1;
175
+ export declare const extractTextItems: typeof extractTextItems$1;
176
+ export declare const extractImages: typeof extractImages$1;
177
+ export declare const renderPageAsImage: typeof renderPageAsImage$1;
178
+ export declare const extractLinks: typeof extractLinks$1;
191
179
  //#endregion
192
- export { type StructuredTextItem, configureUnPDF, createIsomorphicCanvasFactory, definePDFJSModule, extractImages, extractLinks, extractText, extractTextItems, getDocumentProxy, getMeta, getResolvedPDFJS, renderPageAsImage, resolvePDFJSImport };
180
+ export type { StructuredTextItem };