Alex Rivera | Logout

How do I display a PDF in ROR (Ruby)?

Asked 2011-09-21T13:20:56.820
44

I have looked around on the internet, but do not seem able to find how to display a PDF in rails (I can only find info on how to create one).

Does anyone know what code/gem I need to display one?

Edit
Report

1 Answer

56

In your controller:

def pdf
  pdf_filename = File.join(Rails.root, "tmp/my_document.pdf")
  send_file(pdf_filename, :filename => "your_document.pdf", :type => "application/pdf")
end

In config/environment/production.rb:

config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache

or

config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx

The config modification is required because it enables the web server to send the file directly from the disk, which gives a nice performance boost.

Update

If you want to display it instead of downloading it, use the :disposition option of send_file:

send_file(pdf_filename, :filename => "your_document.pdf", :disposition => 'inline', :type => "application/pdf")

If you want to display it inline, this question will be much more complete that I could ever be.

answered 2011-09-21T13:49:15.233

Your Answer