2010-06-08 85 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,感謝您的提示。 我承認,我不知道