Rails, Prawn - PDF 显示在浏览器中等等 [英] Rails, Prawn - PDF show up in browser & etc
问题描述
我正在尝试理解 Prawn pdf gem.
I'm trying to understand the Prawn pdf gem.
我能够让它生成一个pdf.gemfile 中的每个 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'
在 config/application.rb 中:
In the 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 中.
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.
问题:
- 如何强制 prawn 在 app/report(或任何其他选定的)文件夹中生成文件?
- 如何在浏览器窗口中执行生成文件而不保存的操作?
- 如何使其保存并显示在浏览器窗口中?
推荐答案
如何强制大虾在 app/report(或任何其他选定的)文件夹中生成文件?
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
这篇关于Rails, Prawn - PDF 显示在浏览器中等等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!