Skip to content

Instantly share code, notes, and snippets.

@satomixx
Created March 31, 2015 03:03
Show Gist options
  • Save satomixx/db01e8de92c2ec5ca86e to your computer and use it in GitHub Desktop.
Save satomixx/db01e8de92c2ec5ca86e to your computer and use it in GitHub Desktop.
[Rails 4.x] Linuxで、wkhtmltoimage(wkhtmltopdf) を利用して WebサイトのScreenshot を取る ref: http://qiita.com/tsumekoara/items/b683bcc3564fcef8d3af
# 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
$ 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
$ 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
# 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
$ sudo cp ~/.fonts/*.ttf /usr/share/fonts/
$ wkhtmltoimage http://www.google.com hoge.jpg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment