我在 node.js 中使用 npm wkhtmltopdf,但出现错误

I use npm wkhtmltopdf in node.js, I get an error

https://www.npmjs.com/package/wkhtmltopdf

通过设置路径,cmd中的pdf转换功能也可以正常使用
为什么我会在 node.js 中收到以下错误?

使用其他选项也是同样的错误。

错误:错误:生成 wkhtmltopdf ENOENT
在 ChildProcess。 (C:\Users\khj\React_androidProjects\restAPIchecklist\node_modules\wkhtmltopdf\index.js:161:11)
在 Object.onceWrapper (events.js:273:13)
在 ChildProcess.emit (events.js:182:13)
在 Process.ChildProcess._handle.onexit (internal/child_process.js:252:12)
在 onErrorNT (internal/child_process.js:421:16)
在 process.internalTickCallback (internal/process/next_tick.js:72:19)
错误!代码生命周期
错误!错误号 1
错误! restapichecklist@0.0.0 开始:node ./bin/www
错误!退出状态 1
npm 错误!
错误!在 restapichecklist@0.0.0 启动脚本时失败。 错误!这可能不是 npm 的问题。上面可能有额外的日志输出。

npm 错误!此 运行 的完整日志可在以下位置找到:
错误! C:\Users\khj\AppData\Roaming\npm-cache_logs19-09-05T05_27_53_709Z-debug.log

->>>

debug.log 内容

0 条信息如果以 ok 结尾就有效
1 个冗长的 cli ['E:\nodejs\node.exe',
1 个冗长的 cli 'C:\Users\khj\AppData\Roaming\npm\node_modules\npm\bin\npm-cli.js',
1 个冗长的 cli 'start' ]

router.get('/pdfbox', (req, res) => {
  wkhtmltopdf('https://www.google.com/', { output: './data/exlist.pdf' });
});

首先,您需要在系统上安装 wkhtmltopdf 命令行工具。

最简单的方法是 download 为您的系统预构建版本。不要尝试使用您的发行版提供的包,因为它们可能没有使用打过补丁的 Qt 并且缺少功能。

最后,要安装节点模块,使用 npm:

npm install wkhtmltopdf --save

确保安装完成后 wkhtmltopdf 命令行工具在您的 PATH 中。如果出于某种原因不想这样做,可以将 require('wkhtmltopdf').command 属性 更改为 wkhtmltopdf 命令行工具的路径。

然后确保 bin 文件夹在您的 PATH 中。 C:\Program Files\wkhtmltopdf\bin 是默认的安装路径。验证它是否适合您的机器。

在index.js中添加这条命令:

var wkhtmltopdf = require('wkhtmltopdf');
router.get('/pdfbox', (req, res) => {
  wkhtmltopdf('https://www.google.com/', { output: './data/exlist.pdf' });
});

在 windows 中添加路径:windows

在 linux 中添加路径:linux