微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

html-pdf:无法加载 PhantomJS 模块使用 aws ec2

如何解决html-pdf:无法加载 PhantomJS 模块使用 aws ec2

我有 EC2 实例在 aws 中运行。在我部署到服务器之后。一切正常,直到我检查 html-pdf。每次我点击api创建pdf时,我都会检查日志,结果是:

enter image description here

我已经尝试使用 npm uninstall html-pdf 卸载 html pdf,然后使用 npm install html-pdf

再次安装

并且我已经尝试使用 npm link phantomjs-prebuilt 但仍然有相同的结果。当我检查本地一切正常时,有没有人有同样的问题并且已经解决了?谢谢!

*更新:

我已经尝试安装 phantomjs global,并使用:

"phantomPath":"../../../../../usr/local/lib/node_modules/phantomjs-prebuilt/bin/phantomjs"

我从节点模块得到的路径:

enter image description here

这是我的代码

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,data) {
                              res.contentType("application/pdf");
                              res.send(data);
                          });
                      }
                  });
              }
          });
      } catch (e) {
          console.log(e)
          return res.status(500).json({ status: "error",data: "internal server error" });
      }
  },

解决方法

尝试设置phantomJS的路径

old_list= ['list4','this1','my3','is2']
order = [1,3,2,0]
new_list = [old_list[idx] for idx in order]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。