Ruby Ruport实践—中文PDF报表之PRAWN
FPDF能支持PDF的中文打印,然而却存在不少的不如意之处,据我目前的使用,总结出几点:
1)FPDF提供的Pdf_Chinese字体,将GB这种字体写入了代码中,若我们希望引入其他的中文字体,则不能方便地使用
2)FPDF输出的数字并不美观,不如Ruport::Formatter::PDF自带的字体
3)FPDF提供的方法并不多,对于一般的报表打印,用的最多的也许是 Cell方法,但如果连一个普通的报表打印都需要能过代码控制换行、画直线、写单元格,那就太不方便了
Prawn也是呼声叫高的一种Ruby PDF报表工具,对于它和 FPDF 到底谁更优秀,这就要因人而异了,FPDF是用PHP编写的PDF库的移植,而PRAWN则不是 Prawn:使用Ruby生成PDF更简捷 中有简单的介绍
下面来看看PRAWN是如何实现PDF的打印的(基本的实现参考 Ruby Ruport实践—简单报表系统)
一、安装Prawn
gem install prawn
二、修改enviroment.rb
添加如下引用:
require "prawn"
require 'prawn/layout'
三、引入合适的中文字体
Prawn支持外部引入中文字体,例如“楷体”
从C:\Windows\Fonts下将本机支持的字体文件(*.ttf)复制到
%RUBY_HOME%\lib\ruby\gems\1.8\gems\prawn-core-0.8.4\data\fonts
在代码中通过
@pdf_writer.font"#{::Prawn::BASEDIR}/data/fonts/simkai.ttf"
指定需要引用的字体
四、修改ReportOutputController.rb
替换Pdf<Ruport::Formatter::PDF为如下代码:
class Ruport::Formatter::PRAWN< Ruport::Formatter::PDF
def pdf_writer
unless @pdf_writer
@pdf_writer = ::Prawn::Document.new
@pdf_writer.font"#{::Prawn::BASEDIR}/data/fonts/simkai.ttf"
end
return @pdf_writer
end
def render_pdf
output<<pdf_writer.render
end
end
class Pdf<Ruport::Formatter::PRAWN
renders :pdf, :for => ReportOutputController
build :data_sheet do
eval(options[:outputContent])
end
end
五、保存repoert_templates
对于template_content的内容保存为如下:
pdf_writer.text ("产品列表",
:align=>:center,:size=>15)
pdf_writer.image ("public/images/ruport.jpg",
:at => [0,730],
:height=>50,
:width=>100)
pheader=%w[类别 名称 价格]
pdata=[]
for i in 0..data.length-1 do
temp=[data[
相关文档:
ruby常规访问access数据库的方法应该是使用DBI库
:
require 'dbi'
DBI.connect("DBI:ADO:Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db.mdb;")
可是
简单尝试之后没能成功,提示找不到驱动器ADO,懒得再试,遂找其他方法。
一番搜索之后,发现可以用WIN32OLE来访问access,写一个简单的类包装之:
......
Ruby 类的继承
关键字: Ruby 类的继承
一、普通方式的继承
Ruby只支持单继承
ruby 代码
class
Child < Father
......
end
Object是所有类的始祖,并且Object的实例方法 ......
http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/
Understanding Ruby Blocks, Procs and Lambdas
Blocks, Procs and lambdas (referred to as closures
in Computer Science) are one of the most powerful aspects of Ruby, and
also one of the most misunderstood. This ......
Prepare
1. Download 'OCI 1.x.x.rb' (it's VERY important - execution MUST be *.rb)
2. Download 'oci8lib.so'
Install
1. Copy 'OCI8.rb' to .../ruby/lib/ruby/site_ruby/1.8/DBD/OCI8
2. Copy 'oci8.rb' to .../ruby/lib/ruby/site_ruby/1.8
3. Copy 'oci8lib.so' to .../ruby/lib/ruby/site_ruby/1.8/i386-msv ......