Skip to content

Instantly share code, notes, and snippets.

@merlos
Last active August 9, 2018 16:15
Show Gist options
  • Save merlos/41e0f51446b7d2f3dd91ed89e9f43430 to your computer and use it in GitHub Desktop.
Save merlos/41e0f51446b7d2f3dd91ed89e9f43430 to your computer and use it in GitHub Desktop.
Simple phantomJS script to create a PDF from a URL or a HTML file
/**
* Simple script to create a PDF from a URL.
* Usage:
* $ phantomjs topdf.js
*
*/
var page = require('webpage').create();
page.paperSize = {
format: 'A2', //Can be "Letter", "A4", "A3" http://phantomjs.org/api/webpage/property/paper-size.html
orientation: 'Portrait',
margin: '1cm'
};
// By default Phantom renders a mobile screen size. With these two settings you can control the viewport.
page.settings.userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36';
page.viewportSize = {
width: 1200,
height: 1200
};
//Set your URL
// Output is guidelines.pdf in the same folder the script is saved.
//
page.open('http://localhost:4000/guidelines.html', function() {
page.render('guidelines.pdf',{format: 'pdf'});
phantom.exit();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment