今、Rubyで帳票を作成するならThinReportsが熱い!ということらしいので、Mac OSX Lionで使ってみる。
まずは、下記URLにて概要を確認。
http://www.thinreports.org/
インストールはRubyGemsを使ってできるようだけど、RubyGemsは1.3.7以上を使ってくださいとのこと。
Lionの標準は1.3.6なので、以下のコマンドで1.3.7にしてみる。
$ gem install -v 1.3.7 rubygems-update
$ update_rubygems
[実行結果]
$ gem -v
1.3.6
$ sudo gem install -v 1.3.7 rubygems-update
Password:
Successfully installed rubygems-update-1.3.7
1 gem installed
Installing ri documentation for rubygems-update-1.3.7...
Installing RDoc documentation for rubygems-update-1.3.7...
Could not find main page README
Could not find main page README
Could not find main page README
Could not find main page README
$ sudo update_rubygems
RubyGems 1.3.7 installed
=== 1.3.7 / 2010-05-13
NOTE:
http://rubygems.org is now the default source for downloading gems.
You may have sources set via ~/.gemrc, so you should replace
http://gems.rubyforge.org with http://rubygems.org
http://gems.rubyforge.org will continue to work for the forseeable future.
New features:
* `gem` commands
* `gem install` and `gem fetch` now report alternate platforms when a
matching one couldn't be found.
* `gem contents` --prefix is now the default as specified in --help. Bug
#27211 by Mamoru Tasaka.
* `gem fetch` can fetch of old versions again. Bug #27960 by Eric Hankins.
* `gem query` and friends output now lists platforms. Bug #27856 by Greg
Hazel.
* `gem server` now allows specification of multiple gem dirs for
documentation. Bug #27573 by Yuki Sonoda.
* `gem unpack` can unpack gems again. Bug #27872 by Timothy Jones.
* `gem unpack` now unpacks remote gems.
* --user-install is no longer the default. If you really liked it, see
Gem::ConfigFile to learn how to set it by default. (This change was made
in 1.3.6)
* RubyGems now has platform support for IronRuby. Patch #27951 by Will Green.
Bug fixes:
* Require rubygems/custom_require if --disable-gem was set. Bug #27700 by
Roger Pack.
* RubyGems now protects against exceptions being raised by plugins.
* rubygems/builder now requires user_interaction. Ruby Bug #1040 by Phillip
Toland.
* Gem::Dependency support #version_requirements= with a warning. Fix for old
Rails versions. Bug #27868 by Wei Jen Lu.
* Gem::PackageTask depends on the package dir like the other rake package
tasks so dependencies can be hooked up correctly.
------------------------------------------------------------------------------
RubyGems installed the following executables:
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/gem
$ gem -v
1.3.7
参考URL
http://d.hatena.ne.jp/ruedap/20110221/ruby_gem_update_version_down
途中、いくつかエラーと思しき文字列も見受けられるけど、完了したと言っているので無視する。
また、Lionに標準で入っているRuby1.8.7にはjsonが必要とのことなので、以下のコマンドでこれもインストールしておきます。
$ sudo gem install json
[実行結果]
$ sudo gem install json
Password:
Building native extensions. This could take a while...
Successfully installed json-1.6.5
1 gem installed
Installing ri documentation for json-1.6.5...
Installing RDoc documentation for json-1.6.5...
最後にThinReports本体をインストールする。
$ sudo gem install thinreports
[実行結果]
$ sudo gem install thinreports
Password:
********************************************
v1.0.0 of PDF::Reader introduced a new page-based API. There are extensive
examples showing how to use it in the README and examples directory.
For detailed documentation, check the rdocs for the PDF::Reader,
PDF::Reader::Page and PDF::Reader::ObjectHash classes.
The old API is marked as deprecated but will continue to work with no
visible warnings for now.
********************************************
********************************************
A lot has changed since 0.8.4
Please read the changelog for details:
https://github.com/sandal/prawn/wiki/CHANGELOG
\ ********************************************
Successfully installed Ascii85-1.0.1
Successfully installed ruby-rc4-0.1.5
Successfully installed pdf-reader-1.0.0
Successfully installed ttfunk-1.0.3
Successfully installed prawn-0.12.0
Successfully installed thinreports-0.7.0
6 gems installed
Installing ri documentation for Ascii85-1.0.1...
Installing ri documentation for ruby-rc4-0.1.5...
Installing ri documentation for pdf-reader-1.0.0...
Installing ri documentation for ttfunk-1.0.3...
Installing ri documentation for prawn-0.12.0...
Installing ri documentation for thinreports-0.7.0...
Installing RDoc documentation for Ascii85-1.0.1...
Installing RDoc documentation for ruby-rc4-0.1.5...
Installing RDoc documentation for pdf-reader-1.0.0...
Installing RDoc documentation for ttfunk-1.0.3...
Installing RDoc documentation for prawn-0.12.0...
Could not find main page README
Could not find main page README
Could not find main page README
Could not find main page README
Installing RDoc documentation for thinreports-0.7.0...
インストールされたライブラリを見ると、Prown使ってますね。
過去に記事を書いたことがあるので、これだけで何となく親しみを感じます。
では、次にエディタをダウンロード&インストール。
上記のThinReportsのサイトからMac版をダウンロードして、アプリケーションに入れておきます。
インストールが終わったら、早速試しに帳票を作成してみます。
下記URLに手順があるのでそれを参考にする。
http://osc.matsukei.net/projects/thinreports/wiki/Getting_Started
とりあえずテキストを一つ追加して、IDを「mySample」としてみました。
そして、ファイルを「sample.tlf」として保存し、下記のコードを書いて実行してみる。
# coding: utf-8
require 'rubygems'
require 'thinreports'
report = ThinReports::Report.new :layout => 'sample'
report.start_new_page
report.page.item(:mySample).value('aaaaa')
report.generate_file('sample.pdf')
puts 'Done!'
すると...できた、できた!
きちんと、入力した文字列が「aaaaa」に置き換わってPDFが作成されてます。
ちなみに、サンプルコードを以下のように変更するとWebアプリケーションから動的にPDFを表示することもできるようになります。
#!/usr/bin/ruby
# coding: utf-8
require 'rubygems'
require 'thinreports'
require 'cgi'
report = ThinReports::Report.new :layout => 'sample'
report.start_new_page do
values(
:mysample => "aaaa123"
)
end
$cgi = CGI.new()
print $cgi.header('type' => 'application/pdf')
print report.generate
やるなThinReports、使えるぞ。
最近のコメント