うちのいぬ Tech Blog

Tech Blog of Uchinoinu/My dog

Screen-capture with wkhtmltoimage(wkhtmltopdf) onLinux

In many cases, phantom.js is used to get screen capture. But wkhtmltoimage can be alternative.

Install

http://wkhtmltopdf.org/downloads.html

$ sudo yum install -y openssl libX11 libXext libXext.so.6
$ cd /usr/local/src
$ wget http://downloads.sourceforge.net/project/wkhtmltopdf/0.12.2.1/wkhtmltox-0.12.2.1_linux-centos6-amd64.rpm

$ sudo rpm -ivh wkhtmltox-0.12.2.1_linux-centos6-amd64.rpm
error: Failed dependencies:
    xorg-x11-fonts-Type1 is needed by wkhtmltox-1:0.12.2.1-1.x86_64
    xorg-x11-fonts-75dpi is needed by wkhtmltox-1:0.12.2.1-1.x86_64

Install dependencies

$ sudo rpm -ivh ftp://rpmfind.net/linux/centos/6.6/os/x86_64/Packages/ttmkfdir-3.0.9-32.1.el6.x86_64.rpm

$ sudo rpm -ivh ftp://rpmfind.net/linux/centos/6.6/os/x86_64/Packages/xorg-x11-fonts-Type1-7.2-9.1.el6.noarch.rpm

$ sudo rpm -ivh ftp://rpmfind.net/linux/centos/6.6/os/x86_64/Packages/xorg-x11-fonts-75dpi-7.2-9.1.el6.noarch.rpm

$ sudo rpm -ivh wkhtmltox-0.12.2.1_linux-centos6-amd64.rpm

Japanese Font

Install IPA font

# set font files at user home directory 
$ mkdir ~/.fonts
$ cp IPAfont00302.zip ~/.fonts
$ cd ~/.fonts 
$ unzip IPAfont00302.zip 
# clear font cache
$ fc-cache -fv
# confirm installed font
$ fc-list

if something doesn't go well....

$ sudo cp ~/.fonts/*.ttf /usr/share/fonts/

Command

$ wkhtmltoimage http://www.google.com hoge.jpg

IMGKit

https://github.com/csquared/IMGKit

if you want to edit captured image, try to use IMGKit.

but lots of error has occurred by IMGKit, so I think Phantom.js is better solution than this.

Batch Script

basic script to get screenshot and save to /tmp/screenshot/hoge.jpg

# encoding: utf-8
namespace :screenshot do
  desc "get screenshot from official_url and save tmp/screenshots/"
  task :get => :environment do

    quality       = 100
    aspect_width  = 4
    aspect_height = 3 # height : width = 4 : 3
    width         = 1200
    height        = width * 3 /4

    output_dir = "tmp/screenshots/"

    items.each do |item|
      output_file = "#{item.id}.jpg"
      file_path = output_dir + output_file
      command = "wkhtmltoimage --quality #{quality} --width #{width} --height #{height} #{url} #{file_path}"
      IO.popen("#{command}")
    end
  end

warnings

process of getting screenshot sometimes stop by some reasons. network trouble, batch-server-spec, js, css... so try many time to get image completely.

ref