Skip to content

PDF.js + Webpack has drawImage check failure #994

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Cybourgeoisie opened this issue Feb 18, 2025 · 3 comments
Open

PDF.js + Webpack has drawImage check failure #994

Cybourgeoisie opened this issue Feb 18, 2025 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@Cybourgeoisie
Copy link

Cybourgeoisie commented Feb 18, 2025

The following error occurs only in Electron builds (update: it's occurring in the webpack code, not just electron) with pdfjs-dist (current version, v4.10.38) and @napi-rs/canvas (current version, v0.1.67). This issue does not happen when running the code in development, but does happen after consolidating the code into webpack, and running via electron development or a build.

Error message: Error: Value is non of these types CanvasElement, SVGCanvas, Image,

In my specific case, I've traced the exact call error down to a pdfjs function that calls drawImage on a canvas.

Error: Value is non of these types `CanvasElement`, `SVGCanvas`, `Image`, 
    at CanvasGraphics.endGroup (.../build/626.server.js:13928:16)
    at CanvasGraphics.executeOperatorList (.../build/626.server.js:12800:20)
    at InternalRenderTask._next (.../build/626.server.js:18340:37)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  code: 'InvalidArg'

The inputs, contexts, and canvases are all existent both in development and in the running Electron build, so there isn't a missing parameter, and as far as I can see, it isn't malformed.

The error seems to be triggered early in the ctx.rs::draw_image function, possibly when trying to match the type of image on line 1539 - as I was able to find that the Either3 error code is what produces the text for the above error, per https://prisma.github.io/prisma-engines/doc/src/napi/bindgen_runtime/js_values/either.rs.html#91

I will keep investigating, but it feels like moving forward is to identify why a valid CanvasElement input from Node.JS would not be identified as a valid type when the rust code is called.

@Cybourgeoisie Cybourgeoisie changed the title PDF.js + Electron build has drawImage check failure PDF.js + Webpack has drawImage check failure Feb 19, 2025
@Cybourgeoisie
Copy link
Author

I'm able to reproduce the issue with a very simple repo, which only has the objective of loading a PDF and printing the first page: https://github.com/Cybourgeoisie/webpack-pdfjs-napirscanvas-test

@Brooooooklyn Brooooooklyn self-assigned this Feb 22, 2025
@Brooooooklyn Brooooooklyn added the bug Something isn't working label Mar 4, 2025
@nora-heydecker-axa
Copy link

nora-heydecker-axa commented Mar 13, 2025

We have observed the same issue. It looks like this is a regression from 0.1.65 -> 0.1.68. We saw the problem with a dependency upgrade that upgraded pdfjs-dist's dependency on @napi-rs/canvas from 0.1.65 to 0.1.68, and reverting this upgrade fixed it (note that there were some other upgrades as well, I'm not 100% sure that it really was that upgrade).

The error occurs on this line of pdfjs (pdfjs-dist@4.9.155/legacy/build/pdf.mjs:13381:

ctx.drawImage(maskCanvas, mask.offsetX, mask.offsetY);

(https://github.com/mozilla/pdf.js/blob/d5c185b90a104ca3e2eb4eec240c9414996a3a34/src/display/canvas.js#L2834)

This makes me suspect that it's related to #984.

@nora-heydecker-axa
Copy link

The error is coming from decoding this parameter:

canvas/src/ctx.rs

Line 1521 in 5944ea3

image: Either3<&mut CanvasElement, &mut SVGCanvas, &mut Image>,

The canvas passed in by pdfjs is coming from _createMaskCanvas:
https://github.com/mozilla/pdf.js/blob/d5c185b90a104ca3e2eb4eec240c9414996a3a34/src/display/canvas.js#L1276

which comes from the cached canvases:
https://github.com/mozilla/pdf.js/blob/d5c185b90a104ca3e2eb4eec240c9414996a3a34/src/display/canvas.js#L1206

and the canvas is being created from a canvas factory (without a flag, so a CanvasElement should be created):
https://github.com/mozilla/pdf.js/blob/d5c185b90a104ca3e2eb4eec240c9414996a3a34/src/display/node_utils.js#L90

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants