我正在使用此代码生成PDF:

let fileUri = process.env.PWD + '/storage/orders-pdf/' + fileName;

// Commence Webshot
webshot(html_string, fileUri, options, function(error) {
  fs.readFile(fileUri, function (err, data) {
    if (err) {
        return console.log(err);
    }

    fs.unlinkSync(fileUri);
    fut.return(data);
  });
});

let pdfData = fut.wait();

但它会抛出以下错误:
{ [Error: ENOENT, open '/opt/holi/storage/orders-pdf/Attributes.pdf']
   errno: 34,
   code: 'ENOENT',
   path: '/opt/holi/storage/orders-pdf/Attributes.pdf' }

尝试使用npm包https://github.com/brenden/node-webshot
然后,代码在本地主机上运行良好,但在服务器上失败并引发以下错误:
编辑:
即使运行webshot时没有:
fs.readFile(fileUri, function (err, data) {
  if (err) {
    return console.log(err);
  }

  fs.unlinkSync(fileUri);
  fut.return(data);
});

文件未创建..
编辑-2:
webshot抛出错误:[Error: PhantomJS exited with return value 2]
编辑-3:
实际问题:https://github.com/brenden/node-webshot/issues/123

最佳答案

我有一个类似的问题,花了一天的大部分时间试图解决这个问题。我最后补充道:

"phantomPath": "/usr/bin/phantomjs"

到我的webshot选项对象。我使用的幻影路径是mup在服务器设置中安装幻影js的位置。

08-16 06:24