Muh*_*lik 8 amazon-ec2 node.js phantomjs node-html-pdf
我有 EC2 实例在 aws 中运行。当我部署到服务器后。一切正常,直到我检查 html-pdf。每次我点击 api 创建 pdf 时,我都会检查日志,结果是:
我已经尝试使用npm uninstall html-pdf卸载 html pdf,然后使用npm install html-pdf再次安装
我已经尝试使用npm link phantomjs-prebuilt但仍然有相同的结果。当我检查当地一切工作正常时,是否有人遇到这样的相同问题并且已经解决?谢谢!
*更新:
我已经尝试全局安装 phantomjs,并使用以下命令设置幻像路径:
"phantomPath":"../../../../../usr/local/lib/node_modules/phantomjs-prebuilt/bin/phantomjs"
Run Code Online (Sandbox Code Playgroud)
我从节点模块获得的路径:
这是我的代码:
createOwn: async function (req, res, next) {
try {
const owner = await getOwnership(req.params.id);
const name = await Date.now();
ejs.renderFile(path.join(__dirname, './template', "own.template.ejs"), { own: owner, moment: moment, }, (err, data) => {
if (err) {
console.log(err);
return res.status(500).json({ status: "error", data: err });
} else {
let options = {
"format": "A4",
"orientation": "portrait",
"phantomPath": "../../../../../usr/local/lib/node_modules/phantomjs-prebuilt/bin/phantomjs",
"border": {
"top": "1cm", // default is 0, units: mm, cm, in, px
"right": "2cm",
"bottom": "1cm",
"left": "1cm"
},
};
pdf.create(data, options).toFile(`temp/${name}.pdf`, function (err, data) {
if (err) {
console.log(err)
res.send("cannot create pdf");
} else {
var file = `temp/${name}.pdf`;
fs.readFile(file, function (err, data) {
res.contentType("application/pdf");
res.send(data);
});
}
});
}
});
} catch (e) {
console.log(e)
return res.status(500).json({ status: "error", data: "internal server error" });
}
},
Run Code Online (Sandbox Code Playgroud)
尝试设置phantomJS的路径
var option={
"phantomPath": "./node_modules/phantomjs/bin/phantomjs",
}
var fs = require('fs');
var pdf = require('html-pdf');
var html = fs.readFileSync('./test/businesscard.html', 'utf8');
pdf.create(html, options).toFile('./businesscard.pdf', function(err, res) {
if (err) return console.log(err);
console.log(res); // { filename: '/app/businesscard.pdf' }
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14016 次 |
| 最近记录: |