日本語PDFを作れなかった話
昨日の「PDFJをインストールしてみる」より前に試していたのだが、アップするタイミングを逃していたPDF::Writeモジュールについての調査結果をアップしておく。
経験上、「何が出来て、何が出来なかったか。また、その理由。」というのは「うまくいく理由」よりも、あとで大事になる情報だからね。
PDF::Writerと出会う
まず、Rubyで使えるPDFライブラリを探してみた。こういうのはRubyGemを使うと簡単。
>gem search -r pdf *** REMOTE GEMS *** Bulk updating Gem source index for: http://gems.rubyforge.org jpeg2pdf (0.12) jpeg2pdf is a free program that converts a directory of JPEG files to a PDF file. pdf-labels (1.0.0) Easy label creation in Ruby through PDF::Writer and using templates from gLabels. Contains the library pdf_labels, the Rails engine LabelPageEngine and an example application FileClerk. pdf-toolkit (0.49, 0.5.0) A wrapper around pdftk to allow PDF metadata manipulation pdf-writer (1.1.3, 1.1.2, 1.1.1, 1.1.0, 1.0.1, 1.0.0) A pure Ruby PDF document creation library.
説明文を読むと、pdf-writerが良さそうな気がします。
- http://rubyforge.org/projects/ruby-pdf/
- http://ruby-pdf.rubyforge.org/pdf-writer/index.html
- We retire raa.ruby-lang.org
上記のようにいろいろリンクがありますが、Ruby Gemでインストールすれば、依存ライブラリも適当に返事するだけでインストールしてくれます。
以下のようにタイプします。
>gem install pdf-writer Bulk updating Gem source index for: http://gems.rubyforge.org Install required dependency color-tools? [Yn] Y Install required dependency transaction-simple? [Yn] Y Successfully installed pdf-writer-1.1.3 Successfully installed color-tools-1.3.0 Successfully installed transaction-simple-1.4.0 Installing ri documentation for pdf-writer-1.1.3... Installing ri documentation for color-tools-1.3.0... Installing ri documentation for transaction-simple-1.4.0... Installing RDoc documentation for pdf-writer-1.1.3... Installing RDoc documentation for color-tools-1.3.0... Installing RDoc documentation for transaction-simple-1.4.0... >
以上でインストールは完了。
PDF::Writerを試す
早速使ってみる。
sampleにあるhello.rbを参考にして以下のようなコードを実行してみた。
def initLibs begin require 'pdf/writer' print "Load OK\n" rescue LoadError => le print "Load Error: #{le}\n" exit end end def createPDF pdf = PDF::Writer.new pdf.select_font( "Times-Roman" ) pdf.text( "Hello, Ruby with PDF::Writer",\ :font_size => 72, :justification => :center ) pdf.save_as( "hello.pdf" ) end initLibs createPDF
これで、特に問題なく、hello.pdfが作成されました!おぉ〜、ちょっと感動。
PDF::Writerは日本語に非対応
その後、いろいろソースを試してたら、現在配布されているPDF::Writerでは日本語フォントに対応していないことが判明した。
検索してみたら、日本語フォントパッチと名づけられたものを発見。
しかし、ダウンロードしてdiffファイルを見てみると、ソース内でフォント名をハードコーディングして対応している。それはRyuminとGothicBBBの二つのみ。これでは使えない・・。
ちなみに、こちらの記事が上記パッチ実装までの過程のようです。
PDF::Writerはフォント埋め込みも非対応
もう一つの難点が、フォント埋め込みに非対応、という点。
ちなみにパッチはここにある。
結論
とりあえず今やっている仕事では、日本語で縦書きされたPDFを作りたかったので、使えなさそう。
テキストが英語程度で、グラフィック中心ならかなり使い勝手が良さそうな気がします。
2005年末から2年、全然更新されていないのが残念でならない。せめてフォント埋め込みパッチを正規リポジトリに対応させたりして欲しいところ。
オープンソースなんだからおまえがやれ、と言われそうだけど。
ここでも読んでしっかり勉強しなきゃ、かも。