Display attached pdf in browser within iframe

I have module which had a attachments functionality which uploads pdf files. I need to display stored pdf in the app within the browser/iframe and without downloading it. Is there a gem or chance which will show/read pdfs in rails.

Note : The pdfs will contain images,text content etc.

Thanks in advance

Ranjit


yes, there is a gem is named Prawn in github. Check it out.

But there is more flexible way to implement this feature with Google Docs. You can embed your pdf with google docs viewer.

Simply:

<a href="https://docs.google.com/gview?url={YOUR_PDF_URL}&embedded=true">Your Pdf Viewer</a> 

Thanks.


In your controller define a function like the following:

def show_pdf
  pdf_filename = File.join(Rails.root, "tmp/my_pdf_doc.pdf")
  send_file(pdf_filename, :filename => "your_document.pdf", :disposition => 'inline', :type => "application/pdf")
end

In your environment config (developemnt.rb/production.rb)file :

config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx

The config modification enables the web server to send the file directly from the disk.

You can have a look at this question Recommended way to embed PDF in HTML?


This worked for me:

In the 'show.hmtl.erb'

<iframe src=<%= @certificate.certificate_pdf %> width="600" height="780" style="border: none;"> </iframe>

just put the embedded ruby of the file as the source worked for me after hours and hours of searching. 'certificate' is my model, and 'certificate_pdf' is my attachment file.

链接地址: http://www.djcxy.com/p/36468.html

上一篇: 如何在aspx页面显示pdf?

下一篇: 在iframe中的浏览器中显示附加的pdf