Skip to content

Instantly share code, notes, and snippets.

@RReverser
Last active December 20, 2015 08:49
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 RReverser/6103562 to your computer and use it in GitHub Desktop.
Save RReverser/6103562 to your computer and use it in GitHub Desktop.
Rendering page with PhantomJS with custom dimensions (not possible w/o hack)
console.log('Usage: phantomjs render.js URL [width=1024] [height=768] [out=render.png]');
var args = require('system').args;
if (args.length < 2) {
phantom.exit();
}
var page = require('webpage').create();
var rect = page.clipRect = {
left: 0,
top: 0,
width: args[2] || 1024,
height: args[3] || 768
};
console.log('Loading in ' + rect.width + 'x' + rect.height + ' frame');
page.onCallback = function () {
var filename = args[4] || 'render.png';
console.log('Rendering to ' + filename)
page.render(filename);
phantom.exit();
};
page.open('data:text/html,<iframe id="frame" style="position: absolute; left: 0; top: 0; border: 0; width: ' + rect.width + 'px; height: ' + rect.height + 'px"></iframe>', function () {
page.evaluate(function (url) {
var frame = document.getElementById('frame');
frame.onload = function () {
callPhantom();
};
frame.src = url;
}, args[1]);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment