Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PhantomJS capturing screenshot of mobile browser

I was trying to create a web screenshot with PhantomJS, but I am getting the image as mobile browser. I am using MAC OS Yosemite. Here is my JavaScript:

screen.js

var WebPage = require('webpage');
page = WebPage.create();
page.open('http://www.apple.com');
page.onLoadFinished = function() {
    window.setTimeout(function () {
        page.render('appleScreenShot' + '.png');
        phantom.exit();
    }, 2000);
}

And here is my command line code

phantomjs --ignore-ssl-errors=true --web-security=false --ssl-protocol=tlsv1 --debug=true screen.js
like image 574
Deepak Avatar asked Dec 05 '15 01:12

Deepak


2 Answers

You may find that you need to specify a viewportSize (and perhaps even zoomFactor) for certain websites depending on the media queries specified within their resources.

From the documentation on viewportSize:

Because PhantomJS is headless (nothing is shown), viewportSize effectively simulates the size of the window like in a traditional browser.

Example usages:

page.viewportSize = {
    width: 1280,
    height: 800
};
page.zoomFactor = 1; //default value is 1
like image 127
dbeg Avatar answered Oct 17 '22 07:10

dbeg


I suggest you to add the background color rendering, because the default will be a transparent background.

page.evaluate(function() {
    document.body.bgColor = 'white';
});

example

like image 1
luyishisi Avatar answered Oct 17 '22 06:10

luyishisi