首页 > 解决方案 > 我在 node.js 中使用 npm wkhtmltopdf,出现错误

问题描述

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

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

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

错误:错误:在 ChildProcess 处生成 wkhtmltopdf ENOENT
。(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)
npm呃!代码 ELIFECYCLE
npm 错误!errno 1
npm 错误!restapichecklist@0.0.0 开始:node ./bin/www
npm 错误!退出状态 1
npm ERR!
npm 错误!在 restapichecklist@0.0.0 启动脚本中失败。npm 错误!这可能不是 npm 的问题。上面可能有额外的日志输出。

npm 错误!可以在以下位置找到此运行的完整日志:
npm ERR!C:\Users\khj\AppData\Roaming\npm-cache_logs\2019-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' });
});

标签: node.jswkhtmltopdfhtml-to-pdf

解决方案


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

最简单的方法是为您的系统下载预构建版本。不要尝试使用您的发行版提供的软件包,因为它们可能没有使用修补的 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中添加PATH:windows

在linux中添加PATH:linux


推荐阅读