如何在 ROR (Ruby) 中显示 PDF?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7500451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 02:00:21  来源:igfitidea点击:

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

ruby-on-railsrubypdf

提问by Doomsknight

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).

我在互联网上环顾四周,但似乎无法找到如何在 Rails 中显示 PDF(我只能找到有关如何创建 PDF 的信息)。

Does anyone know what code/gem I need to display one?

有谁知道我需要显示什么代码/宝石?

回答by Benoit Garret

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/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.

配置修改是必需的,因为它使 Web 服务器能够直接从磁盘发送文件,这提供了很好的性能提升。

Update

更新

If you want to display it instead of downloading it, use the :dispositionoption of send_file:

如果您想显示它而不是下载它,请使用以下:disposition选项send_file

send_file(pdf_filename, :filename => "your_document.pdf", :disposition => 'inline', :type => "application/pdf")

If you want to display it inline, this questionwill be much more complete that I could ever be.

如果你想内联显示它,这个问题会比我更完整。

回答by andrewcockerham

Basically you just need to write it in the html in your view. So this simple solution worked for me:

基本上你只需要在你的视图中将它写在 html 中。所以这个简单的解决方案对我有用:

In the 'show.hmtl.erb'

在“show.hmtl.erb”中

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

just putting the file location in embedded ruby as the source of the iframe tag worked for me after hours and hours of searching. 'certificate' is my model, and 'certificate_pdf' is my attachment file.

只是将文件位置放在嵌入式 ruby​​ 中,作为 iframe 标签的来源,经过数小时的搜索后对我有用。'certificate' 是我的模型,'certificate_pdf' 是我的附件文件。

回答by mliebelt

Depending where the PDF comes from, the following may help you. I have an application where I store a lot of things, and some of them have (additional) PDFs connected to the items. I store the items in the directory /public/res/<item_id>/. resmeans result, and item_idis the numeric id of that item in Rails.

根据 PDF 的来源,以下内容可能对您有所帮助。我有一个应用程序,我可以在其中存储很多东西,其中一些有(附加的)PDF 连接到这些项目。我将项目存储在目录中/public/res/<item_id>/res表示结果,item_id是 Rails 中该项目的数字 ID。

In the view, I provide a link to the PDFs by the following (pseudo-)code as a helper method, that may be used in the view:

在视图中,我通过以下(伪)代码提供了指向 PDF 的链接作为辅助方法,可以在视图中使用:

def file_link(key, name = nil)
  res= Ressource.find(:first, :conditions => ["key = ?", key])
  list = Dir["public/res/#{res.id}/*"]
  file= list.empty? ? "" : list[0]
  return file if file.empty?
  fn = name ? name : File.basename(file)
  link_to fn, "/res/#{res.id}/#{File.basename(file)}", :popup => true
end

The relevant part here is the link_to name, "/res/#{res.id}/#{File.basename(file)}"thing.

这里的相关部分是link_to name, "/res/#{res.id}/#{File.basename(file)}"事物。

回答by Makean

This may be too simple, but I had trouble finding a simple answer to my problem, so I'm posting it here. I really didn't want to add another action to my controller just to download a static file.

这可能太简单了,但我很难找到问题的简单答案,所以我把它贴在这里。我真的不想仅仅为了下载一个静态文件而向我的控制器添加另一个动作。

I just uploaded my file to S3 & used link_to referring to the path, which S3 provides after you upload the file and set the permissions (remember, everyone has to be able to upload and download it). I store lots of data for the app on S3 so it seemed like a fine choice.

我刚刚将我的文件上传到 S3 并使用了 link_to 指的是在您上传文件并设置权限后 S3 提供的路径(请记住,每个人都必须能够上传和下载它)。我在 S3 上为该应用程序存储了大量数据,所以这似乎是一个不错的选择。

<%= link_to "speaker registration form", "https://s3.amazonaws.com/gws_documents/speaker_registration.pdf" %>

回答by Matt

def pdf
  pdf_content = ...# create the pdf
  send_data(pdf_content, :filename => "test.pdf", :type => "application/pdf")
end