Thanks to everyone who helped. Writing the file in Binary mode did the
trick.
In case anyone has this problem in the future here is my full script:
require 'rubygems'
require 'mechanize'
URL_LOGIN =
'https://www.bethere.co.uk/cas/login?service=https://www.bethere.co.uk/c/portal/login'
URL_BILLING = 'https://www.bethere.co.uk/group/beportal/billsandpayment'
abort "Usage: #{$0} <username> <password>" unless ARGV.length == 2
agent = Mechanize.new
agent.follow_meta_refresh = true
agent.redirect_ok = true
agent.user_agent = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6;
en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6'
login_page = agent.get(URL_LOGIN)
login_form = login_page.forms.first
login_form.username = ARGV[0]
login_form.password = ARGV[1]
redirect_page = agent.submit(login_form)
invoice_page = agent.get(URL_BILLING)
link = invoice_page.links_with(:href => "/be-portal/downloadPdf").first
page = agent.click(link)
File.open(page.filename.gsub("/","_"), 'w+b') do |file|
file << page.body.strip
end
--
Posted via http://www.ruby-forum.com/.