Skip to content

Instantly share code, notes, and snippets.

@possatti
Created August 29, 2014 03:06
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 possatti/31eabd25c28e675735e9 to your computer and use it in GitHub Desktop.
Save possatti/31eabd25c28e675735e9 to your computer and use it in GitHub Desktop.
// PhantomJS script for rendering a page to an image.
// Author: Lucas Possatti
// Require system to get the command line arguments.
var system = require('system');
// Verify if the program received the proper arguments. And warns the user
// if necessary.
if (system.args.length != 3) {
console.log('Usage: phantomjs picturize.js page_url image_path');
console.log('\n`picturize.js` renders the specified page to an image on your disk.');
// Capture the image if everything is fine.
} else {
// Get the arguments.
var url = system.args[1];
var image_path = system.args[2];
// Make garanteed the file extension is png. Otherwise, the
// `page.render()` may not work.
if (!image_path.match('.*\.png')) {
image_path = image_path + '.png';
}
// Tell the user to wait.
console.log(' >> Rendering ' + url + ' to ' + image_path + ' ...');
// Create a new page.
var page = require('webpage').create();
// Load URL.
page.open(url, function() {
// Render page to an image and save.
page.render(image_path);
// Finish the program.
console.log(' >> Rendering finished.');
phantom.exit();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment