Display pdf file inline in Rails app

Posted by Martas on Stack Overflow See other posts from Stack Overflow or by Martas
Published on 2010-06-08T19:09:19Z Indexed on 2010/06/08 19:12 UTC
Read the original article Hit count: 299

Filed under:
|
|

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 PdfAttachmentController:

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

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about pdf