2013-04-25 108 views
3

我有模塊,它具有上傳PDF文件的附件功能。 我需要在瀏覽器/ iframe中的應用程序中顯示存儲的pdf,而不需要下載它。有沒有一個寶石或機會,會顯示/閱讀軌道的PDF文件。在iframe中的瀏覽器中顯示附加的pdf - rails

注:PDF文件將包含圖片,文字內容等

在此先感謝

蘭吉特

+1

試試這個瀏覽器See pdf file打開並可以讀取/顯示的PDF文件。 – visnu 2013-04-25 07:27:38

回答

5

是的,有一個寶石被命名爲蝦在github上。 Check它出來。

但是有一種更靈活的方式可以在Google文檔中實現此功能。您可以使用Google文檔查看器嵌入您的PDF。

簡單:

<a href="https://docs.google.com/gview?url={YOUR_PDF_URL}&embedded=true">Your Pdf Viewer</a> 

感謝。

+0

謝謝,它幫助了我。 – Ranzit 2013-05-06 04:19:25

+0

不客氣。 :) – rony36 2013-05-06 06:07:47

3

在你的控制器定義像下面這樣的函數:

def show_pdf 
    pdf_filename = File.join(Rails.root, "tmp/my_pdf_doc.pdf") 
    send_file(pdf_filename, :filename => "your_document.pdf", :disposition => 'inline', :type => "application/pdf") 
end 

在您的環境配置(developemnt.rb/production.rb)文件:

config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache 
config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx 

配置修改使Web服務器能夠直接從磁盤發送文件。

你可以看看這個問題Recommended way to embed PDF in HTML?

+0

謝謝,這對我很有幫助。 – Ranzit 2013-05-06 04:18:53

2

我用下面的代碼中使用的最當前Google Docs embedded pdf code specifications使在視圖礦山工作(與示例性對象@person):

<iframe src="https://docs.google.com/viewer?url=http://www.mysite.com/<%= @person.pdf.url %>&embedded=true" 
width="100%" height="800" > 
<p>Your browser does not support iframes.</p> 
</iframe> 
1

下面是一個例子PDF的iframe:

http://googlesystem.blogspot.com/2009/09/embeddable-google-document-viewer.html

只需將xyz.pdf更改爲所需的文件名:

<% url = "#{request.protocol}#{request.host_with_port}#{asset_path("xyz.pdf")}" %> 

<iframe src="http://docs.google.com/gview?url=<%= url %>&embedded=true" style="width:1170px; height:800px;" frameborder="0"></iframe> 

您可能還需要如果您還沒有(在application.rb中)來添加您的資源路徑:

config.assets.paths << "#{Rails.root}/app/assets/pdf" 
3

這爲我工作:

在「秀.hmtl.erb'

<iframe src=<%= @certificate.certificate_pdf %> width="600" height="780" style="border: none;"> </iframe> 

只是把文件的嵌入式紅寶石作爲源工作數小時後和我小時的搜索。 'certificate'是我的模型,'certificate_pdf'是我的附件。

1

這爲我工作:

<iframe src="http://docs.google.com/viewer?url=<%= u @model.modeldoc %>&embedded=true" width="600" height="780" style="border: none;"></iframe> 

的「U」是打開,使網址URL編碼(其中谷歌文檔查看器需要)一個紅寶石方法。這是CodeBiker回答時遇到的問題。在「U」

更多信息 - http://rdoc.info/stdlib/erb/1.8.7/ERB/Util%3aurl_encode

相關問題