unpdf 0.7.0 → 0.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 (51) hide show
  1. package/README.md +14 -12
  2. package/dist/index.cjs +10 -16
  3. package/dist/index.d.cts +2 -0
  4. package/dist/index.d.mts +2 -0
  5. package/dist/index.d.ts +2 -0
  6. package/dist/index.mjs +10 -16
  7. package/dist/pdfjs.mjs +1 -1
  8. package/dist/types/src/display/annotation_layer.d.ts +14 -7
  9. package/dist/types/src/display/annotation_storage.d.ts +4 -4
  10. package/dist/types/src/display/api.d.ts +14 -9
  11. package/dist/types/src/display/content_disposition.d.ts +7 -0
  12. package/dist/types/src/display/display_utils.d.ts +2 -5
  13. package/dist/types/src/display/editor/annotation_editor_layer.d.ts +13 -8
  14. package/dist/types/src/display/editor/editor.d.ts +51 -15
  15. package/dist/types/src/display/editor/freetext.d.ts +2 -9
  16. package/dist/types/src/display/editor/ink.d.ts +6 -12
  17. package/dist/types/src/display/editor/stamp.d.ts +5 -1
  18. package/dist/types/src/display/editor/tools.d.ts +15 -4
  19. package/dist/types/src/display/fetch_stream.d.ts +68 -0
  20. package/dist/types/src/display/font_loader.d.ts +4 -1
  21. package/dist/types/src/display/network.d.ts +83 -0
  22. package/dist/types/src/display/network_utils.d.ts +14 -0
  23. package/dist/types/src/display/node_stream.d.ts +71 -0
  24. package/dist/types/src/display/node_utils.d.ts +27 -0
  25. package/dist/types/src/display/transport_stream.d.ts +3 -0
  26. package/dist/types/src/display/xfa_layer.d.ts +2 -1
  27. package/dist/types/src/display/xfa_text.d.ts +2 -0
  28. package/dist/types/src/interfaces.d.ts +118 -0
  29. package/dist/types/src/pdf.d.ts +3 -3
  30. package/dist/types/src/shared/util.d.ts +1 -0
  31. package/dist/types/web/annotation_editor_layer_builder.d.ts +8 -5
  32. package/dist/types/web/annotation_layer_builder.d.ts +12 -17
  33. package/dist/types/web/app_options.d.ts +2 -1
  34. package/dist/types/web/download_manager.d.ts +1 -1
  35. package/dist/types/web/generic_scripting.d.ts +1 -1
  36. package/dist/types/web/genericl10n.d.ts +14 -7
  37. package/dist/types/web/interfaces.d.ts +16 -8
  38. package/dist/types/web/l10n.d.ts +27 -0
  39. package/dist/types/web/l10n_utils.d.ts +7 -5
  40. package/dist/types/web/pdf_find_controller.d.ts +2 -1
  41. package/dist/types/web/pdf_link_service.d.ts +1 -1
  42. package/dist/types/web/pdf_page_view.d.ts +9 -7
  43. package/dist/types/web/pdf_thumbnail_view.d.ts +8 -15
  44. package/dist/types/web/pdf_thumbnail_viewer.d.ts +2 -8
  45. package/dist/types/web/pdf_viewer.d.ts +20 -14
  46. package/dist/types/web/text_accessibility.d.ts +5 -3
  47. package/dist/types/web/text_layer_builder.d.ts +7 -2
  48. package/dist/types/web/ui_utils.d.ts +5 -9
  49. package/dist/types/web/xfa_layer_builder.d.ts +4 -6
  50. package/package.json +18 -18
  51. package/dist/types/external/webL10n/l10n.d.ts +0 -0
package/README.md CHANGED
@@ -14,6 +14,11 @@ This library is also intended as a modern alternative to the unmaintained but st
14
14
  - 🧱 Opt-in to legacy PDF.js build
15
15
  - 💨 Zero dependencies
16
16
 
17
+ ## PDF.js Compatibility
18
+
19
+ > [!NOTE]
20
+ > This package is currently using PDF.js v4.0.189.
21
+
17
22
  ## Installation
18
23
 
19
24
  Run the following command to add `unpdf` to your project.
@@ -179,8 +184,11 @@ defineUnPDFConfig({
179
184
 
180
185
  const pdf = await readFile("./dummy.pdf");
181
186
  const buffer = new Uint8Array(pdf);
187
+ const pageNumber = 1;
182
188
 
183
- const result = await renderPageAsImage(buffer, 1);
189
+ const result = await renderPageAsImage(buffer, pageNumber, {
190
+ canvas: () => import("canvas"),
191
+ });
184
192
  await writeFile("dummy-page-1.png", Buffer.from(result));
185
193
  ```
186
194
 
@@ -191,6 +199,7 @@ declare function renderPageAsImage(
191
199
  data: BinaryData | PDFDocumentProxy,
192
200
  pageNumber: number,
193
201
  options?: {
202
+ canvas?: () => Promise<typeof import("canvas")>;
194
203
  /** @default 1 */
195
204
  scale?: number;
196
205
  width?: number;
@@ -199,20 +208,13 @@ declare function renderPageAsImage(
199
208
  ): Promise<ArrayBuffer>;
200
209
  ```
201
210
 
202
- ### `extractImages`
203
-
204
- ```ts
205
- function extractImages(
206
- data: BinaryData | PDFDocumentProxy,
207
- pageNumber: number,
208
- ): Promise<Uint8ClampedArray[]>;
209
- ```
210
-
211
211
  ## FAQ
212
212
 
213
- ### What About Canvas?
213
+ ### Why Is `canvas` An Optional Dependency?
214
+
215
+ The official PDF.js library depends on the `canvas` module for Node.js environments, which [doesn't work inside worker threads](https://github.com/Automattic/node-canvas/issues/1394). That's why `unpdf` ships with a serverless build of PDF.js that mocks the `canvas` module.
214
216
 
215
- The official PDF.js library depends on the optional `canvas` module, which [doesn't work inside worker threads](https://github.com/Automattic/node-canvas/issues/1394). That's why `unpdf` ships with a serverless build of PDF.js that mocks the `canvas` module.
217
+ However, to render PDF pages as images in Node.js environments, you need to install the `canvas` module. That's why it is a peer dependency.
216
218
 
217
219
  ## License
218
220
 
package/dist/index.cjs CHANGED
@@ -116,7 +116,7 @@ async function renderPageAsImage$1(data, pageNumber, options = {}) {
116
116
  if (outputScale !== 1 && outputScale > 0) {
117
117
  viewport = page.getViewport({ scale: outputScale });
118
118
  }
119
- const canvasFactory = await createIsomorphicCanvasFactory();
119
+ const canvasFactory = await createIsomorphicCanvasFactory(options.canvas);
120
120
  const ctx = canvasFactory.create(viewport.width, viewport.height);
121
121
  await page.render({
122
122
  canvasContext: ctx.context,
@@ -126,25 +126,19 @@ async function renderPageAsImage$1(data, pageNumber, options = {}) {
126
126
  const response = await fetch(dataUrl);
127
127
  return await response.arrayBuffer();
128
128
  }
129
- async function createIsomorphicCanvasFactory() {
130
- let _canvas;
131
- if (isNode) {
132
- try {
133
- _canvas = await import('canvas');
134
- } catch {
135
- throw new Error(
136
- 'The "canvas" package is required when running in Node.js.'
137
- );
138
- }
139
- }
129
+ async function createIsomorphicCanvasFactory(canvas) {
130
+ const _canvas = await canvas?.();
140
131
  return {
141
132
  _createCanvas(width, height) {
142
133
  if (isBrowser) {
143
- const canvas = document.createElement("canvas");
144
- canvas.width = width;
145
- canvas.height = height;
146
- return canvas;
134
+ const canvas2 = document.createElement("canvas");
135
+ canvas2.width = width;
136
+ canvas2.height = height;
137
+ return canvas2;
147
138
  } else if (isNode) {
139
+ if (!_canvas) {
140
+ throw new Error('Failed to resolve "canvas" package.');
141
+ }
148
142
  return _canvas.createCanvas(width, height);
149
143
  }
150
144
  throw new Error("Unsupported environment for canvas creation.");
package/dist/index.d.cts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { BinaryData, PDFDocumentProxy, DocumentInitParameters } from './types/src/display/api';
2
+ import * as canvas from 'canvas';
2
3
  import * as _PDFJS from './types/src/pdf';
3
4
 
4
5
  declare function getMeta$1(data: BinaryData | PDFDocumentProxy): Promise<{
@@ -15,6 +16,7 @@ declare function extractText$1(data: BinaryData | PDFDocumentProxy, options?: {
15
16
 
16
17
  declare function extractImages$1(data: BinaryData | PDFDocumentProxy, pageNumber: number): Promise<Uint8ClampedArray[]>;
17
18
  declare function renderPageAsImage$1(data: BinaryData | PDFDocumentProxy, pageNumber: number, options?: {
19
+ canvas?: () => Promise<typeof canvas>;
18
20
  /** @default 1 */
19
21
  scale?: number;
20
22
  width?: number;
package/dist/index.d.mts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { BinaryData, PDFDocumentProxy, DocumentInitParameters } from './types/src/display/api';
2
+ import * as canvas from 'canvas';
2
3
  import * as _PDFJS from './types/src/pdf';
3
4
 
4
5
  declare function getMeta$1(data: BinaryData | PDFDocumentProxy): Promise<{
@@ -15,6 +16,7 @@ declare function extractText$1(data: BinaryData | PDFDocumentProxy, options?: {
15
16
 
16
17
  declare function extractImages$1(data: BinaryData | PDFDocumentProxy, pageNumber: number): Promise<Uint8ClampedArray[]>;
17
18
  declare function renderPageAsImage$1(data: BinaryData | PDFDocumentProxy, pageNumber: number, options?: {
19
+ canvas?: () => Promise<typeof canvas>;
18
20
  /** @default 1 */
19
21
  scale?: number;
20
22
  width?: number;
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { BinaryData, PDFDocumentProxy, DocumentInitParameters } from './types/src/display/api';
2
+ import * as canvas from 'canvas';
2
3
  import * as _PDFJS from './types/src/pdf';
3
4
 
4
5
  declare function getMeta$1(data: BinaryData | PDFDocumentProxy): Promise<{
@@ -15,6 +16,7 @@ declare function extractText$1(data: BinaryData | PDFDocumentProxy, options?: {
15
16
 
16
17
  declare function extractImages$1(data: BinaryData | PDFDocumentProxy, pageNumber: number): Promise<Uint8ClampedArray[]>;
17
18
  declare function renderPageAsImage$1(data: BinaryData | PDFDocumentProxy, pageNumber: number, options?: {
19
+ canvas?: () => Promise<typeof canvas>;
18
20
  /** @default 1 */
19
21
  scale?: number;
20
22
  width?: number;
package/dist/index.mjs CHANGED
@@ -114,7 +114,7 @@ async function renderPageAsImage$1(data, pageNumber, options = {}) {
114
114
  if (outputScale !== 1 && outputScale > 0) {
115
115
  viewport = page.getViewport({ scale: outputScale });
116
116
  }
117
- const canvasFactory = await createIsomorphicCanvasFactory();
117
+ const canvasFactory = await createIsomorphicCanvasFactory(options.canvas);
118
118
  const ctx = canvasFactory.create(viewport.width, viewport.height);
119
119
  await page.render({
120
120
  canvasContext: ctx.context,
@@ -124,25 +124,19 @@ async function renderPageAsImage$1(data, pageNumber, options = {}) {
124
124
  const response = await fetch(dataUrl);
125
125
  return await response.arrayBuffer();
126
126
  }
127
- async function createIsomorphicCanvasFactory() {
128
- let _canvas;
129
- if (isNode) {
130
- try {
131
- _canvas = await import('canvas');
132
- } catch {
133
- throw new Error(
134
- 'The "canvas" package is required when running in Node.js.'
135
- );
136
- }
137
- }
127
+ async function createIsomorphicCanvasFactory(canvas) {
128
+ const _canvas = await canvas?.();
138
129
  return {
139
130
  _createCanvas(width, height) {
140
131
  if (isBrowser) {
141
- const canvas = document.createElement("canvas");
142
- canvas.width = width;
143
- canvas.height = height;
144
- return canvas;
132
+ const canvas2 = document.createElement("canvas");
133
+ canvas2.width = width;
134
+ canvas2.height = height;
135
+ return canvas2;
145
136
  } else if (isNode) {
137
+ if (!_canvas) {
138
+ throw new Error('Failed to resolve "canvas" package.');
139
+ }
146
140
  return _canvas.createCanvas(width, height);
147
141
  }
148
142
  throw new Error("Unsupported environment for canvas creation.");