Forum: Ruby on Rails pdf-file tot desktop with pdf/writer problem

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.
Rudy (Guest)
on 2007-04-16 21:04
(Received via mailing list)
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
Bill Walton (Guest)
on 2007-04-16 21:21
(Received via mailing list)
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
Rudy (Guest)
on 2007-04-16 22:20
(Received via mailing list)
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.
Andy Triboletti (Guest)
on 2007-04-16 22:34
(Received via mailing list)
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
Rudy (Guest)
on 2007-04-16 22:45
(Received via mailing list)
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.
Bill Walton (Guest)
on 2007-04-16 22:48
(Received via mailing list)
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
Rudy (Guest)
on 2007-04-17 19:11
(Received via mailing list)
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.
Bill Walton (Guest)
on 2007-04-17 19:35
(Received via mailing list)
Hi Ruby,

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

Best regards,
Bill
This topic is locked and can not be replied to.