Replies: 3 comments 2 replies
-
after further investigation I noticed that the dynamic import of if (isNodeJS) {
var packageCapability = Promise.withResolvers();
var packageMap = null;
const loadPackages = async () => {
const fs = await import(/*webpackIgnore: true*/"fs"),
http = await import(/*webpackIgnore: true*/"http"),
https = await import(/*webpackIgnore: true*/"https"),
url = await import(/*webpackIgnore: true*/"url");
let canvas, path2d;
try {
canvas = await import(/*webpackIgnore: true*/"canvas");
} catch {}
try {
path2d = await import(/*webpackIgnore: true*/"path2d");
} catch {}
return new Map(Object.entries({
fs,
http,
https,
url,
canvas,
path2d
}));
};
... the |
Beta Was this translation helpful? Give feedback.
-
Not sure why this got moved to discussion instead of an issue, but in case other find this: experiencing the same bug using version
Digging into this a bit I see that in this version the import code is actually different: if (isNodeJS) {
var packageCapability = Promise.withResolvers();
var packageMap = null;
const loadPackages = async () => {
const fs = await import(/*webpackIgnore: true*/"fs"),
http = await import(/*webpackIgnore: true*/"http"),
https = await import(/*webpackIgnore: true*/"https"),
url = await import(/*webpackIgnore: true*/"url");
let canvas, path2d;
return new Map(Object.entries({
fs,
http,
https,
url,
canvas,
path2d
}));
};
loadPackages().then(map => {
packageMap = map;
packageCapability.resolve();
}, reason => {
warn(`loadPackages: ${reason}`);
packageMap = new Map();
packageCapability.resolve();
});
}
class NodePackages {
static get promise() {
return packageCapability.promise;
}
static get(name) {
return packageMap?.get(name);
}
}
// ...
class NodeFilterFactory extends BaseFilterFactory {}
class NodeCanvasFactory extends BaseCanvasFactory {
_createCanvas(width, height) {
const canvas = NodePackages.get("canvas");
return canvas.createCanvas(width, height);
}
} Which seems to take away the try-catch anyway! Adding back in however fixes the issue, but then I tried downgrading my version to yours and the try-catches are missing. I suspect this will be fixed soon because they just merged in a PR to change the canvas dependency in Node but I'm not sure. |
Beta Was this translation helpful? Give feedback.
-
Having the exact same issue on a plane express/node server. |
Beta Was this translation helpful? Give feedback.
-
Attach (recommended) or Link to PDF file
sample.pdf
Web browser and its version
nodejs
Operating system and its version
windows 11
PDF.js version
4.7.76
Is the bug present in the latest PDF.js version?
Yes
Is a browser extension
No
Steps to reproduce the problem
when trying to render a pdf page on canvas, i get the following error:
What is the expected behavior?
it should correctly render the page and save it to a buffer
What went wrong?
file is correctly read, number of pages and viewport are correctly reported, but the render call fails:
Link to a viewer
No response
Additional context
this is the conversion function I wrote:
this is the package.json:
Beta Was this translation helpful? Give feedback.
All reactions