html to pdf · pdf · Ruby · watir-webdriver

Convert HTML webpage to PDF using WatirWebDriver + Pdfcrowd


Sign up and obtain your username and API key —

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

Get the API key from your account page


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

# create an API client instance
username="YOUR USERNAME"
apikey="YOUR API KEY"
client =, apikey)

# convert an HTML string and save the result to a file :ff
html=browser.html'html.pdf', 'wb') {|f| client.convertHtml(html, f)}

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


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


One thought on “Convert HTML webpage to PDF using WatirWebDriver + Pdfcrowd

  1. Raveendran, how can i select a particular portion of window or screen ? Basically i wanted to take a screenshot of a selection, using driver.save_screenshot(“./abc.png”) i can take the whole page, which i dont want.

    So if there is a way to get a portion of page either in png or pdf, it will work for me.

Leave a Reply

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

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

Google+ photo

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

Twitter picture

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

Facebook photo

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

Connecting to %s