Building inline rendering of a PDF document in Rails

I am writing a service that creates PDF files from a set of XML files. PDF is created correctly. However, every time I click on the link "view PDF", the browser asks the user to download the PDF file.

I need a PDF to display inline like any regular HTML page. I, although I wrote the code correctly, but something should be missing - the browser continues to request 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?

+4
source share
1 answer

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

Another option is to use iframe and install src from iframe into your PDF file. Almost all browsers that support integrated PDF viewing will do this correctly. The downside is that you can end up displaying an empty iframe , while unsupported browsers would otherwise make a graceful disconnect to a simple PDF download.

+3
source

Source: https://habr.com/ru/post/1336220/


All Articles