LoginSignup
11
11

More than 5 years have passed since last update.

[Rails 4.x] Linuxで、wkhtmltoimage(wkhtmltopdf) を利用して WebサイトのScreenshot を取る

Posted at

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

Install

$ 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

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

example.rb
# 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

11
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
11