Skip to content

Instantly share code, notes, and snippets.

@bekatom
Created April 5, 2018 12:26
Show Gist options
  • Save bekatom/986621848f333b6b19a3a26889a72687 to your computer and use it in GitHub Desktop.
Save bekatom/986621848f333b6b19a3a26889a72687 to your computer and use it in GitHub Desktop.
PDF_GENERATION_WITH_PHANTOM_CONTROLLER
pdf(req, res) {
const {id} = req.params
var fs = require('fs');
phantom.create().then(function(ph) {
ph.createPage().then(function(page) {
page.open(`${config.HTTP_HOST}/api/v1/html-report-endpoint/${id}`).then(function(status) {
var fileName = `./public/uploads/pdf/${id}.pdf`
page.render(fileName).then(function() {
// HERE RETURNS AS PDF
var file = fs.createReadStream(fileName);
var stat = fs.statSync(fileName);
res.setHeader('Content-Length', stat.size);
res.setHeader('Content-Type', 'application/pdf');
file.pipe(res);
ph.exit();
});
});
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment