/ RAILS

Ruby Screenshot Website Capture – Screencap, Phantomjs

This tutorial will discuss various tools for Ruby Screenshot capture such as

  • Phantomjs (Command-line)
  • Screencap (Ruby gem)

Objective

You need to capture screenshot of a particular web page and you want to do that programmatically by just passing an url then you can use of the gems that I have listed above. After reading this article, you will be able to select an option to use for your needs.

1. Phantomjs

phantom-js

Install phantomjs (If not Installed)

If you’re using brew, then -

brew install phantomjs

command will help you install phantomjs on your machine

Installing on CentOS

cd ~
wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-1.9.7-linux-i686.tar.bz2
bunzip2 phantomjs*.tar.bz2
tar xvf phantomjs*.tar
sudo cp phantomjs*/bin/phantomjs /usr/bin/phantomjs
phantomjs -v

If this still gives error of fontconfig/freetype, then try installing using command -

sudo yum install fontconfig freetype libfreetype.so.6 libfontconfig.so.1 libstdc++.so.6
phantomjs -v
2.0.0

This should list the installed version of phantomjs.

Usage

It’s fairly easy to capture screenshots using phantomjs. Following code will help you-

Steps

1) Create a .js file with code to capture screenshots - let’s say rubyinrails-snapshot.js

var page = require('webpage').create();
page.open('https://rubyinrails.com/', function() {
  page.render('screenshot-rubyinrails-com.png');
  phantom.exit();
});

2) Run the js code as,

phantomjs rubyinrails-snapshot.js

This code will capture and save screenshots by file name screenshot-rubyinrails-com.png

Screen Capture using Phantomjs command line - This article shows how to capture screenshots with phantomjs in detail

2. Screencap - a Ruby Screenshot capturing gem

  • This is a ruby gem developed on top of phantomjs.

    You need to have phantomjs installed on your machine to capture screenshots with this gem.

  • Source - Screencap Github Source

Installation

  • Install gem by -
gem install screencap

This command will install screencap on your machine

  • If you need to use gem from your Rails application then you need to add it to the Gemfile
gem 'screencap'

After adding to Gemfile, bundle

bundle

This command will ensure availability of your gem your Rails application

Usage

Using screencap gem to capture screenshots is fairly easy. The following code will help you capturing screenshots with ruby -

require 'screencap'
fetcher_object = Screencap::Fetcher.new('https://rubyinrails.com')
screenshot = fetcher_object.fetch

The fetch method supports various options as given below -

screenshot = fetcher_object.fetch(
output: '~/some-directory/file-name.png',
# optional parameters:
div: '.header', # CSS selector
width: 1024, # Width of Screenshot - Viewport
height: 768, # Height of Screenshot - Viewport
top: 0, left: 0, width: 100, height: 100 # Area to capture screenshot of)

Hope, this tutorial helps you to capture screenshots with Ruby. Please feel free to comment down below.

akshay

Akshay Mohite

Hi there! I am a Ruby on Rails & ReactJS Enthusiast, building some cool products at DTree Labs.

Read More
Buy me a coffee