Skip to content

Commit

Permalink
fix: lazy import for Node canvas dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
johannschopplich committed Sep 6, 2023
1 parent e4dbc32 commit 11f6543
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,11 @@ defineUnPDFConfig({

const pdf = await readFile("./dummy.pdf");
const buffer = new Uint8Array(pdf);
const pageNumber = 1;

const result = await renderPageAsImage(buffer, 1);
const result = await renderPageAsImage(buffer, pageNumber, {
canvas: () => import("canvas"),
});
await writeFile("dummy-page-1.png", Buffer.from(result));
```

Expand All @@ -191,8 +194,9 @@ declare function renderPageAsImage(
data: BinaryData | PDFDocumentProxy,
pageNumber: number,
options?: {
canvas?: () => Promise<typeof "canvas">;
/** @default 1 */
scale?: number;
scale?: number
width?: number;
height?: number;
},
Expand Down
27 changes: 11 additions & 16 deletions src/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export async function renderPageAsImage(
data: BinaryData | PDFDocumentProxy,
pageNumber: number,
options: {
canvas?: () => Promise<typeof import("canvas")>;
/** @default 1 */
scale?: number;
width?: number;
Expand Down Expand Up @@ -78,7 +79,7 @@ export async function renderPageAsImage(
viewport = page.getViewport({ scale: outputScale });
}

const canvasFactory = await createIsomorphicCanvasFactory();
const canvasFactory = await createIsomorphicCanvasFactory(options.canvas);
const ctx = canvasFactory.create(viewport.width, viewport.height);

await page.render({
Expand All @@ -94,19 +95,10 @@ export async function renderPageAsImage(
return await response.arrayBuffer();
}

async function createIsomorphicCanvasFactory() {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
let _canvas: typeof import("canvas") | undefined;

if (isNode) {
try {
_canvas = await import("canvas");
} catch {
throw new Error(
'The "canvas" package is required when running in Node.js.',
);
}
}
async function createIsomorphicCanvasFactory(
canvas?: () => Promise<typeof import("canvas")>,
) {
const _canvas = await canvas?.();

return {
_createCanvas(width: number, height: number) {
Expand All @@ -116,8 +108,11 @@ async function createIsomorphicCanvasFactory() {
canvas.height = height;
return canvas;
} else if (isNode) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return _canvas!.createCanvas(width, height);
if (!_canvas) {
throw new Error('Failed to resolve "canvas" package.');
}

return _canvas.createCanvas(width, height);
}

throw new Error("Unsupported environment for canvas creation.");
Expand Down
1 change: 1 addition & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ describe("unpdf", () => {
const result = (await renderPageAsImage(
await getPDF("image-sample.pdf"),
1,
{ canvas: () => import("canvas") },
))!;
// await writeFile(
// new URL("image-sample.png", import.meta.url),
Expand Down

0 comments on commit 11f6543

Please sign in to comment.