Ruby Ruport实践—中文PDF报表之FPDF
Ruport中pdf_writer对中文的支持并不好,输出的中文显示的是乱码。上网查了很多资料,也没有找到好的解决方案,无奈只好查看源代码,到底为什么Ruport自带的PDF工具不支持中文输出。
Ruport::Formatter::PDF::Writer中找到以下代码
metrics = load_font_metrics(font)
metrics = PDF::Writer::FontMetrics.open(font)
PDF::Writer::FontMetrics::METRICS_PATH = [ File.join(File.dirname(File.expand_path(__FILE__)), 'fonts') ]
原来METRICS_PATH=%RUBY_HOME%\lib\ruby\gems\1.8\gems\pdf-writer-1.1.8\lib\pdf\writer\fonts
在此路径下存在的afm字体文件均不支持中文,于是开始将目标转向添加一个支持中文的 *.afm字体文件。无奈文件找到了,放到该路径下,运行竟报错了(也许是PDF_WRITER不支持该文件)
放弃了此方法,把目标转向FPDF
1)下载fpdf http://download.csdn.net/source/5608
2)解压到Rails项目的lib下
3)按Ruby Roport实践—简单报表系统完成Ruport应用
4)接下来,修改ReportOutputController.rb
将 class Ruport::Formatter::PDF修改为如下内容
class Ruport::Formatter::FPDF < Ruport::Formatter::PDF
def pdf_writer
unless @pdf_writer
@pdf_writer = ::FPDF.new
@pdf_writer.extend(PDF_Chinese)
@pdf_writer.AddPage
@pdf_writer.AddGBFont
@pdf_writer.SetFont('GB','', options[:font_size] || 16)
end
return @pdf_writer
end
def render_pdf
output << pdf_writer.Output
end
end
class Pdf < Ruport::Formatter::FPDF
renders :pdf, :for => BaseSqlController
build :data_sheet do
eval(options[:pdfContent])
end
end
5)修改数据表report_templates中template_content内容为
pdf_writer.SetFont('GB','B',10)
pdf_writer.SetLeftMargin(80)
pdf_writer.Cell(40,10,Iconv.conv('GB2312','UTF-8','产品报表2'))
pdf_writer.Ln()
pdf_writer.SetFont('GB','',8)
pdf_writer.Cell(40,5,Iconv.conv('GB2312','UTF-8','名称'),1,0,'C')
pdf_writer.Cell(40,5,Iconv.conv('GB2312','UTF-8','类别'),1,0,'C')
pdf_writer.Cell(40,5,Iconv.conv('GB2312','UTF-8','价格'),1,0,'C')
pdf_wri
相关文档:
使用 will_paginate 进行分页和简单查询
在命令行下使用 gem install will_paginate 命令,出现下面结果安装成功
打开 books_controller.rb (你自己的控制器)
注释掉查找全部的方法,使用下面的方法,已经集成根据title进行查询
Ruby代码
#@books = Book.all
@books = Book.pagina ......
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 ......
%{String} 用于创建一个使用双引号括起来的字符串
%Q{String} 用于创建一个使用双引号括起来的字符串
%Q!Some String of “Characters”! <==> ” Some String of \”Characters\” “
%q{String} 用于创建一个使用单引号括起来的字符串
%q!Som ......
本文转自:
http://hi.baidu.com/24xinhui/blog/item/9f52dd34382e11325ab5f553.html
ruby-语法
2009年06月20日 星期六 上午 00:21
http://www.blogjava.net/xxllnnn/archive/2009/01/18/251762.html
http://www.cnblogs.com/cnblogsfans/archive/2009/01/24/1380804.html
__setobj__ (2009-7-14)
&nbs ......
转自 http://zhujg.javaeye.com/blog/355040
首先 安装 cygwin
cygwin 要安装的插件是
默认的+ make + gcc + libiconv
+ openssl
cygwin下需要编译原文件
到ruby-lang(http://ruby-lang.org/)
下载ruby-1.9.1-p0.tar.gz
tar xvf ruby-1.9.1-p0.tar.gz
cd
ruby-1.9.1-p0
./configure
make && make in ......