微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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 举报,一经查实,本站将立刻删除。