unpdf 0.7.0 → 0.7.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.
package/README.md CHANGED
@@ -179,8 +179,11 @@ defineUnPDFConfig({
179
179
 
180
180
  const pdf = await readFile("./dummy.pdf");
181
181
  const buffer = new Uint8Array(pdf);
182
+ const pageNumber = 1;
182
183
 
183
- const result = await renderPageAsImage(buffer, 1);
184
+ const result = await renderPageAsImage(buffer, pageNumber, {
185
+ canvas: () => import("canvas"),
186
+ });
184
187
  await writeFile("dummy-page-1.png", Buffer.from(result));
185
188
  ```
186
189
 
@@ -191,8 +194,9 @@ declare function renderPageAsImage(
191
194
  data: BinaryData | PDFDocumentProxy,
192
195
  pageNumber: number,
193
196
  options?: {
197
+ canvas?: () => Promise<typeof "canvas">;
194
198
  /** @default 1 */
195
- scale?: number;
199
+ scale?: number
196
200
  width?: number;
197
201
  height?: number;
198
202
  },
@@ -210,9 +214,11 @@ function extractImages(
210
214
 
211
215
  ## FAQ
212
216
 
213
- ### What About Canvas?
217
+ ### Why Is `canvas` A Peer Dependency?
218
+
219
+ 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
220
 
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.
221
+ However, to render PDF pages as images, you need to install the `canvas` module in Node.js environments. That's why `canvas` is a peer dependency.
216
222
 
217
223
  ## License
218
224
 
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.");
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "unpdf",
3
3
  "type": "module",
4
- "version": "0.7.0",
4
+ "version": "0.7.1",
5
5
  "packageManager": "pnpm@8.7.0",
6
6
  "description": "Utilities to work with PDFs in Node.js, browser and workers",
7
7
  "author": "Johann Schopplich <pkg@johannschopplich.com>",