“Read ruby” (the ruby 1.9 book) as PDF

Open books (like Read Ruby organized by runpaint) are great,
but if there is no pdf version, how to read/print it properly !?

Converting to pdf…

require 'rubygems'
require 'open-uri'
require 'hpricot'

url = 'http://ruby.runpaint.org'

links = Hpricot(open(url).read).search('a').map{|link| link['href'] }
links.reject!{|link| link.include?('#') or link.include?('//') or link.include?('@') }
links -= ['/opensearch', '/toc']
links.unshift('/toc')
links.map!{|link| url+link }

content = links.map{|link| open(link).read }

html = "#{content * "/n/n/n/n"}"

out = 'temp.html'
File.open(out,'w'){|f| f.print html }

`wkhtmltopdf #{out} temp.pdf`

(or download the version from 2010-09-21)

Have fun!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s