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

Does not work with electron #1

Open
kefniark opened this issue Jul 10, 2020 · 9 comments
Open

Does not work with electron #1

kefniark opened this issue Jul 10, 2020 · 9 comments
Assignees

Comments

@kefniark
Copy link

kefniark commented Jul 10, 2020

Description

I like the idea of this lib, and I was surprised to see someone else working on the same thing few hours ago 😄

Apparently the dependence to canvas caused issues with other flavor of JS like electron
Seems like only node binding are managed

Error: The module '\\reader\node_modules\canvas\build\Release\canvas.node'
was compiled against a different Node.js version using
NODE_MODULE_VERSION 72. This version of Node.js requires
NODE_MODULE_VERSION 76. Please try re-compiling or re-installing
the module (for instance, using `npm rebuild` or `npm install`).
    at process.func [as dlopen] (electron/js2c/asar.js:140:31)
    at Object.Module._extensions..node (internal/modules/cjs/loader.js:1016:18)
    at Object.func [as .node] (electron/js2c/asar.js:140:31)
    at Module.load (internal/modules/cjs/loader.js:816:32)
    at Module._load (internal/modules/cjs/loader.js:728:14)
    at Function.Module._load (electron/js2c/asar.js:748:26)
    at Module.require (internal/modules/cjs/loader.js:853:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (\reader\node_modules\canvas\lib\bindings.js:3:18)
    at Module._compile (internal/modules/cjs/loader.js:968:30)
@kefniark
Copy link
Author

I think I will just take some inspiration from your code and switch the canvas by a real canvas.
I just need to extract few images like the cover anyway 😃

Btw, I would suggest to change that Sync method by a promise you can await

pdfData = new Uint8Array(fs.readFileSync(pdfURL));

by

import util from 'util'
const readFile= util.promisify(fs.readFile);

...

pdfData = new Uint8Array(await readFile(pdfURL));

@ol-th
Copy link
Owner

ol-th commented Jul 10, 2020

Thanks for pointing this out. I'd only tested it on pure node and chose node-canvas accordingly. I'll try to find a workaround for this but in the meantime feel free to fork the repo and make the necessary changes for your use case. (Please do submit a PR if you find a solution that works for node too 🙂)

I think I will just take some inspiration from your code and switch the canvas by a real canvas.
I just need to extract few images like the cover anyway 😃

Btw, I would suggest to change that Sync method by a promise you can await

pdfData = new Uint8Array(fs.readFileSync(pdfURL));

by

import util from 'util'
const readFile= util.promisify(fs.readFile);

...

pdfData = new Uint8Array(await readFile(pdfURL));

I'll make this change you suggested here - thanks for pointing this out too.

@ol-th ol-th self-assigned this Jul 11, 2020
@Yarin4
Copy link

Yarin4 commented Jul 11, 2020

Hi guys,
Same issue for me, I tried to rebuild it after the installation but it still won't work
But great reactivity @olliet88 thanks!

@ol-th
Copy link
Owner

ol-th commented Jul 13, 2020

@Yarin4 just to double check - did you rebuild it against electron using electron-rebuild or against node using npm rebuild?

If you rebuilt against node could you try rebuilding against electron and reporting back? It'd be really useful thanks.

@Yarin4
Copy link

Yarin4 commented Jul 15, 2020

@Yarin4 just to double check - did you rebuild it against electron using electron-rebuild or against node using npm rebuild?

If you rebuilt against node could you try rebuilding against electron and reporting back? It'd be really useful thanks.

I used electron-rebuild but I actually think I am missing a package I'll have to check again

@ivysrono
Copy link

Same issue for me
Electron 11.1.1

@ZhangZheng-GIS
Copy link

when I used electron-rebuild, it fails and reports an error.

⠴ Building module: canvas, Completed: 0C:\Workspace\ELECTRON\getPDFtext\get-pdf-text\node_modules\canvas\src\backend\Backend.h(3,10): fatal error C1083: 无法打开包括文件: “cairo.h”: No suc
h file or directory [C:\Workspace\ELECTRON\getPDFtext\get-pdf-te
xt\node_modules\canvas\build\canvas.vcxproj]
⠏ Building module
✖ Rebuild Failed

An unhandled error occurred inside electron-rebuild
node-gyp failed to rebuild 'C:\Workspace\ELECTRON\getPDFtext\get-pdf-text\node_modules\canvas'.
Error: `C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\MSBuild.exe` failed with exit code: 1

ol-th pushed a commit that referenced this issue Mar 5, 2023
@Moebits
Copy link

Moebits commented Feb 16, 2024

You should update to master branch of node-canvas (v3.0.0) v2 has a lot of build issues with electron rebuild. The update also requires updating pdfjs-dist's canvas dependency to master branch as well, so I used a fork.

https://github.com/Automattic/node-canvas
https://github.com/Tenpi/pdfjs-dist

Run this to fix: electron-rebuild -f -w canvas

Specifically on windows, the canvas node depends on multiple dll files so I just added this with webpack copy plugin.

let copyPatterns = []

if (process.platform === "win32") {
  copyPatterns.push(...[
    {from: "node_modules/canvas/build/Release/jpeg62.dll", to: "[name][ext]"},
    {from: "node_modules/canvas/build/Release/libcairo-2.dll", to: "[name][ext]"},
    {from: "node_modules/canvas/build/Release/libexpat-1.dll", to: "[name][ext]"},
    {from: "node_modules/canvas/build/Release/libfontconfig-1.dll", to: "[name][ext]"},
    {from: "node_modules/canvas/build/Release/libfreetype-6.dll", to: "[name][ext]"},
    {from: "node_modules/canvas/build/Release/libglib-2.0-0.dll", to: "[name][ext]"},
    {from: "node_modules/canvas/build/Release/libgmodule-2.0-0.dll", to: "[name][ext]"},
    {from: "node_modules/canvas/build/Release/libgobject-2.0-0.dll", to: "[name][ext]"},
    {from: "node_modules/canvas/build/Release/libgthread-2.0-0.dll", to: "[name][ext]"},
    {from: "node_modules/canvas/build/Release/libintl-8.dll", to: "[name][ext]"},
    {from: "node_modules/canvas/build/Release/libpango-1.0-0.dll", to: "[name][ext]"},
    {from: "node_modules/canvas/build/Release/libpangocairo-1.0-0.dll", to: "[name][ext]"},
    {from: "node_modules/canvas/build/Release/libpangoft2-1.0-0.dll", to: "[name][ext]"},
    {from: "node_modules/canvas/build/Release/libpangowin32-1.0-0.dll", to: "[name][ext]"},
    {from: "node_modules/canvas/build/Release/libpng14-14.dll", to: "[name][ext]"},
    {from: "node_modules/canvas/build/Release/zlib1.dll", to: "[name][ext]"}
  ])
}
// add plugin
new CopyPlugin({
        patterns: copyPatterns
      })

@LionSR
Copy link

LionSR commented Jan 10, 2025

for 2.0.0

.../dist/fa782d78b72b06e169fa60a7d72fbab8.node'
was compiled against a different Node.js version using
NODE_MODULE_VERSION 120. This version of Node.js requires
NODE_MODULE_VERSION 123. Please try re-compiling or re-installing
the module (for instance, using npm rebuild or npm install)..

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

No branches or pull requests

7 participants