k-yle / pdf-to-img

📃📸 Converts PDFs to images in nodejs
https://npm.im/pdf-to-img
MIT License
84 stars 22 forks source link

webworker problem react / vite #245

Open gandarufu opened 2 weeks ago

gandarufu commented 2 weeks ago

I cannot get pdf-to-img working, I get the following error The file does not exist at "/node_modules/.vite/deps/build/pdf.worker.mjs?worker_file&type=module" which is in the optimize deps directory. The dependency might be incompatible with the dep optimizer. Try adding it to optimizeDeps.exclude.

In another project I solved it by importing import * as pdfjsLib from "pdfjs-dist/webpack";

Would that be an option?

k-yle commented 1 week ago

This is a NodeJS package, it's not meant to work in the browser.

In a browser, we can access the native <canvas /> element, so most of the logic in this library is not required

spencermize commented 1 week ago

I'm having a similar issue - I'm not using it on the frontend, but instead in a Next.js / Webpack backend. I still get the same error:

Failed to parse document: Atel letter.pdf Error: Setting up fake worker failed: "Cannot find module '/sites/web/.next/server/vendor-chunks/pdf.worker.mjs' imported from /sites/web/.next/server/vendor-chunks/pdfjs-dist.js".
    at eval (webpack-internal:///(rsc)/../node_modules/pdfjs-dist/legacy/build/pdf.mjs:8581:36)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Any advice would be appreciated, thanks!