我有模塊,它具有上傳PDF文件的附件功能。 我需要在瀏覽器/ iframe中的應用程序中顯示存儲的pdf,而不需要下載它。有沒有一個寶石或機會,會顯示/閱讀軌道的PDF文件。在iframe中的瀏覽器中顯示附加的pdf - rails
注:PDF文件將包含圖片,文字內容等
在此先感謝
蘭吉特
我有模塊,它具有上傳PDF文件的附件功能。 我需要在瀏覽器/ iframe中的應用程序中顯示存儲的pdf,而不需要下載它。有沒有一個寶石或機會,會顯示/閱讀軌道的PDF文件。在iframe中的瀏覽器中顯示附加的pdf - rails
注:PDF文件將包含圖片,文字內容等
在此先感謝
蘭吉特
在你的控制器定義像下面這樣的函數:
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服務器能夠直接從磁盤發送文件。
謝謝,這對我很有幫助。 – Ranzit 2013-05-06 04:18:53
我用下面的代碼中使用的最當前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>
下面是一個例子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"
這爲我工作:
在「秀.hmtl.erb'
<iframe src=<%= @certificate.certificate_pdf %> width="600" height="780" style="border: none;"> </iframe>
只是把文件的嵌入式紅寶石作爲源工作數小時後和我小時的搜索。 'certificate'是我的模型,'certificate_pdf'是我的附件。
這爲我工作:
<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
試試這個瀏覽器See pdf file打開並可以讀取/顯示的PDF文件。 – visnu 2013-04-25 07:27:38