4

I store PDF receipts in S3. I use WickedPDF for generating the PDF files. Via the admin area of our app, there is a "Download" link, that is simply a direct link to the S3 path:

link_to "Download", order.receipt.url unless order.receipt.blank?

Any idea on how I can have it default to opening in a browser window instead of direct download?

Ryan Rebo
  • 1,278
  • 2
  • 13
  • 27

4 Answers4

1

The trick is to change the headers, if you link to a static file your webserver (S3) will add a header Content-Disposition: attachment; filename="name.pdf" without this header the browser will try to render this inline (same window).

To solve this issue you could have an action on your controller that downlaods the file itself and steams it to the user

require "open-uri"

class OrderController
  def  receipt
    @order=Order.find(....)

    render text: open(@order.receipt.url).read, content_type=>'application/pdf'
  end
end

Obviously add a route to it identifying the order and link to this new action, this will render the pdf downloaded by rails without the 'attachment' header.

knutsel
  • 1,281
  • 9
  • 22
0

Render pdf:

render pdf: "Invoice",
       orientation: 'Portrait',
       template: "Invoice/download",
       page_size:  'Letter'
Deepak Mahakale
  • 22,834
  • 10
  • 68
  • 88
0

If the PDF should be shown in a part of the page, you could use the following code on your page

<object data="<%= upload.file.url %>" width="100%" height="100%" style="min-height: 100%; height:100%; width:100%" type="<%= upload.mime_type %>">
  <p>No PDF render functionality on your device...</p>
</object>

If you just want to show the PDF as a full page file, use this in your controller (creating a pdf on the fly)

pdf = @sales_invoice.pdf
send_data pdf.render, filename: "sales_invoice.pdf",
                      type: "application/pdf",
                      disposition: "inline"
Yoko
  • 793
  • 10
  • 19
-1

use send_file method. This is the link that may help you. http://apidock.com/rails/ActionController/Streaming/send_file

Rankit Ranjan
  • 39
  • 1
  • 3
  • 1
    Please don't provide link-only answers. Links go dead quickly on the internet, and then this answer is worthless. Include the relevant parts you like to express in the answers – Prasad Khode Jul 31 '15 at 09:40