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?
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.