# Saving a PDF locally

**URL:** <https://www.ruby-forum.com/t/saving-a-pdf-locally/61942>\
**Category:** Ruby\
**Created:** [May 30, 2006, 12:29am UTC](https://www.ruby-forum.com/t/saving-a-pdf-locally/61942 "2006-05-30T00:29:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![yoche2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/57b2e6/32.png) [@yoche2001](https://www.ruby-forum.com/u/yoche2001)\
**Post date:** [May 30, 2006, 12:29am UTC](https://www.ruby-forum.com/t/saving-a-pdf-locally/61942/1 "2006-05-30T00:29:43Z")

</div>

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("[Google](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

---

<div class="post-metadata">

**Author:** ![yoche2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/57b2e6/32.png) [@yoche2001](https://www.ruby-forum.com/u/yoche2001)\
**Post date:** [May 30, 2006, 12:40am UTC](https://www.ruby-forum.com/t/saving-a-pdf-locally/61942/2 "2006-05-30T00:40:29Z")

</div>

Yochen G. 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.

---

<div class="post-metadata">

**Author:** ![yoche2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/57b2e6/32.png) [@yoche2001](https://www.ruby-forum.com/u/yoche2001)\
**Post date:** [May 30, 2006, 12:48am UTC](https://www.ruby-forum.com/t/saving-a-pdf-locally/61942/3 "2006-05-30T00:48:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![yoche2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/57b2e6/32.png) [@yoche2001](https://www.ruby-forum.com/u/yoche2001)\
**Post date:** [May 30, 2006, 12:55am UTC](https://www.ruby-forum.com/t/saving-a-pdf-locally/61942/4 "2006-05-30T00:55:46Z")

</div>

On May 30, 2006, at 12:29 AM, Yochen G. wrote:

> open("[Google](http://www.somewebpage.com/atestfile.pdf)){|u|  
> targetFile = File.new("test.pdf,“w”)  
> u.each\_byte {|ch|  
> targetFile.putc ch  
> }  
> }

Try:

open(“[Google](http://www.somewebpage.com/atestfile.pdf)”, ‘rb’){|u|  
File.open(“test.pdf”, “wb”) do |f|  
f.write(u.read)  
end  
}

– Daniel

---

<div class="post-metadata">

**Author:** ![yoche2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/57b2e6/32.png) [@yoche2001](https://www.ruby-forum.com/u/yoche2001)\
**Post date:** [May 30, 2006, 1:05am UTC](https://www.ruby-forum.com/t/saving-a-pdf-locally/61942/5 "2006-05-30T01:05:09Z")

</div>

Yochen G. 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-sdforum.pdf](http://path.berkeley.edu/~vjoel/redshift/ruby-sdforum.pdf)”){|u|  
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-sdforum.pdf](http://path.berkeley.edu/~vjoel/redshift/ruby-sdforum.pdf)”) do |u|  
targetFile = File.new(“test.pdf”,“w”)  
loop do  
dat = u.read(1000)  
break unless dat  
targetFile.write dat  
end  
end

---

<div class="post-metadata">

**Author:** ![yoche2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/57b2e6/32.png) [@yoche2001](https://www.ruby-forum.com/u/yoche2001)\
**Post date:** [May 30, 2006, 1:25am UTC](https://www.ruby-forum.com/t/saving-a-pdf-locally/61942/6 "2006-05-30T01:25:51Z")

</div>

Daniel H. wrote:

> Try:
> 
> open(“[Google](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

---

<div class="post-metadata">

**Author:** ![yoche2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/57b2e6/32.png) [@yoche2001](https://www.ruby-forum.com/u/yoche2001)\
**Post date:** [May 30, 2006, 1:21am UTC](https://www.ruby-forum.com/t/saving-a-pdf-locally/61942/7 "2006-05-30T01:21:31Z")

</div>

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-sdforum.pdf](http://path.berkeley.edu/~vjoel/redshift/ruby-sdforum.pdf)”) 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
