Created
April 5, 2018 12:26
-
-
Save bekatom/986621848f333b6b19a3a26889a72687 to your computer and use it in GitHub Desktop.
PDF_GENERATION_WITH_PHANTOM_CONTROLLER
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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