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.

pdf · QA · Ruby · watir-webdriver

Convert web page(using Watir-Webdriver) to PDF File (Its my 150th post :) )

Question/Requirement:

1. I just navigated to the webpage “http://humblelittlerubybook.com/book/hlrb.pdf”

2. I want to Save the web page as a PDF File.

Solution:

1. Install Ruby

2. Install Watir-webdriver (CMD>gem install watir-webdriver)

3. Register Autoitx3.dll (https://raveendran.wordpress.com/2012/06/15/how-to-installregister-autoitx3-dll/)

4. Run the Below Script

Code:

require 'rubygems'
require 'watir-webdriver'
require 'win32ole'

ie=Watir::Browser.new :ie
ie.goto(“http://humblelittlerubybook.com/book/hlrb.pdf”)

sleep 15
$autoit = WIN32OLE.new(“AutoItX3.Control”)

$autoit.Send(“{ALTDOWN}”)
sleep 3
$autoit.Send(“f”)
sleep 3
$autoit.Send(“{ALTUP}”)
sleep 3

$autoit.Send(“{DOWN}”)
sleep 3
$autoit.Send(“{DOWN}”)
sleep 1
$autoit.Send(“{DOWN}”)
sleep 1
$autoit.Send(“{DOWN}”)
sleep 1
$autoit.Send(“{DOWN}”)
sleep 1
$autoit.Send(“{DOWN}”)
sleep 1
$autoit.Send(“{ENTER}”)
sleep 10
$autoit.Send(“D:\\test1.pdf”)
sleep 3
$autoit.Send(“{TAB}”)
sleep 1
$autoit.Send(“{TAB}”)
sleep 1
$autoit.Send(“{ENTER}”)
sleep 1

Note:

1. Depends upon your browser you may need to add/remove the {TAB} and {DOWN}  lines

2. Change the destination folder.

Output:

PDF file saved in the mentioned location D:\\test1.pdf