Skip to content
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

[Bug]: ImageData is not defined - nodejs #19180

Closed
soadzoor opened this issue Dec 6, 2024 · 2 comments
Closed

[Bug]: ImageData is not defined - nodejs #19180

soadzoor opened this issue Dec 6, 2024 · 2 comments

Comments

@soadzoor
Copy link

soadzoor commented Dec 6, 2024

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.155

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

Yes

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:

file:///home/soadzoor/works/node-pdfjs/node_modules/pdfjs-dist/build/pdf.mjs:6910
if (imgData instanceof ImageData) {
^

ReferenceError: ImageData is not defined
at putBinaryImageData (file:///home/soadzoor/works/node-pdfjs/node_modules/pdfjs-dist/build/pdf.mjs:6910:26)
at CanvasGraphics.paintInlineImageXObject (file:///home/soadzoor/works/node-pdfjs/node_modules/pdfjs-dist/build/pdf.mjs:8505:7)
at CanvasGraphics.paintImageXObject (file:///home/soadzoor/works/node-pdfjs/node_modules/pdfjs-dist/build/pdf.mjs:8431:10)
at CanvasGraphics.executeOperatorList (file:///home/soadzoor/works/node-pdfjs/node_modules/pdfjs-dist/build/pdf.mjs:7177:20)
at InternalRenderTask._next (file:///home/soadzoor/works/node-pdfjs/node_modules/pdfjs-dist/build/pdf.mjs:13056:37)

Link to a viewer

No response

Additional context

After the fix was released for #19145 , I was really excited to give it a shot again, but now it says a different runtime error (see above), when I try to run it in node.js environment.

I guess ImageData is not available in node.js, so we need an additional check before running that condition, to see whether we're in nodejs, or in browser.

I can confirm that if I replace this line:
if (imgData instanceof ImageData) {
with this line:
if (!isNodeJS && imgData instanceof ImageData) {

everything seems to be working fine

@soadzoor
Copy link
Author

soadzoor commented Dec 6, 2024

Created a PR with a fix: #19181

@Snuffleupagus
Copy link
Collaborator

Please refer to PR #19015, and in particular commit 9b62f2e.

Note that only the legacy-build is supported in Node.js, as clearly mentioned in the FAQ: https://github.com/mozilla/pdf.js/wiki/Frequently-Asked-Questions#faq-support
Unless the correct build is being used, none of the necessary polyfills are loaded and various things can/will break. Hence you need to change https://github.com/soadzoor/node-pdfjs/blob/8411d8aed2118cb01d36a078344903d2f754f90d/index.js#L2 into

import { getDocument } from "pdfjs-dist/legacy/build/pdf.mjs";

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants