0

I'm looking at Watir-Webdriver to manipulate a browser. In particular, I'd like to open a local file and print it to a PDF file.

Yes, wkhtmltopdf would be a good thing, but it's not working for me on debian squeeze, for reasons that are difficult to ascertain. The page contains Javascript, which rules out many html-to-pdf options. wkhtmltopdf works on OS X, same version (0.9.9), so I know it's not a problem with how I'm using it (PDFKit and Ruby). I'd just like to sidestep these issues and try a different way. Opening up chromium on debian shows a perfectly rendered page.

How does one "print" from Watir?

Edit: After more reading, I think there is no way to do this.

Chuck van der Linden
  • 6,660
  • 2
  • 28
  • 43
dfrankow
  • 20,191
  • 41
  • 152
  • 214
  • The 'more reading' link above is for a pretty old version of watir, not even watir-webdriver. A more current version of the rdoc would be here: http://rubydoc.info/github/watir/watir-webdriver/master/frames. Although it doesn't really change the answer that there is no direct way to do this with watir (unless the screenshot trick works or there is a webdriver function for this) – Chuck van der Linden Feb 21 '12 at 22:46

2 Answers2

2

You could take a png sreenshot, then use the prawn gem to convert the png screenshot to a pdf:

require 'prawn'
require 'watir-webdriver'

b = Watir::Browser.start 'watirwebdriver.com'
b.driver.save_screenshot 'screenshot.png'
Prawn::Document.generate 'screenshot.pdf' do
  image 'screenshot.png', :scale => 0.5
end
b.close 
Alister Scott
  • 3,675
  • 24
  • 41
  • Not sure if I'll use this solution, but it works, you crazy genius. +1 – dfrankow Feb 21 '12 at 15:59
  • Note: the screenshot is Selenium (b.driver). It doesn't actually require Watir. – dfrankow Feb 21 '12 at 22:30
  • not sure what you mean. Yes, you can do this via selenium-webdriver, but watir-webdriver is simply an API over selenium-webdriver, hence the name – Alister Scott Feb 21 '12 at 22:43
  • I was just noting for myself that Watir is not required. In this case, the Selenium code looks almost the same. Fewer dependencies is often better. However, I accepted your answer. – dfrankow Feb 22 '12 at 23:10
0

You'll need to use something that lets you do automation at the OS level. such as Autoit or maybe RAutomation. not sure what exists to do this on *nix operating systems.

Watir only drives the browser in terms of what is inside the browser window, it has very limited capability to work the menus of the browser itself.

Chuck van der Linden
  • 6,660
  • 2
  • 28
  • 43