views:

197

answers:

2

Hi,

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

So, my question is: what do I do to properly display a pdf file inline?

Thanks martin

A: 

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?

Esse
Yes, the .content_type contains 'application/pdf' :type => pdf_attachment.content_type
Martas
A: 

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?

Nicklas Ansman
Just got to it now. No change, it behaves the same way with or without the :stream key.
Martas