I need to be able to render some views as PDFs from a Rails 3 project. I've never before used PDF generation techniques with ruby/rails, so I researched a few popular approaches such as Prawn and PDF::Writer, but all the examples and articles I found so far seem outdated and only applicable for rails 2.x. I haven't yet seen a working Rails3 example; tried myself installing prawn and the prawnto gems and reproducing the example described in this Railscasts episode, but I'm getting error of prawnto method not being recognized. I'm uncertain of whether this was an implementation error or just a sign of incompatibility, but seeing other people share on the web that prawn is no longer working for them in Rails3 I didn't bother tracing the code further.
我需要能够从Rails 3项目中将一些视图呈现为PDF。我之前从未使用过ruby / rails的PDF生成技术,因此我研究了一些流行的方法,如Prawn和PDF :: Writer,但到目前为止我发现的所有示例和文章都显得过时,仅适用于rails 2.x 。我还没有看到一个有效的Rails3示例;我尝试自己安装虾和prawnto宝石并重现这个Railscasts剧集中描述的例子,但是我得到了prawnto方法无法识别的错误。我不确定这是一个实现错误还是只是一个不兼容的迹象,但是看到其他人在网上分享大虾不再在Rails3中为他们工作我没有费心进一步追踪代码。
Has anyone found a working reliable solution for pdf generation in Rails3? Could you possibly share it or point me to external resources and documentation? Big thanks!
有没有人在Rails3中找到一个可靠的pdf生成解决方案?您可以分享它或指向外部资源和文档吗?十分感谢!
5 个解决方案
#1
11
New answer to an old question, in case others stumble across this: WickedPDF (which uses wkhtmltopdf just like PDFkit) makes this a snap.
一个旧问题的新答案,以防其他人偶然发现:WickedPDF(使用像PDFkit一样使用wkhtmltopdf)使这一点变得轻而易举。
https://github.com/mileszs/wicked_pdf
https://github.com/mileszs/wicked_pdf
#2
11
Prawn does work with Rails 3. I have personally used it with no problems. You do have to get the latest versions of the gem and the prawnto plugin for rails.
Prawn确实可以使用Rails 3.我亲自使用它没有任何问题。你必须获得gem的最新版本和rails的prawnto插件。
PDFkit does have the advantage of using the Webkit rendering engine, so you get to use CSS to define your layouts, and you get matching web pages for free with Safari and Chrome. It has a slightly nicer learning curve than Prawn.
PDFkit确实具有使用Webkit渲染引擎的优势,因此您可以使用CSS来定义布局,并且您可以通过Safari和Chrome免费获得匹配的网页。它的学习曲线比Prawn略好。
#3
7
Have you seen PDFkit? I'm pretty sure that works with Rails 3, it is a piece of Rack middleware that can convert any HTML page to PDF that matches a route ending in .pdf
你见过PDFkit吗?我很确定它适用于Rails 3,它是一个Rack中间件,可以将任何HTML页面转换为PDF格式,以匹配以.pdf结尾的路径
#4
2
About prawn, here is a seamless integration for Rails 3 that seems to work just fine: https://github.com/Whoops/prawn-rails
关于大虾,这里是Rails 3的无缝集成,似乎工作得很好:https://github.com/Whoops/prawn-rails
#5
1
You can use the Report gem, which generates PDF but also XLSX and CSV.
您可以使用Report gem,它可以生成PDF,还可以生成XLSX和CSV。
# a fake Manufacturer class - you probably have an ActiveRecord model
Manufacturer = Struct.new(:name, :gsa)
require 'report'
class ManufacturerReport < Report
table 'Manufacturers' do # you can have multiple tables, which translate into multiple sheets in XLSX
head do
row 'Manufacturer report'
end
body do
rows :manufacturers
column 'Name', :name
column 'GSA?', :gsa
end
end
# you would want this so that you can pass in an array
# attr_reader :manufacturers
# def initialize(manufacturers)
# @manufacturers = manufacturers
# end
def manufacturers
[
Manufacturer.new('Ford', true),
Manufacturer.new('Fischer', false),
Manufacturer.new('Tesla', nil),
]
end
end
When you call report.pdf.path
, a PDF is generating in the tmp directory:
当您调用report.pdf.path时,将在tmp目录中生成PDF:
report = ManufacturerReport.new
puts report.pdf.path #=> /tmp/185051406_Report__Pdf.pdf
puts report.xlsx.path #=> /tmp/185050541_Report__Xlsx.xlsx
You can do it in your controller like:
您可以在控制器中执行以下操作:
@manufacturers = Manufacturer.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @manufacturers }
format.pdf do
report = ManufacturerReport.new(@manufacturers) # using the commented-out code
send_file report.pdf.path, :type => 'application/pdf', :disposition => 'attachment', :filename => 'ManufacturersReport.pdf'
# tmp files are periodically cleaned up by the operating system, but if you want to be extra clean you can call
# report.cleanup
# but this may remove the tmp files before apache/nginx/etc. finishes delivering the file
end
end
End result:
最终结果:
XLSX
XLSX
Note that the XLSX has an autofilter added for you automatically.
请注意,XLSX会自动为您添加自动过滤器。
#1
11
New answer to an old question, in case others stumble across this: WickedPDF (which uses wkhtmltopdf just like PDFkit) makes this a snap.
一个旧问题的新答案,以防其他人偶然发现:WickedPDF(使用像PDFkit一样使用wkhtmltopdf)使这一点变得轻而易举。
https://github.com/mileszs/wicked_pdf
https://github.com/mileszs/wicked_pdf
#2
11
Prawn does work with Rails 3. I have personally used it with no problems. You do have to get the latest versions of the gem and the prawnto plugin for rails.
Prawn确实可以使用Rails 3.我亲自使用它没有任何问题。你必须获得gem的最新版本和rails的prawnto插件。
PDFkit does have the advantage of using the Webkit rendering engine, so you get to use CSS to define your layouts, and you get matching web pages for free with Safari and Chrome. It has a slightly nicer learning curve than Prawn.
PDFkit确实具有使用Webkit渲染引擎的优势,因此您可以使用CSS来定义布局,并且您可以通过Safari和Chrome免费获得匹配的网页。它的学习曲线比Prawn略好。
#3
7
Have you seen PDFkit? I'm pretty sure that works with Rails 3, it is a piece of Rack middleware that can convert any HTML page to PDF that matches a route ending in .pdf
你见过PDFkit吗?我很确定它适用于Rails 3,它是一个Rack中间件,可以将任何HTML页面转换为PDF格式,以匹配以.pdf结尾的路径
#4
2
About prawn, here is a seamless integration for Rails 3 that seems to work just fine: https://github.com/Whoops/prawn-rails
关于大虾,这里是Rails 3的无缝集成,似乎工作得很好:https://github.com/Whoops/prawn-rails
#5
1
You can use the Report gem, which generates PDF but also XLSX and CSV.
您可以使用Report gem,它可以生成PDF,还可以生成XLSX和CSV。
# a fake Manufacturer class - you probably have an ActiveRecord model
Manufacturer = Struct.new(:name, :gsa)
require 'report'
class ManufacturerReport < Report
table 'Manufacturers' do # you can have multiple tables, which translate into multiple sheets in XLSX
head do
row 'Manufacturer report'
end
body do
rows :manufacturers
column 'Name', :name
column 'GSA?', :gsa
end
end
# you would want this so that you can pass in an array
# attr_reader :manufacturers
# def initialize(manufacturers)
# @manufacturers = manufacturers
# end
def manufacturers
[
Manufacturer.new('Ford', true),
Manufacturer.new('Fischer', false),
Manufacturer.new('Tesla', nil),
]
end
end
When you call report.pdf.path
, a PDF is generating in the tmp directory:
当您调用report.pdf.path时,将在tmp目录中生成PDF:
report = ManufacturerReport.new
puts report.pdf.path #=> /tmp/185051406_Report__Pdf.pdf
puts report.xlsx.path #=> /tmp/185050541_Report__Xlsx.xlsx
You can do it in your controller like:
您可以在控制器中执行以下操作:
@manufacturers = Manufacturer.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @manufacturers }
format.pdf do
report = ManufacturerReport.new(@manufacturers) # using the commented-out code
send_file report.pdf.path, :type => 'application/pdf', :disposition => 'attachment', :filename => 'ManufacturersReport.pdf'
# tmp files are periodically cleaned up by the operating system, but if you want to be extra clean you can call
# report.cleanup
# but this may remove the tmp files before apache/nginx/etc. finishes delivering the file
end
end
End result:
最终结果:
XLSX
XLSX
Note that the XLSX has an autofilter added for you automatically.
请注意,XLSX会自动为您添加自动过滤器。