Open soadzoor opened 12 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);
}
}
This is indeed a bug in how
pdfjs-dist
gets bundled before publishing itpdfjs-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?
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.
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, 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:
Anyway, I can see that @Snuffleupagus already submitted a PR for this, thanks!
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
npm install
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:DOMMatrix
, rendering may be broken. Warning: Cannot polyfillImageData
, rendering may be broken. Warning: Cannot polyfillPath2D
, rendering may be broken. PDF loaded Page loaded node:internal/modules/cjs/loader:1249 const err = new Error(message); ^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:
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 mypackage.json
, deletednode_modules
and rannpm install
again:Unfortunately, I still have the same issue.
What am I doing wrong?