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?
Try removing the
Content-Dispositionheader altogether. It’s been my experience thatContent-Disposition: attachmentworks 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 settingContent-Type: application/pdf.The other option would be to use an
iframeand set thesrcof theiframeto 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 blankiframewhereas non-supported browsers would have otherwise done a graceful fallback to simply downloading the PDF.