html-pdf:无法加载PhantomJS模块。错误:找不到模块'phantomjs-prebuilt'



我正在使用html-pdf-NPM模块将我的html(bill.html(代码转换为pdf文件(bill.pdf(,我有一个带有标题标签的简单html,并通过下面的fs模块导出该html

import { create } from 'html-pdf';
import fs from 'fs';
import path from 'path';
var html = fs.readFileSync(path.resolve(__dirname, "./bill.html"), 'utf8');
var options = { format: 'Letter' };

create(html, options).toFile('./bill.pdf', function(err, res) {
if (err) return console.log(err);
console.log(res);
});

我得到了"phantomjs预构建"的错误

html-pdf: Failed to load PhantomJS module. Error: Cannot find module 'phantomjs-prebuilt'
Require stack:
- /home/hardy/Documents/personal/api/node_modules/html-pdf/lib/pdf.js
- /home/hardy/Documents/personal/api/node_modules/html-pdf/lib/index.js
- /home/hardy/Documents/personal/api/src/app.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:793:17)
at Function.Module._load (internal/modules/cjs/loader.js:686:27)
at Module.require (internal/modules/cjs/loader.js:848:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (/home/hardy/Documents/personal/api/node_modules/html-pdf/lib/pdf.js:7:19)
at Module._compile (internal/modules/cjs/loader.js:955:30)
at Module._extensions..js (internal/modules/cjs/loader.js:991:10)
at Object.require.extensions.<computed> [as .js] (/home/hardy/Documents/personal/api/node_modules/babel-register/lib/node.js:152:7)
at Module.load (internal/modules/cjs/loader.js:811:32)
at Function.Module._load (internal/modules/cjs/loader.js:723:14) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/home/hardy/Documents/personal/api/node_modules/html-pdf/lib/pdf.js',
'/home/hardy/Documents/personal/api/node_modules/html-pdf/lib/index.js',
'/home/hardy/Documents/personal/api/src/app.js'
]
}
Debugger listening on ws://127.0.0.1:5858/508c0ea9-c495-4254-9b05-cd2fd3cd8ae3
For help, see: https://nodejs.org/en/docs/inspector
assert.js:374
throw err;
^
AssertionError [ERR_ASSERTION] [ERR_ASSERTION]: html-pdf: Failed to load PhantomJS module. You have to set the path to the PhantomJS binary using 'options.phantomPath'
at new PDF (/home/hardy/Documents/personal/api/node_modules/html-pdf/lib/pdf.js:38:3)
at createPdf (/home/hardy/Documents/personal/api/node_modules/html-pdf/lib/index.js:10:14)
at Object.<anonymous> (/home/hardy/Documents/personal/api/src/app.js:8:1)
at Module._compile (internal/modules/cjs/loader.js:955:30)
at loader (/home/hardy/Documents/personal/api/node_modules/babel-register/lib/node.js:144:5)
at Object.require.extensions.<computed> [as .js] (/home/hardy/Documents/personal/api/node_modules/babel-register/lib/node.js:154:7)
at Module.load (internal/modules/cjs/loader.js:811:32)
at Function.Module._load (internal/modules/cjs/loader.js:723:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)
at Object.<anonymous> (/home/hardy/Documents/personal/api/node_modules/babel-cli/lib/_babel-node.js:154:22) {
generatedMessage: false,
code: 'ERR_ASSERTION',
actual: undefined,
expected: true,
operator: '=='
}
Waiting for the debugger to disconnect...

由于phantomjs重建是一个不推荐使用的

您仍然可以强制安装它并解决错误。

命令-npm i phantomjs-prebuilt --force

最新更新