问题描述
我在互联网上四处张望,但似乎无法找到如何在Rails中显示PDF的方法(我只能找到有关如何创建PDF的信息).
I have looked around on the internet, but do not seem able to find how to display a PDF in rails (I can only find info on how to create one).
有人知道我需要显示什么代码/宝石吗?
Does anyone know what code/gem I need to display one?
推荐答案
在您的控制器中:
def pdf
pdf_filename = File.join(Rails.root, "tmp/my_document.pdf")
send_file(pdf_filename, :filename => "your_document.pdf", :type => "application/pdf")
end
在config/environment/production.rb
中:
config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
或
config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx
需要进行config修改,因为它使Web服务器可以直接从磁盘发送文件,从而提高了性能.
The config modification is required because it enables the web server to send the file directly from the disk, which gives a nice performance boost.
更新
如果要显示而不是下载它,请使用send_file
的:disposition
选项:
If you want to display it instead of downloading it, use the :disposition
option of send_file
:
send_file(pdf_filename, :filename => "your_document.pdf", :disposition => 'inline', :type => "application/pdf")
如果要内嵌显示,则此问题将比以前更完整.
If you want to display it inline, this question will be much more complete that I could ever be.
这篇关于如何在ROR(Ruby)中显示PDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!