0

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

1 Answers1

0

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' }
});
Aditya Joshi
  • 529
  • 4
  • 13
  • hi, thanks for your answer. i already try with phantomPath. but i check in node_modules. there is no folder with phantomjs name. but there is phantomjs-prebuilt. so i edit with: let options = { "phantomPath": "../../node_modules/phantomjs-prebuilt/bin/phantomjs" } but still get the same error. i try to add phantomPath to local developement, but error if i add phantomPath. – Muhammad Yusuf Malik Jan 19 '21 at 07:14
  • try to install phantomJS using these steps https://gist.github.com/julionc/7476620 – Aditya Joshi Jan 19 '21 at 07:50
  • 1
    hello, i already try and got this error: Error: html-pdf: Received the exit code '1' internal/validators.js:125 throw new ERR_INVALID_ARG_TYPE(name, 'string', value); ^ TypeError [ERR_INVALID_ARG_TYPE]: The "file" argument must be of type string. Received type object – Muhammad Yusuf Malik Jan 22 '21 at 12:44
  • this is my phantomPath: "../../../../../usr/local/lib/node_modules/phantomjs-prebuilt/bin/phantomjs" – Muhammad Yusuf Malik Jan 22 '21 at 12:46