首页 > 解决方案 > html-pdf: Failed to load PhantomJS module. Error: Cannot find module 'phantomjs-prebuilt'

问题描述

I am using html-pdf NPM module to convert my html (bill.html) code to pdf file (bill.pdf), I have simple html with heading tag and exporting that html by fs module below

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);
});

And I am getting this error of '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...

标签: javascriptnode.jspdfphantomjshtml-pdf

解决方案


推荐阅读