7

How can I generate a pdf file with some html content and render the file and content to the browser using phantom.js in node.js?

1 Answer 1

9

Why not use the html-pdf npm module which uses phantomJS?

Code example

var fs = require('fs');
var pdf = require('html-pdf');
var html = fs.readFileSync('./test/businesscard.html', 'utf8');
var options = { format: 'Letter' };

pdf.create(html, options).toFile('./businesscard.pdf', function(err, res) {
  if (err) return console.log(err);
  console.log(res); // { filename: '/app/businesscard.pdf' } 
});
Sign up to request clarification or add additional context in comments.

2 Comments

can I pass url like this var html = fs.readFileSync('http://localhost:3000/product/generate-invoice', 'utf8'); to generate pdf report?
The characters are becoming cut at the end of each pdf. It's a dynamic pdf. Hot to solve it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.