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

Advertisements

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

  1. Hi,
    I tried the above script, there is no issue in execution but no pdf is created. Actually my issue is : I need to verify the embed-pdf content (IE & Chrome browser). I can handle the newly opened pdf browser window but could not copy the text of that pdf content. Could you pls help me in fixing this.

  2. Dear Chan,

    It seems you are trying to verify the URL which contains .pdf at the end. To test the PDF content you need to do 2 steps here

    Step 1 :

    require ‘rubygems’
    require ‘open-uri’

    file1=”http://yourwebsite/sample.pdf” #browser.url will give the current page url

    File.open(“test.pdf”, “wb”) do |file|
    file.write open(file1).read
    end

    At the end of Step1 , You will have test.pdf in your local machine

    Step2:

    With the help of PDF Reader or some other library you can read the PDF content and verify the expected result.

    Please update if any other details required

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s