2010-06-08 80 views
1

我有一个保存在云中的pdf文件附件。该文件使用attachment_fu附加。我要做的一切在视图中显示的是:在Rails应用程序中内嵌显示pdf文件

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

当网页加载完毕后,此代码在浏览器中,我想要做什么:它显示附件中的PDF文件。

但只限于Mac。

在Windows上,浏览器将显示一个损坏的图像占位符。 Chrome的开发者工具报告:“资源被解释为图像,但是通过MIME类型application/pdf进行传输。”

我还试图将来自控制器的文件:

在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 

在routes.rb中:

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

并在视图:

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

,不显示在Mac上的文件(我没有在Windows尝试),它显示的路径:

pdf_attachments/send_pdf_attachment/35 

我该怎么做才能正确显示PDF文件在线?

回答

1

我的猜测是它不会显示,因为你正试图在图像标签中显示PDF。这不是一个图像,它是一个PDF。
要么将​​其显示在iframe中,要么将其放入只显示pdf的链接中,方法是使用send_file和内联处置。

我使用princely插件创建PDF并使用此方法显示内联。

+0

嗨Khronos,感谢您的提示。 我承认,我不知道