Skip to content

Instantly share code, notes, and snippets.

@moodysalem
Created March 2, 2016 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moodysalem/b4f855aeab2611503744 to your computer and use it in GitHub Desktop.
Save moodysalem/b4f855aeab2611503744 to your computer and use it in GitHub Desktop.
Render a page to PDF using phantomjs 2.1
"use strict";
var page = require('webpage').create(),
system = require('system'),
address, output, size;
if (system.args.length !== 3) {
console.log('Usage: rasterize.js URL filename [paperwidth*paperheight|paperformat] [zoom]');
console.log(' paper (pdf output) examples: "5in*7.5in", "10cm*20cm", "A4", "Letter"');
console.log(' image (png/jpg output) examples: "1920px" entire page, window width 1920px');
console.log(' "800px*600px" window, clipped to 800x600');
phantom.exit(1);
} else {
address = system.args[1];
output = system.args[2];
page.viewportSize = { width: 816, height: 1056 };
page.paperSize = {
width: '8.5in',
height: '11in',
margin: '0px'
};
page.open(address, function (status) {
page.evaluate(function () {
document.body.style.zoom = 0.75;
});
if (status !== 'success') {
console.log('Unable to load the address!');
phantom.exit(1);
} else {
window.setTimeout(function () {
page.render(output);
phantom.exit();
}, 200);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment