Forum: Ruby Saving a PDF locally

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9ba33dabca19a6a223ea8a9931288409?d=identicon&s=25 Yochen Gutmann (yoche2001)
on 2006-05-30 00:29
Hi Ruby-folks,

I am currently working on a small program, which saves copies of website
locally to my harddisk. For normal html-pages this works as expected.
But now I am struggeling with binary files such as PDFs.

Here is my code:

open("http://www.somewebpage.com/atestfile.pdf){|u|
   targetFile = File.new("test.pdf,"w")
   u.each_byte {|ch|
     targetFile.putc ch
   }
}

The resulting local file cannot be opened with my pdf-reader. When I
open it in an editor, there seems only to be numbers in the file (->not
binary).

instead of putc if tried write which did not work, too.

Any hints?

Yochen
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2006-05-30 00:40
(Received via mailing list)
Yochen Gutmann wrote:
> Hi Ruby-folks,
>
> I am currently working on a small program, which saves copies of website
> locally to my harddisk. For normal html-pages this works as expected.
> But now I am struggeling with binary files such as PDFs.
>
>    targetFile = File.new("test.pdf,"w")

Use "wb" instead of "w". On windows, this treats the data as binary
instead of lines of text that should be terminated with cr-lf.
9ba33dabca19a6a223ea8a9931288409?d=identicon&s=25 Yochen Gutmann (yoche2001)
on 2006-05-30 00:48
Joel VanderWerf wrote:
> Use "wb" instead of "w". On windows, this treats the data as binary
> instead of lines of text that should be terminated with cr-lf.
well, although I am working on OSX (forgot to mention), I tried your
hint but that did not work(like expected). Hm.. Any other idea?

-Yochen
9358cc96c46055cd68d4a76a9aefe026?d=identicon&s=25 Daniel Harple (Guest)
on 2006-05-30 00:55
(Received via mailing list)
On May 30, 2006, at 12:29 AM, Yochen Gutmann wrote:

> open("http://www.somewebpage.com/atestfile.pdf){|u|
>    targetFile = File.new("test.pdf,"w")
>    u.each_byte {|ch|
>      targetFile.putc ch
>    }
> }

Try:

open("http://www.somewebpage.com/atestfile.pdf", 'rb'){|u|
    File.open("test.pdf", "wb") do |f|
      f.write(u.read)
    end
}

-- Daniel
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2006-05-30 01:05
(Received via mailing list)
Yochen Gutmann wrote:
> Joel VanderWerf wrote:
>> Use "wb" instead of "w". On windows, this treats the data as binary
>> instead of lines of text that should be terminated with cr-lf.
> well, although I am working on OSX (forgot to mention), I tried your
> hint but that did not work(like expected). Hm.. Any other idea?
>
> -Yochen
>

Sorry! I jumped to conclusions about the problem.

The following works for me on linux. Can't make any predictions about
OSX, tho'.

require 'open-uri'

open("http://path.berkeley.edu/~vjoel/redshift/ruby-sdfo...
   targetFile = File.new("test.pdf","w")
   u.each_byte {|ch|
     targetFile.putc ch
   }
}


Why are you doing it a byte at a time? This seems to run much faster for
me:

require 'open-uri'

open("http://path.berkeley.edu/~vjoel/redshift/ruby-sdfo...) do |u|
  targetFile = File.new("test.pdf","w")
  loop do
    dat = u.read(1000)
    break unless dat
    targetFile.write dat
  end
end
9ba33dabca19a6a223ea8a9931288409?d=identicon&s=25 Yochen Gutmann (yoche2001)
on 2006-05-30 01:21
Joel VanderWerf wrote:
> Why are you doing it a byte at a time?
just ran out of ideas ;-)

> This seems to run much faster for me:
>
> require 'open-uri'
>
> open("http://path.berkeley.edu/~vjoel/redshift/ruby-sdfo...) do |u|
>   targetFile = File.new("test.pdf","w")
>   loop do
>     dat = u.read(1000)
>     break unless dat
>     targetFile.write dat
>   end
> end

Fantastic!

Thanx a lot.

-Yochen
9ba33dabca19a6a223ea8a9931288409?d=identicon&s=25 Yochen Gutmann (yoche2001)
on 2006-05-30 01:25
Daniel Harple wrote:
> Try:
>
> open("http://www.somewebpage.com/atestfile.pdf", 'rb'){|u|
>     File.open("test.pdf", "wb") do |f|
>       f.write(u.read)
>     end
> }
Thanx, Daniel, your solution is working as well! And it is even shorter!


Slowly I am wondering why I did't come up with a working solution by
myself ;-]

-- Yochen
This topic is locked and can not be replied to.