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-prebuilt”的错误

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...

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...