我在互联网上四处寻找,但似乎找不到如何在rails中显示PDF(我只能找到如何创建PDF的信息).

有人知道我需要显示什么代码/Ruby 吗?

推荐答案

在控制器中:

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" # f或 apache

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

The config modification is required because it enables the web server to send the file directly from the disk, which gives a nice perf或mance boost.

Update

如果要显示而不是下载,请使用send_file:disposition选项:

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 m或e complete that I could ever be.

Ruby-on-rails相关问答推荐

在数组中定位不匹配的索引

通过激励控制器正确更新Turbo帧信源

Ubuntu Webhooks无法读取安装的正确RVM Ruby

如何在测试期间为 Rails 应用程序启用 Rails.cache 会话对象

Rails 100% 新手问题 - send() 方法

2个空格或1个制表符,Rails社区的缩进标准是什么?

使用 RSpec 和 Capybara (Rails) 测试重定向

Rails cron 与时俱进,设置环境

Rails ActiveRecord:验证单个属性

如何避免 has_many :through 关系中的重复?

如何将 yaml 文件解析为 ruby​​ 哈希和/或数组?

在 ruby​​ on rails 上构建方法

Rails 3. 构建 oauth2 提供程序

Elastic Beanstalk Ruby/Rails 需要安装 git,所以 bundle install 可以工作.. 但不是

如何编写 Capybara 断言来判断按钮的存在及其启用或禁用状态?

表单中的第一个参数不能包含 nil 或为空 - Rails 4

用户注册时设计确认令牌无效

在 Rails 协会中未找到名为的协会可能拼写错误的问题

如何在 Rails 4 中测试控制器关注点

Rails:您已经激活了 rake 10.3.1,但您的 Gemfile 需要 rake 10.2.2 (Gem::LoadError)