Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forcing the inline rendering of a PDF document in Rails

I'm writing a service that generates PDF files from a set of XML files. The PDF is being correctly generated. However, everytime I click on the "view PDF" link, the browser asks the user to download the PDF file.

I need the PDF to display inline, just like any regular HTML page. I though I wrote the code right, but something must be missing - the browser keeps asking the user to download.

Here's the current code:

class PdfController < Controller
  def generate
    # stuff
    send_data pdf_bytes, :disposition => 'inline', :type => 'application/pdf'
  end
end

Any ideas?

like image 313
Fábio Batista Avatar asked Oct 14 '22 17:10

Fábio Batista


1 Answers

Try removing the Content-Disposition header altogether. It's been my experience that Content-Disposition: attachment works pretty well, but many browsers have inconsistent behavior for any other value. If you want to display inline, it might just be better to remove the header and hope for the best. IE seems to have the most problems with this header. (Surprise, surprise.) Just make sure you're still setting Content-Type: application/pdf.

The other option would be to use an iframe and set the src of the iframe to your PDF file. Almost all browsers that support inline PDF viewing will handle this correctly. The downside is that you might end up displaying a blank iframe whereas non-supported browsers would have otherwise done a graceful fallback to simply downloading the PDF.

like image 94
Bob Aman Avatar answered Oct 31 '22 11:10

Bob Aman