Capture web page screenshot with PhantomJS | Trần Đình Thoại

Capture web page screenshot with PhantomJS

I used to be upset when I can not find out how to capture screenshot of web page on server. Now, I find PhantomJS which can help me easily to capture screenshot. This is a script to do:

var system = require('system');
if (system.args == null || system.args.length < 3) {
  console.log('Usage: render.js filename URL [width]');
  phantom.exit();
}
var filename = 'screenshot.png';
if (system.args.length > 1) {
  filename = system.args[1];
}
var url = 'http://codingore.blogspot.com/2016/03/resume-of-tran-inh-thoai.html';
if (system.args.length > 2) {
  url = system.args[2];
}
var width = 1024;
if (system.args.length > 3) {
  width = parseInt(system.args[3]);
}
var page = require('webpage').create();
page.viewportSize = { width: width, height: 500};
page.open(url);
page.onLoadFinished = function() {
  var height = page.evaluate(function() { return document.body.offsetHeight });
  var page2 = require('webpage').create();
  page2.viewportSize = {
    width: width,
    height: height
  };
  page2.open(url, function(status) {
    if(status === "success") {
      page2.render(filename);
    }
    phantom.exit();
  });
};
Tuesday, March 8, 2016 at 12:27 AM