Skip to content

Instantly share code, notes, and snippets.

@avtaniket
Created February 7, 2017 09:05
Show Gist options
  • Select an option

  • Save avtaniket/fa0b9657db5195fbe30eccc6e211e509 to your computer and use it in GitHub Desktop.

Select an option

Save avtaniket/fa0b9657db5195fbe30eccc6e211e509 to your computer and use it in GitHub Desktop.
PDF generation using Phantom Js
var params = {
input: outputHtmlFile,
output: outputPdf
};
generatePDF(params, function(err, response) {
if (err) {
callback('Unable to generate PDF');
} else {
}
});
/**
* [generatePDF - Generate PDF ]
* @param {object} params [{input:file,output:file,config:{}}]
* @param {Function} callback
*/
function generatePDF(params, callback) {
var phInstance = null;
phantom.create()
.then(function(instance) {
phInstance = instance;
return instance.createPage();
})
.then(function(page) {
// page.property('viewportSize', {
// width: 800,
// height: 600
// });
page.property('paperSize', {
format: 'A4',
orientation: 'landscape',
margin: 20
});
page.property('zoomFactor', 1);
page.open(params.input)
.then(function(status) {
if (status == 'fail') {
phInstance.exit();
callback('Unable to generate PDF');
} else {
page.render(params.output, {
format: 'pdf',
quality: '100'
}).then(function() {
page.close();
phInstance.exit();
callback();
});
}
});
})
.catch(function(error) {
phInstance.exit();
logger.error('phantom', error);
callback('Unable to generate PDF')
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment