「The Swift Programming Language 日本語訳」の pdf化

The Swift Programming Language 日本語訳 | スタジオガラゴ

wkhtmltopdf(Linux Mint なら「ソフトウェアの管理」で入る)と pdftk(apt-get で入る)が必要。カレントディレクトリに pdf フォルダ(作業用)を作っておく。

require 'bundler/setup'
require 'mechanize'

Dir.chdir('pdf')
agent = Mechanize.new
agent.user_agent_alias = 'Linux Firefox'
page = agent.get('http://www.studiogalago.com/the-swift-programming-language/')
Nokogiri::HTML(page.body).css('div.title a').each_with_index do |node, i|
  next if i < 0    #中断用
  url = node.attribute('href').value
  title = ("%03d" % (i + 1)) + '.pdf'
  `wkhtmltopdf #{url} #{title}`
  sleep(10)
end
`pdftk *.pdf cat output swift.pdf`

サーバが重いので注意。相当に時間がかかります。