Ruby,CSV and pdf's
<p>So I am converting urls into images and downloading them into a document. The file can be an .jpg or .pdf. I can successfully download the pdf and there is something on the pdf (in form of memory) but when I try to open the pdf, adobe reader does not recognize it and deem it broken.</p>

<p>Here is a link to one of the URLs - <a href="http://www.finfo.se/www.artdb.finfo.se/cgi-bin/lankkod.dll/lev?knr=7770566&amp;art=001317514&amp;typ=PI" rel="nofollow">http://www.finfo.se/www.artdb.finfo.se/cgi-bin/lankkod.dll/lev?knr=7770566&amp;art=001317514&amp;typ=PI</a></p>

<p>And here is the code =></p>

<pre><code>require 'open-uri'
require 'tempfile'
require 'uri'
require 'csv'

DOWNLOAD_DIR = "#{Dir.pwd}/PI/"
CSV_FILE = "#{Dir.pwd}/konvertera4.csv"

def downloadFile(id, url, format)

  begin
    open("#{DOWNLOAD_DIR}#{id}.#{format}", "w") do |file|
      file &lt;&lt; open(url).read
      puts "Successfully downloaded #{url} to #{DOWNLOAD_DIR}#{id}.#{format}"
    end
  rescue Exception =&gt; e
    puts "#{e} #{url}"
  end
end


CSV.foreach(CSV_FILE, headers: true, col_sep: ";") do |row|
  puts row
  next unless row[0] &amp;&amp; row[1]
  id = row[0]
  format = row[1].match(/PI\.(.+)$/)&amp;.captures.first
  puts format
  #format = "pdf"
  #format = row[1].match(/BD\.(.+)$/)&amp;.captures.first
  url = row[1].gsub ".pdf", ""
  downloadFile(id, url, format)
end
</code></pre>