Last active
December 20, 2015 08:49
-
-
Save RReverser/6103562 to your computer and use it in GitHub Desktop.
Rendering page with PhantomJS with custom dimensions (not possible w/o hack)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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