i have EC2 instance running in aws. after i deploy to the server. all workking fine until i check html-pdf. everytime i hit the api to create pdf, i check the log and the result is:

enter image description here

i already try to unninstall html pdf using npm uninstall html-pdf then install again with npm install html-pdf

and i already try with npm link phantomjs-prebuilt but still have the same result. when i check with local everything working fine, does anyone have same issue like this and already solved? thanks!

*update:

i already try with install phantomjs global, and set the phantom path using:

"phantomPath":"../../../../../usr/local/lib/node_modules/phantomjs-prebuilt/bin/phantomjs"

the path i got from the node modules:

enter image description here

this is my code:

createOwn: async function (req, res, next) {
      try {
          const owner = await getOwnership(req.params.id);
          const name = await Date.now();
          ejs.renderFile(path.join(__dirname, './template', "own.template.ejs"), { own: owner, moment: moment, }, (err, data) => {
              if (err) {
                  console.log(err);
                  return res.status(500).json({ status: "error", data: err });
              } else {
                  let options = {
                      "format": "A4",
                      "orientation": "portrait",
                      "phantomPath": "../../../../../usr/local/lib/node_modules/phantomjs-prebuilt/bin/phantomjs",
                      "border": {
                          "top": "1cm",            // default is 0, units: mm, cm, in, px
                          "right": "2cm",
                          "bottom": "1cm",
                          "left": "1cm"
                      },
                  };
                  pdf.create(data, options).toFile(`temp/${name}.pdf`, function (err, data) {
                      if (err) {
                          console.log(err)
                          res.send("cannot create pdf");
                      } else {
                          var file = `temp/${name}.pdf`;
                          fs.readFile(file, function (err, data) {
                              res.contentType("application/pdf");
                              res.send(data);
                          });
                      }
                  });
              }
          });
      } catch (e) {
          console.log(e)
          return res.status(500).json({ status: "error", data: "internal server error" });
      }
  },
3

There are 3 best solutions below

0
On

To resolve this issue please go to the

node_modules folder through terminal and use the command sudo node ./install.js or sudo node ./install.js -f

this will definitely install this module.

6
On

try with setting the path of phantomJS

var option={
"phantomPath": "./node_modules/phantomjs/bin/phantomjs", 
}
var fs = require('fs');
var pdf = require('html-pdf');
var html = fs.readFileSync('./test/businesscard.html', 'utf8');


pdf.create(html, options).toFile('./businesscard.pdf', function(err, res) {
  if (err) return console.log(err);
  console.log(res); // { filename: '/app/businesscard.pdf' }
});
0
On

I must suggest you use a different one instead of "phantomjs" as phantamjs development is suspended - https://phantomjs.org/

As html-pdf uses Phantamjs as an engine and phantamjs is suspended so it may not be on your device.

Instead of html-pdf, I recommend

use 'jspdf' library

OR

use 'puppeteer' library

Here I am demonstrating By using 'puppeteer'

npm install puppeteer

Then

const puppeteer = require('puppeteer');

async function generatePDFfromHTML(htmlContent, outputPath) {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.setContent(htmlContent);
  await page.pdf({ path: outputPath, format: 'A4' });
  await browser.close();
}

// Usage
const htmlContent = '<h1>Hello World</h1><p>This is custom HTML content.</p>';
generatePDFfromHTML(htmlContent, 'custom.pdf')
  .then(() => console.log('PDF generated successfully'))
  .catch(err => console.error('Error generating PDF:', err));

Diffrence between Puppeteer, html-pdf, jsPDF Image source apitemplate.io