将网页html转化成pdf
gem包安装:
1. gem install 'wicked_pdf'
2. gem install 'wkhtmltopdf-binary'
3. gem install 'httpclient'
4. gem install 'hpricot'
require 'wicked_pdf'
require 'httpclient'
require 'hpricot'
@client = HTTPClient.new
def run
get_list("http://www.runoob.com/redis/redis-tutorial.html")
end
def get_list(url)
doc = Hpricot(@client.get(url).body)
doc.search("#leftcolumn").search("a").each do |x|
puts hrf = x[:href]
puts title = x.inner_html
href = "http://www.runoob.com#{hrf}"
save_to_pdf(href,title)
end
end
def save_to_pdf(url,title)
pdf = WickedPdf.new.pdf_from_url(url,encoding:"utf-8")
File.open("#{title}.pdf",'ab+') do |file|
file.puts pdf
end
end
run