简体   繁体   中英

Display pdf file inline in Rails app

I have a pdf file attachment saved in the cloud. The file is attached using attachment_fu. All I do to display it in the view is:

<%= image_tag @model.pdf_attachment.public_filename %>

When I load the page with this code in the browser, it does what I want: it displays the attached pdf file.

But only on Mac.

On Windows, browsers will display a broken image placeholder. Chrome's Developer Tools report: "Resource interpreted as image but transferred with MIME type application/pdf."

I also tried sending the file from controller:

in PdfAttachmentsController:

def send_pdf_attachment
  pdf_attachment = PdfAttachment.find params[:id]
  send_file pdf_attachment.public_filename,
    :type => pdf_attachment.content_type,
    :file_name => pdf_attachment.filename,
    :disposition => 'inline'
end

in routes.rb:

map.send_pdf_attachment '/pdf_attachments/send_pdf_attachment/:id', 
  :controller => 'pdf_attachments', 
  :action => 'send_pdf_attachment'

and in the view:

<%= send_pdf_attachment_path @model.pdf_attachment %>
or
<%= image_tag( send_pdf_attachment_path @model.pdf_attachment ) %>

And that doesn't display the file on Mac (I didn't try on Windows), it displays the path:

pdf_attachments/send_pdf_attachment/35

What do I do to properly display a pdf file inline?

Did you try just using a simple <iframe> tag?

This worked for me:

In the view (show.hmtl.erb)

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

just calling the file with embedded ruby as the source of the 'iframe' tag worked for me after hours and hours of searching. 'certificate' is my model, and 'certificate_pdf' is my attachment file. I tested it and works on Mac (Safari, FF, Chrome) and Windows (IE10, FF, Chrome)

You can't send files that aren't on your local machine. If you want to send a file to the web client whose contents exist on another server, you'll first need to get the file's contents, then stream that file once it has been downloaded to your local file.

ie... issue an HTTP get request to the (remote) server, then save the contents of that in a file object, and then send_file THAT.

Have you tried disabling streaming of the file?

send_file pdf_attachment.public_filename,
  :type => pdf_attachment.content_type,
  :file_name => pdf_attachment.filename,
  :disposition => 'inline',
  :stream => false

Also make sure the file isn't broken, if you change the disposition to attachment and download it can it still be opened, if you make an md5-hash of it is it the same as the original?

My guess would be that it's not displaying because you're trying to display a pdf in an image tag. It's not an image it's a pdf.
Either display it in an iframe or put it in a link that displays just the pdf using send_file and a disposition of inline.

I create pdfs with the princely plugin and they display inline using this method.

have you tried setting type explicitly by writing: :type => 'application/pdf' ? when file is served from the cloud, it's served by other then yours webserver - maybe they sending it with Mime Type "application/octet-stream", not as pdf?

Browsers have evolved since this was last answered. Easiest method is to use the <embed> tag:

<embed type="application/pdf" src="<%= image_path( send_pdf_attachment_path @model.pdf_attachment )%>">

There are other options available such as size, zoom level etc. This is an excellent source of information on it:

https://www.codexworld.com/embed-pdf-document-file-in-html-web-page/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM