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

发布于 2024-12-06 06:43:42 字数 1539 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

没企图 2024-12-13 06:43:42

在您的控制器中:

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

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

更新

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

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

如果您想内联显示它,< a href="https://stackoverflow.com/questions/291813/best-way-to-embed-pdf-in-html">这个问题将比我更完整。

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.

Smile简单爱 2024-12-13 06:43:42

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

在“show.hmtl.erb”中,

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

只需将文件位置放入嵌入式 ruby​​ 中作为 iframe 标记的源,经过数小时的搜索后,对我来说很有效。 “certificate”是我的模型,“certificate_pdf”是我的附件文件。

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

In the '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.

熊抱啵儿 2024-12-13 06:43:42

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

在视图中,我通过以下(伪)代码作为辅助方法提供了 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

此处的相关部分是 link_to 名称,“/res/#{ res.id}/#{File.basename(file)}" 东西。

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>/. res means result, and item_id is the numeric id of that item in Rails.

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:

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.

唯憾梦倾城 2024-12-13 06:43:42

这可能太简单了,但我很难找到问题的简单答案,所以我将其发布在这里。我真的不想只是为了下载静态文件而向控制器添加另一个操作。

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

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

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.

<%= link_to "speaker registration form", "https://s3.amazonaws.com/gws_documents/speaker_registration.pdf" %>
假面具 2024-12-13 06:43:42
def pdf
  pdf_content = ...# create the pdf
  send_data(pdf_content, :filename => "test.pdf", :type => "application/pdf")
end
def pdf
  pdf_content = ...# create the pdf
  send_data(pdf_content, :filename => "test.pdf", :type => "application/pdf")
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文