Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
98 views
in Technique[技术] by (71.8m points)

How do I display a PDF in ROR (Ruby)?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In your controller:

def pdf
  pdf_filename = File.join(Rails.root, "tmp/my_document.pdf")
  send_file(pdf_filename, :filename => "your_document.pdf", :type => "application/pdf")
end

In config/environment/production.rb:

config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache

or

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

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.

Update

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...