我在电子中使用 html-pdf 从 html 生成 pdf。
当我通过“npm run start”测试时它是有效的。我可以得到pdf。
但是当我通过 electron-builder 将 electron app 打包成 .dmg 文件时,
调用 pdf.create() 时出现“spawn ENOTDIR”错误
var pdf = require('html-pdf');
var options = { format: 'Letter' };
//resultFilePath = /Users/myname/Documents/result.pdf
pdf.create(htmlContent, options).toFile(resultFilePath, function(err, res)
{
}
我现在不知道。有没有人有同样的问题?
任何帮助将不胜感激。
html-pdf 在打包后可能无法找到幻影二进制文件。未打包时,可以在
node_modules/phantomjs-prebuilt/bin/phantomjs
中找到二进制文件(至少在我的机器上)
尝试通过 html-pdf 选项显式设置 phantomJS 二进制位置。
> 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) { }
您可能还需要设置 options.script 以指向来自 html-pdf 模块的
pdf_a4_portrait.js
的副本。
其他人也有类似的问题。见https://discuss.atom.io/t/asar-limitations-on-node-api-spawn-a-child/28235/2
const pdf = require('html-pdf');
npm i witch phantomjs-prebuilt
然后,在选项 json 对象中,
const options = {
phantomPath: `${phantomPath}`
};
之后,使用像这样的选项对象来创建 PDF:
pdf.create(html, options).toFile(`${fileName}.pdf`, function (err, res) {
if (err) return console.log(err);
console.log(res);
});
像这样使用 phantomPath,希望它能起作用。它对我有用。
对于任何在电子打印时遇到问题的人。
Open print content in a visible windows is good solution,我跟着zen的回答 在如何在 ElectronJS 中打印 DIV
在 Windows 和 MacOS 上运行良好。