3

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

Ranzit
  • 1,327
  • 2
  • 13
  • 32

6 Answers6

5

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.

rony36
  • 3,277
  • 1
  • 30
  • 42
5

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.

andrewcockerham
  • 2,676
  • 3
  • 23
  • 19
3

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?

Community
  • 1
  • 1
user2316072
  • 111
  • 2
2

I used the following code to make mine work in the view (with the example object @person) using the most current Google Docs embedded pdf code specifications:

<iframe src="https://docs.google.com/viewer?url=http://www.mysite.com/<%= @person.pdf.url %>&embedded=true" 
width="100%" height="800" >
<p>Your browser does not support iframes.</p>
</iframe>
CodeBiker
  • 2,985
  • 2
  • 33
  • 36
1

Here is an example PDF iframe:

http://googlesystem.blogspot.com/2009/09/embeddable-google-document-viewer.html

Simply change xyz.pdf to the desired filename:

<% url = "#{request.protocol}#{request.host_with_port}#{asset_path("xyz.pdf")}" %>

<iframe src="http://docs.google.com/gview?url=<%= url %>&embedded=true" style="width:1170px; height:800px;" frameborder="0"></iframe>

You may also need to add you asset path if you haven't already (in application.rb):

config.assets.paths << "#{Rails.root}/app/assets/pdf"
Abram
  • 39,950
  • 26
  • 134
  • 184
1

This worked for me:

<iframe src="http://docs.google.com/viewer?url=<%= u @model.modeldoc %>&embedded=true" width="600" height="780" style="border: none;"></iframe>

The "u" is a ruby method that turns that makes the url URL-encoded (which google doc viewer requires). This was the issue I was having with CodeBiker's answer.

more info on "u" -- http://rdoc.info/stdlib/erb/1.8.7/ERB/Util%3aurl_encode

HLSdropout
  • 76
  • 2