6

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?

Fábio Batista
  • 25,002
  • 3
  • 56
  • 68
  • This will depend on the browser. Are you sure the browser you're testing in is capable of displaying PDFs inline? – Brian Donovan Jan 19 '11 at 22:26
  • Yes, I am... Actually, the piece of code I'm writing (in Ruby) is replacing an older, buggy Java implementation. The URL for the "PDF Generation Service" in the application I'm working on is configurable, so I just replaced the old implementation with mine. The old displays inline, and mine asks for the download. I'll check the response headers on both now. – Fábio Batista Jan 19 '11 at 22:29

1 Answers1

5

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.

Bob Aman
  • 32,839
  • 9
  • 71
  • 95