1

I am using html-pdf in electron to generate pdf from a html.

It is worked when I tested by "npm run start". I can get pdf.

But when I package electron app to .dmg file by electron-builder,

I got "spawn ENOTDIR" error when call pdf.create()

var pdf = require('html-pdf');
var options = { format: 'Letter' };
//resultFilePath = /Users/myname/Documents/result.pdf
pdf.create(htmlContent, options).toFile(resultFilePath, function(err, res) 
{
}

javascript debugger with error message

I have no idea now. Does anyone have the same problem?

Any help would be greatly appreciated.

Wu Baiquan
  • 129
  • 7

3 Answers3

1

html-pdf may be having trouble finding the phantom binary after it has been packaged. When not packaged, the binary can be found (at least on my machine) in node_modules/phantomjs-prebuilt/bin/phantomjs

Try setting the phantomJS binary location explicitly via an html-pdf option.

> var pdf = require('html-pdf'); 
> var options = { format: 'Letter', phantomPath: '/path/to/phantomJSbinary' };
> //resultFilePath = /Users/myname/Documents/result.pdf
> pdf.create(htmlContent, options).toFile(resultFilePath, function(err,
> res)  { }

You may also need to set options.script to point to a copy of pdf_a4_portrait.js from the html-pdf module.

Other people have had a similar problem. See https://discuss.atom.io/t/asar-limitations-on-node-api-spawn-a-child/28235/2

Tom
  • 939
  • 5
  • 9
0
const pdf = require('html-pdf');    

npm i witch phantomjs-prebuilt

and, then, in the options json object,

const options = {
    phantomPath: `${phantomPath}`
};

after that, use options object like this to create PDF:

pdf.create(html, options).toFile(`${fileName}.pdf`, function (err, res) {
                if (err) return console.log(err);
                console.log(res);
            });

use phantomPath like this, Hope this works. It worked for me.

Aditya Aggarwal
  • 610
  • 6
  • 7
-1

For anyone who meet problem when print in electron.

Open print content in a visible windows is good solution, I followed zen's answer in How to print a DIV in ElectronJS

Works well on Windows and MacOS.

Wu Baiquan
  • 129
  • 7