Skip to content

Instantly share code, notes, and snippets.

@joaodubas
Created February 16, 2012 13:59
Show Gist options
  • Save joaodubas/1845011 to your computer and use it in GitHub Desktop.
Save joaodubas/1845011 to your computer and use it in GitHub Desktop.
Render the content of a page using PhantomJS
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 João Paulo Dubas <http://aval.io>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
/**
* Render the 001.html located on the current directory, and save it on the thumb
* folder as 001.png.
*
* **Usage**
* $ phantomjs render
*
* __author__: Joao P Dubas
* __email__: joao.dubas at gmail.com
*/
var fs = require('fs');
var page = require('webpage').create(),
options = {
address: {
origin: ['file:/', fs.workingDirectory, '001.html'].join(fs.separator),
destin: [fs.workingDirectory, 'thumbs', '001.png'].join(fs.separator)
},
size: {
width: 1024,
height: 768
},
timer: 5000
};
var pageRendering = {
page: null,
options: null,
msg_count: 1,
exit: function exit() {
phantom.exit();
},
log: function log(msg) {
var self = pageRendering;
console.log(self.msg_count + '. ' + msg);
self.msg_count += 1;
},
renderPage: function renderPage() {
var self = pageRendering;
self.log('Rendering page to: ' + self.options.address.destin);
page.render(self.options.address.destin);
self.exit();
},
loadPage: function loadPage(status) {
var self = pageRendering;
if (status !== 'success') {
self.log('OMG, something went wrong!');
self.exit();
return;
}
self.log('Opened page: ' + options.address.origin);
window.setTimeout(self.renderPage, options.timer);
},
init: function init(page, options) {
this.page = page;
this.options = options;
this.log('Setting viewport size to: ' + this.options.size.width +
' x ' + this.options.size.height);
this.page.viewportSize = this.options.size;
this.log('Trying to open: ' + this.options.address.origin);
this.page.open(options.address.origin, this.loadPage);
}
};
pageRendering.init(page, options);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment