viewportSize
viewportSize
{object}
This property sets the size of the viewport for the layout process. It is useful to set the preferred initial size before loading the page, e.g. to choose between 'landscape'
vs 'portrait'
.
Because PhantomJS is headless (nothing is shown), viewportSize
effectively simulates the size of the window like in a traditional browser.
Examples
var webPage = require('webpage');
var page = webPage.create();
page.viewportSize = {
width: 480,
height: 800
};
You must include height for this to work.
var page = require('webpage').create(),
address, output, size;
var system = require('system');
var arg_count = system.args.length - 1;
if (arg_count < 4 || arg_count > 5) {
console.log('Usage: viewport.js URL filename sizeX sizeY');
phantom.exit();
} else {
address = system.args[1];
output = system.args[2];
sizeX = system.args[3];
sizeY = system.args[4];
page.viewportSize = { width: sizeX, height: sizeY };
page.open(address, function (status) {
if (status !== 'success') {
console.log('Unable to load the address!');
} else {
window.setTimeout(function () {
page.render(output);
phantom.exit();
}, 200);
}
});
}
Usage: viewport.js URL filename sizeX sizeY