Rails PDF 输出

Posted by devon on December 19th, 2007 filed in Rails应用

Rails中生成PDF有多种办法,HTMLDOC命令(可配合HTML,CSS将页面转成PDF)、PDF::Writer、Ruby FPDF。本文以Ruby FPDF为例,讲解如何在Rails输出中英文的PDF文档。

1, 安装 railsrfpdf 插件

<code>
ruby script/plugin install http://cnruby.googlecode.com/svn/trunk/rails-projects/infoq_rfpdf/vendor/plugins/railsrfpdf/
</code>

2, 在environment.rb中增加新的Mime Type:

<code>
Mime::Type.register "application/pdf", :pdf
</code>

3, 在controller中:

1
2
3
4
5
6
7
8
9
10
11
respond_to do |format|
  format.html # show.rhtml
  format.xml  { render :xml =&gt; @page.to_xml }
  format.pdf do
    pdf = FPDF.new
    pdf.AddPage
    pdf.SetFont("Arial",'B',18)
    pdf.Cell(100, 20, "Hello World")
    send_data pdf.Output, :filename =&gt; "hello.pdf", :type =&gt; "application/pdf"
  end
end

中文输出的例子(iconv这里用于将utf8,转为gb2321)

1
2
3
4
5
6
7
8
pdf = FPDF.new
pdf.extend(PDF_Chinese)
pdf.AddPage
ic = Iconv.new('GB2312', 'UTF-8')
pdf.AddGBFont("仿宋",ic.iconv("仿宋_GB2312"))
pdf.SetFont("仿宋",'B',18)
pdf.Cell(100, 10, ic.iconv("中文文字输出"))
send_data pdf.Output, :filename =&gt; "hello_cn.pdf", :type =&gt; "application/pdf"

RPDF常用方法
SetFont(string family [, string style [, float size]])
SetMargins(float left, float top [, float right])
SetTextColor(int r [, int g, int b])
SetXY(float x, float y)
AddFont(string family [, string style [, string file]])
Cell(float w [, float h [, string txt [, mixed border [, int ln [, string align [, int fill [, mixed link]]]]]]])
Image(string file, float x, float y [, float w [, float h [, string type [, mixed link]]]])
Line(float x1, float y1, float x2, float y2)
MultiCell(float w, float h, string txt [, mixed border [, string align [, int fill]]])
Rect(float x, float y, float w, float h [, string style])

一个实际的应用案例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
pdf = FPDF.new('portrait', 'pt', [800, 1380])
pdf.SetAutoPageBreak(false)
pdf.AddPage
# draw a rectangle
pdf.SetFillColor(240, 240, 240)
pdf.Rect(0, 0, 800, 895, 'F')    pdf.Image("#{RAILS_ROOT}/public/images/a.jpg", 0, 0, 800, 600)
# add four images
pdf.Image("#{RAILS_ROOT}/public/images/b.jpg", 0, 700, 260)
pdf.Image("#{RAILS_ROOT}/public/images/c.jpg", 270, 700, 260)
pdf.Image("#{RAILS_ROOT}/public/images/d.jpg", 540, 700, 260)
# write text
txt = 'Agile Web Development'
pdf.SetFont("Arial", 'B', 38)
pdf.SetXY(0, 600)
pdf.Cell(800, 100, txt, 10, 1, 'C')
pdf.SetTextColor(10, 10, 10)
txt = 'What is Ruby FPDF?'
pdf.SetFont("Arial", 'B', 30)
pdf.SetXY(20, 920)
pdf.Cell(300, 30, txt, 10, 1)
txt = "Ruby FPDF is a Ruby port of Olivier Plathey's excellent PDF generator, FPDF. FPDF is written in PHP, and as such can only be used From PHP scripts. Ruby FPDF, as the name suggests, is written in Ruby and can be used From Ruby scripts.    Ruby FPDF was ported and is maintained by Brian Ollenberger. If you need to contact me, see the contact link abovenYou can download Ruby FPDF below. It is released under a permissive license, similar to that of FPDF. I only ask that you retain the copyright notice at the top of the source file. You may make modifications to FPDF, but if you redistribute those modifications, make it clear in a comment immediately before or after the copyright notice that you have modified it. I also wouldn't mind if you sent patches back to me, but you are not strictly required to do so."
pdf.SetFont("Arial", '', 18)
pdf.SetXY(20, 960)
pdf.MultiCell(500, 20, txt)
txt = 'Ruby | On | Rails'
pdf.SetFont("Arial", '', 24)
pdf.SetXY(540, 920)
pdf.Cell(260, 30, txt, 10, 1, 'C')
#draw rectangle
pdf.SetFillColor(240, 240, 240)
pdf.Rect(550, 960, 230, 300, 'F')
#write text
txt = "* Get Excitednn* Get Startedn* Get BetternnContactnnEnjoy Railsnenjoyrails@gmail.com"
pdf.SetFont("Arial", 'B', 18)
pdf.SetXY(560, 970)
pdf.MultiCell(220, 36, txt)
pdf.SetFillColor(170, 0, 1)
pdf.Rect(0, 1310, 800, 70, 'F')
txt = 'View this page at http://enjoyrails.com/'
pdf.SetTextColor(255, 255, 255)
pdf.SetFont("Arial", '', 28)
pdf.SetXY(0, 1330)
pdf.Cell(800, 24, txt, 0, 0, 'C')
send_data pdf.Output, :filename =&gt; "hello_advance.pdf", :type =&gt; "application/pdf"


参考:
1, fpdf manual,有完整的rpdf文档,不过,部分参数不完全正确,可参考源码(vendor/plugins/railsrfpdf/lib/fpdf.rb)
2, How to Generate PDFs,介绍了各种在Rails中生成PDF的方法
3, PDF::Writer VS Ruby FPDF
4, Iconv Rdoc
5, fpdf 主页

Related posts:

  1. Rails中检测SWF尺寸 安装: cd vendor/plugins svn export http://ruby-imagespec.googlecode.com/svn/trunk/ image_spec 使用: image =...

Related posts brought to you by Yet Another Related Posts Plugin.

Leave a Comment