Rails、Prawn - PDF 显示在浏览器中ETC

发布于 2024-12-03 01:45:21 字数 708 浏览 6 评论 0原文

我正在尝试理解 Prawn pdf gem。

我能够让它生成一个pdf文件。 gemfile 中的每个 gem 都包含:

gem 'mysql', '~> 2.8.1'
gem 'prawn', '~> 0.12.0'
gem 'pdf-reader', '~> 0.10.0'
gem 'Ascii85', '~> 1.0.1'

在 config/application.rb 中:

config.autoload_paths << "#{Rails.root}/app/reports"

然后在控制器中:

 require 'prawn'

 def index
  pdf = Prawn::Document.new
  pdf.text "Hello World"
  pdf.render_file "x.pdf"
 end

然后我调用索引函数。在我的应用程序的根目录中创建了一个名为 x.pdf 的 PDF。其中包括 gemfile、rakefile 和 config.ru。

问:

  1. 如何强制虾在app/report(或任何其他选定的)文件夹中生成文件?
  2. 如何在浏览器窗口中执行生成文件的操作而不保存它?
  3. 如何使其保存并显示在浏览器窗口中?

I'm trying to understand the Prawn pdf gem.

I was able to make it generate a pdf. Every gem in the gemfile included:

gem 'mysql', '~> 2.8.1'
gem 'prawn', '~> 0.12.0'
gem 'pdf-reader', '~> 0.10.0'
gem 'Ascii85', '~> 1.0.1'

In the config/application.rb:

config.autoload_paths << "#{Rails.root}/app/reports"

Then in the controller:

 require 'prawn'

 def index
  pdf = Prawn::Document.new
  pdf.text "Hello World"
  pdf.render_file "x.pdf"
 end

Than I call the index function. A PDF named x.pdf is created in the root of my application. Amongst the gemfile, rakefile and config.ru.

Question:

  1. How can I force prawn to generate the file in the app/report (or any other selected) folder?
  2. How can I make the action to generate the file in the browser window and don't save it?
  3. How can I make it to save and show up in the browser window?

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

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

发布评论

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

评论(3

尹雨沫 2024-12-10 01:45:21

如何强制虾在app/report(或任何其他选定的)文件夹中生成文件?

def index
  pdf = Prawn::Document.new
  pdf.text "Hello World"
  pdf.render_file File.join(Rails.root, "app/report", "x.pdf")
end

如何在浏览器窗口中执行生成文件的操作而不保存它?

def index
  pdf = Prawn::Document.new
  pdf.text "Hello World"
  send_data pdf.render, :filename => "x.pdf", :type => "application/pdf"
end

如何使其保存并显示在浏览器窗口中?

def index
  pdf = Prawn::Document.new
  pdf.text "Hello World"
  filename = File.join(Rails.root, "app/report", "x.pdf")
  pdf.render_file filename
  send_file filename, :filename => "x.pdf", :type => "application/pdf"
end

How can I force prawn to generate the file in the app/report (or any other selected) folder?

def index
  pdf = Prawn::Document.new
  pdf.text "Hello World"
  pdf.render_file File.join(Rails.root, "app/report", "x.pdf")
end

How can I make the action to generate the file in the browser window and don't save it?

def index
  pdf = Prawn::Document.new
  pdf.text "Hello World"
  send_data pdf.render, :filename => "x.pdf", :type => "application/pdf"
end

How can I make it to save and show up in the browser window?

def index
  pdf = Prawn::Document.new
  pdf.text "Hello World"
  filename = File.join(Rails.root, "app/report", "x.pdf")
  pdf.render_file filename
  send_file filename, :filename => "x.pdf", :type => "application/pdf"
end
深海夜未眠 2024-12-10 01:45:21

回答问题3:“如何才能保存并显示在浏览器窗口中?”

def index
  pdf = Prawn::Document.new
  pdf.text 'Hello World'
  send_data pdf.render, filename: 'x.pdf', type: 'application/pdf', disposition: 'inline'
end

disposition: 'inline' 将强制浏览器(如果可以的话)在当前浏览器窗口内显示您的 PDF

Answering Question 3: "How can I make it to save and show up in the browser window?"

def index
  pdf = Prawn::Document.new
  pdf.text 'Hello World'
  send_data pdf.render, filename: 'x.pdf', type: 'application/pdf', disposition: 'inline'
end

disposition: 'inline' will force the browser ( if it can ) to display your PDF inside the current browser window

微凉 2024-12-10 01:45:21

试试这个:

 def index
  pdf = Prawn::Document.new
  pdf.text "Hello World"
  send_data pdf.render, :filename => "x.pdf", :type => "application/pdf"
 end

也就是说,除了一个简单的 PDF 之外,您可能希望在控制器外部的某个地方生成它。

Try this:

 def index
  pdf = Prawn::Document.new
  pdf.text "Hello World"
  send_data pdf.render, :filename => "x.pdf", :type => "application/pdf"
 end

That said, for anything but a trivial PDF you will probably want to generate it outside the controller somewhere.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文