bauhausjs / phantom-html2pdf

Node module to generate PDFs from HTML via PhantomJS
MIT License
112 stars 47 forks source link

Packing with Electron #44

Open rcrodrigues opened 7 years ago

rcrodrigues commented 7 years ago

Hi guys, I'm trying to pack this lib in an Electron app for windows.

It works perfectly fine for Linux, but when I build de app on a windows env, it gives an error saying "Can't open ...\node_modules\phantom-html2pdf\lib\phantom-script.js".

When debugging the convert method, I saw that it's using the "__dirname" to build the path to that script. Problem is when on a ASAR packed application, it won't be accessible.

Any ideas for a workaround?

rcrodrigues commented 7 years ago

Seems like the problem is that child_process.spawn() is requesting the file I mentioned above, and as this method dosn't support ASAR files, then an error is thrown. This was reported by @sindresorhus in this issue and this one

rcrodrigues commented 7 years ago

Just letting you guys know. I managed to make this work in a packed Electron app! \o/

The trick is, because child_process.spawn() needs to run "phantom-html2pdf\lib\phantom-script.js", we have to config the build on the package.json file to unpack phantom-html2pdf once the app is installed on the target.(Check this if you use electron-builder:options )

This way, we'll have no problems refferencing phantom-script in a ASAR pack. Finally, all I had to do before packing my app, was change the line 83 of the file \lib\phantom-script.js from path.join(__dirname, "phantom-script.js"), to path.join( __dirname.replace("app.asar", "app.asar.unpacked"), "phantom-script.js"),.

Let me know if a PR is welcome!

dustin-H commented 7 years ago

Hi @rcrodrigues,

thanks for reporting this.

Sure, a PR is very welcome if it don't breaks support for other systems.

If you create one I will merge and publish as soon as I can. Please refer to this issue in your PR.

Thanks, and best wishes Dustin

rcrodrigues commented 7 years ago

@dustin-H

Although I believe changing that line wouldn't break support for any other system ( once ASAR is used on Electron apps only ), I couldn't run the test to make sure. Anyways, I'll make a PR changing that line of code and documenting it so other developers can know this can work in Electron. I'll probably do it this weekend.

Thanks, Renan