mozilla / pdf.js

PDF Reader in JavaScript
https://mozilla.github.io/pdf.js/
Apache License 2.0
48.8k stars 10.03k forks source link

[Bug]: Cannot load "@napi-rs/canvas" package: "Error: Cannot find module '@napi-rs/canvas' #19145

Open soadzoor opened 12 hours ago

soadzoor commented 12 hours ago

Attach (recommended) or Link to PDF file

https://github.com/mozilla/pdf.js/files/13179307/c53a656b-551d-4c24-bfb6-f994ef3a6d4b.pdf

But I believe it has nothing to do with the PDF file itself.

Web browser and its version

Node.js v22.11.0

Operating system and its version

Ubuntu 22.04.2 LTS (but I tried with newest MacOS as well, same issue...)

PDF.js version

4.9.124

Is the bug present in the latest PDF.js version?

No

Is a browser extension

No

Steps to reproduce the problem

  1. Clone this repo: https://github.com/soadzoor/node-pdfjs
  2. Run npm install
  3. Run node index.js

What is the expected behavior?

It should generate a PNG file

What went wrong?

It generates a runtime error when running node index.js: Unfortunately, it gives me a runtime error: node index.js Warning: Cannot load "@napi-rs/canvas" package: "Error: Cannot find module '@napi-rs/canvas' Require stack:

Error: Cannot find module '@napi-rs/canvas' Require stack:

Node.js v22.11.0

Link to a viewer

No response

Additional context

I was happy to learn that pdfjs replaced node-canvas with a new canvas implementation (@napi-rs/canvas) which has much less dependencies. This fact also gave me hope that the bug I reported a long time ago might be fixed now, so I gave it a shot.

First I created the canvas context like this:

import {createCanvas} from "@napi-rs/canvas";

var canvas = createCanvas(2048, 2048);
var context = canvas.getContext('2d');

Then I checked the official examples, and based on those, I replaced those with these lines below, and removed the @napi-rs/canvas dependency from my package.json, deleted node_modules and ran npm install again:

const canvasFactory = pdf.canvasFactory;
const canvasAndContext = canvasFactory.create(2048, 2048);

Unfortunately, I still have the same issue.

What am I doing wrong?

nicolo-ribaudo commented 11 hours ago

This is indeed a bug in how pdfjs-dist gets bundled before publishing it pdfjs-dist/legacy/build/pdf.mjs contains a hard-coded path from where it gets published from:

class NodeCanvasFactory extends BaseCanvasFactory {
  _createCanvas(width, height) {
    const require = process.getBuiltinModule("module").createRequire("file:///home/runner/work/pdf.js/pdf.js/src/display/node_utils.js");
    const canvas = require("@napi-rs/canvas");
    return canvas.createCanvas(width, height);
  }
}
soadzoor commented 11 hours ago

This is indeed a bug in how pdfjs-dist gets bundled before publishing it pdfjs-dist/legacy/build/pdf.mjs contains a hard-coded path from where it gets published from:

class NodeCanvasFactory extends BaseCanvasFactory {
  _createCanvas(width, height) {
    const require = process.getBuiltinModule("module").createRequire("file:///home/runner/work/pdf.js/pdf.js/src/display/node_utils.js");
    const canvas = require("@napi-rs/canvas");
    return canvas.createCanvas(width, height);
  }
}

How does anyone use pdfjs in nodejs with this bug?

nicolo-ribaudo commented 11 hours ago

One uses an older version 😅

It'd be great if you could open a PR to fix this regression. There are two usages of import.meta.url in https://github.com/mozilla/pdf.js/blob/d4489531668b172a5ffd4ac6a29c3eb856af79a6/src/display/node_utils.js#L38, and Webpack is replacing it while it should leave it as-is. I'm not good enough at Webpack to quickly tell how to fix it, but the webpack config is defined in https://github.com/mozilla/pdf.js/blob/master/gulpfile.mjs.

soadzoor commented 9 hours ago

One uses an older version 😅

It'd be great if you could open a PR to fix this regression. There are two usages of import.meta.url in

https://github.com/mozilla/pdf.js/blob/d4489531668b172a5ffd4ac6a29c3eb856af79a6/src/display/node_utils.js#L38

, and Webpack is replacing it while it should leave it as-is. I'm not good enough at Webpack to quickly tell how to fix it, but the webpack config is defined in https://github.com/mozilla/pdf.js/blob/master/gulpfile.mjs.

I've tried 4.7.76 and v4.8.69 , they don't work either, I have the following runtime error there: image

Anyway, I can see that @Snuffleupagus already submitted a PR for this, thanks!