ol-th / pdf-img-convert.js

Simple node package to convert a PDF into images.
MIT License
180 stars 42 forks source link

Does not work with electron #1

Open kefniark opened 4 years ago

kefniark commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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.

Yarin4 commented 4 years ago

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 commented 4 years ago

@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 commented 4 years ago

@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 commented 3 years ago

Same issue for me Electron 11.1.1

ZhangZheng-GIS commented 3 years ago

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
Moebits commented 9 months ago

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
      })