html to pdf · pdf · Ruby · watir-webdriver

Convert HTML webpage to PDF using WatirWebDriver + Pdfcrowd

Installation

Sign up and obtain your username and API key — https://pdfcrowd.com/user/sign_in/

$ gem install pdfcrowd
Or download pdfcrowd-2.3.0.gem and run

Get the API key from your account page

Code:

require 'rubygems'
require 'pdfcrowd'
require 'watir-webdriver'

# create an API client instance
username="YOUR USERNAME"
apikey="YOUR API KEY"
client = Pdfcrowd::Client.new(username, apikey)

# convert an HTML string and save the result to a file
browser=Watir::Browser.new :ff
browser.goto("https://raveendran.wordpress.com")
html=browser.html
File.open('html.pdf', 'wb') {|f| client.convertHtml(html, f)}

# retrieve the number of tokens in your account
ntokens = client.numTokens()

Output:

After ran the script, you will get the html.pdf file in the same directory.

Jazzez · pdf-reader · Prawn · Ruby · Ruby 1.9 · ruby excercise · watir

Convert Webpage into PDF files using Ruby gems — Watir-Webdriver + Prawn

Installation : 

1. Install Ruby 1.8.7 or 1.9.2

2. Install WatirWebdriver — Refer  http://rubygems.org/gems/watir-webdriver

a. CMD>gem install watir-webdriver

3. Install Prawn Library

a. CMD>gem install prawn

Sample Code:

require 'rubygems'
require 'watir-webdriver'
require 'prawn'

ff=Watir::Browser.new :ff

ff.goto("https://raveendran.wordpress.com")

Prawn::Document.generate('c:\\test123\\hello.pdf') do |pdf|
pdf.text(ff.text)
end

ff.close

Output:

hello PDF  file contains all the TEXT from Webpage

 

Open Issues:

Alignment issue occurs depends upon web page Design. But You wont miss to collect all the Text from Webpage.