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
Install phantomjs (If not Installed)
If you’re using brew, then -
command will help you install phantomjs on your machine
Installing on CentOS
If this still gives error of fontconfig/freetype, then try installing using command -
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
2) Run the js code as,
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 -
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
After adding to Gemfile, 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 -
The fetch method supports various options as given below -
Hope, this tutorial helps you to capture screenshots with Ruby. Please feel free to comment down below.
Subscribe to Ruby in Rails
Get the latest posts delivered right to your inbox