# Pdf-file tot desktop with pdf/writer problem

**URL:** <https://www.ruby-forum.com/t/pdf-file-tot-desktop-with-pdf-writer-problem/92172>\
**Category:** Rails\
**Created:** [April 16, 2007, 9:04pm UTC](https://www.ruby-forum.com/t/pdf-file-tot-desktop-with-pdf-writer-problem/92172 "2007-04-16T21:04:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rudy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/5fc32e/32.png) [@rudy](https://www.ruby-forum.com/u/rudy)\
**Post date:** [April 16, 2007, 9:04pm UTC](https://www.ruby-forum.com/t/pdf-file-tot-desktop-with-pdf-writer-problem/92172/1 "2007-04-16T21:04:29Z")

</div>

Hi,  
I’m using pdf/Writer to generate a pdf-file, but whatever I do, it  
gets saved in folder of my app, and not on the users desktop.

I’ve used

pdf = PDF::Writer.new  
pdf.select\_font “Times-Roman”  
pdf.text “Hello.pdf”, :font\_size =\> 12, :justification  
=\> :left  
File.open(“hello.pdf”, “wb”) { |f| f.write pdf.render }

and Ive used

\_pdf = PDF::Writer.new  
\_pdf.select\_font “Times-Roman”  
\_pdf.text “Hello, Ruby.”, :font\_size =\> 72, :justification  
=\> :center

```
      send_data _pdf.render, :filename => "hello.pdf",
                :type => "application/pdf"
    end

```

Both save to my folder on the server, but no download or saving to the  
desktop. I must me missing something here. (The action is in my  
general controlller, which has a layout. I don’t know if that has  
anything to do with it, but I mention it just in case…)

Could someone help me out on this simple one? Thanks in advance.  
Rudy

---

<div class="post-metadata">

**Author:** ![rudy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/5fc32e/32.png) [@rudy](https://www.ruby-forum.com/u/rudy)\
**Post date:** [April 16, 2007, 9:21pm UTC](https://www.ruby-forum.com/t/pdf-file-tot-desktop-with-pdf-writer-problem/92172/2 "2007-04-16T21:21:31Z")

</div>

Hi Rudy,

Rudy wrote:

> I’m using pdf/Writer to generate a pdf-file, but whatever I do, it  
> gets saved in folder of my app, and not on the users desktop.

That’s the appropriate behavior. If you want it to go to the user, you  
need  
to use send\_file after the file is written to the folder on the server.  
Note that send\_file counts as a render so you’ll need to structure your  
app  
accordingly to get the UI behavior you want.

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![rudy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/5fc32e/32.png) [@rudy](https://www.ruby-forum.com/u/rudy)\
**Post date:** [April 16, 2007, 10:34pm UTC](https://www.ruby-forum.com/t/pdf-file-tot-desktop-with-pdf-writer-problem/92172/3 "2007-04-16T22:34:36Z")

</div>

Are you accessing that pdf controller in a web browser? The user  
should go to the controller url, be asked to save the pdf, select the  
place to save it, then the pdf streams to them.

Andy

---

<div class="post-metadata">

**Author:** ![rudy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/5fc32e/32.png) [@rudy](https://www.ruby-forum.com/u/rudy)\
**Post date:** [April 16, 2007, 10:20pm UTC](https://www.ruby-forum.com/t/pdf-file-tot-desktop-with-pdf-writer-problem/92172/4 "2007-04-16T22:20:35Z")

</div>

Hi Bill

When I do

pdf = PDF::Writer.new  
pdf.select\_font “Times-Roman”  
pdf.text “Hello.pdf”, :font\_size =\> 12, :justification  
=\> :left  
pdf.save\_as “hello.pdf”  
send\_file “hello.pdf”, :filename =\> ‘hello.pdf’, :type =\>  
‘application/pdf’

the last line on the logfile is

Streaming file hello.pdf

But nothing appears on the desktop. I just don’t get it.

---

<div class="post-metadata">

**Author:** ![rudy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/5fc32e/32.png) [@rudy](https://www.ruby-forum.com/u/rudy)\
**Post date:** [April 16, 2007, 10:45pm UTC](https://www.ruby-forum.com/t/pdf-file-tot-desktop-with-pdf-writer-problem/92172/5 "2007-04-16T22:45:38Z")

</div>

Hi Andy  
thanks for answering. Sorry about this newbiething, but I don’t  
exacltly understand your question.

My user clicks in a browser on a specific icon when he wants some info  
saved as a pdf-file. This icon invokes an action, that sits in de main  
admin\_controller. There is no rhtml or anything else connected to the  
action.

The strange thing is that I used the same bit with Spreadsheet/excel,  
and that works fine.

---

<div class="post-metadata">

**Author:** ![rudy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/5fc32e/32.png) [@rudy](https://www.ruby-forum.com/u/rudy)\
**Post date:** [April 16, 2007, 10:48pm UTC](https://www.ruby-forum.com/t/pdf-file-tot-desktop-with-pdf-writer-problem/92172/6 "2007-04-16T22:48:38Z")

</div>

Hi Rudy,

Rudy wrote:

> the last line on the logfile is
> 
> Streaming file hello.pdf
> 
> But nothing appears on the desktop. I just don’t get it.  
> First, nothing will appear on the desktop without user intervention.  
> The  
> send\_file should cause a “view or save” dialog to be displayed on the  
> visitor’s system and then, assuming they choose ‘save’, they go from  
> there.

I assume you’re not seeing the ‘view or save’ dialog, which means you’ve  
got  
a problem. I don’t see anything obviously wrong with your code. I  
normally  
use explicit paths, but I’m pretty sure if Rails couldn’t find the file  
it  
would throw an error. So that’s probably not it. I’ve not used  
pdf.save so  
my own investigation would start there. What I do to create / save a  
file  
(I assume you’ve checked and you actually have a viewable pdf file?) is:  
pdf = PDF::Writer.new  
…  
File.open("#{RAILS\_ROOT}/private/#{filenametouse}", “wb”) { |f| f.write  
pdf.render}

then I’m doing a redirect to another controller and there I do:

send\_file("#{RAILS\_ROOT}/private/#{filetosend}", :filename =\>  
“#{suggestfile}.pdf”, :stream =\> true)

If moving to an explicit file creation like above didn’t help, I’d go to  
my  
sandbox and try getting send\_file to work on some existing, known-good  
files.

Sorry I can’t be more help,  
Bill

---

<div class="post-metadata">

**Author:** ![rudy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/5fc32e/32.png) [@rudy](https://www.ruby-forum.com/u/rudy)\
**Post date:** [April 17, 2007, 7:35pm UTC](https://www.ruby-forum.com/t/pdf-file-tot-desktop-with-pdf-writer-problem/92172/7 "2007-04-17T19:35:02Z")

</div>

Hi Ruby,

Glad to hear you got it figured out. Nice form, too.

Best regards,  
Bill

---

<div class="post-metadata">

**Author:** ![rudy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/5fc32e/32.png) [@rudy](https://www.ruby-forum.com/u/rudy)\
**Post date:** [April 17, 2007, 7:11pm UTC](https://www.ruby-forum.com/t/pdf-file-tot-desktop-with-pdf-writer-problem/92172/8 "2007-04-17T19:11:58Z")

</div>

Hi Bill and Andy

Silly ME! After using some code of a spreadsheetdownload that worked  
and was within the same controller, I got the same result. Streaming  
but no file. So then lightning struck: must have something to do with  
triggering the action!

I just write this down here as a sort of contribution. One day in the  
future someone may face the same problem. I’ve found so many solutions  
for problems on this list the past few months, that its the least I  
can do.

I was launching the action in the view with a remote\_form\_for.  
(duh…) Once I changed that into a form\_for everything worked like a  
charm. So easy to overlook, so difficult to trace. Hope this helps  
someone sometime.
