Skip to content

Instantly share code, notes, and snippets.

@derek-schaefer
Created August 3, 2012 15:10
Show Gist options
  • Save derek-schaefer/3248483 to your computer and use it in GitHub Desktop.
Save derek-schaefer/3248483 to your computer and use it in GitHub Desktop.
PhantomJS Webpage Rendering
/* Renders a web page to an image. */
var address = phantom.args[0];
var outfile = phantom.args[1];
var width = phantom.args[2] || 1440;
var height = phantom.args[3] || 900;
var page = new WebPage();
var data = {
errors: [],
address: address,
outfile: outfile,
width: width,
height: height
};
var exit_code = 0;
page.clipRect = {width:width, height:height};
page.viewportSize = {width:width, height:height};
page.open(address, function(status) {
data.status = status;
if (status === 'success') {
page.render(outfile);
} else {
exit_code = 1;
}
console.log(JSON.stringify(data));
phantom.exit(exit_code);
});
page.onError = function(msg, trace) {
data.errors.push({error:msg, trace:trace});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment