prawn-svg + Inkscape = 帳票作成
先々週から細々といじっているprawn-svgですが、なんとかそれっぽく使えるようになってきました。
その後の調査でわかったことは、
- Inkscapeの吐き出すSVGでは、文字列のタグとして'text'と'flowRoot'の2種類があるが、prawn-svgでは'text'しか処理していない
- 日本語を表示しようとするとうまく出なかったのは、Inkscapeで設定しているフォントをprawnが認識出来ず、英字フォントで表示しようとしていたため
ということでしたので、これを解決するために少しソースをいじって、下記のコードを追加してみました。
##### lib/prawn/svg/parser.rb 160行目付近 ##### when 'flowRoot' # Very primitive support for font-family; it won't work in most cases because # PDF only has a few built-in fonts, and they're not the same as the names # used typically with the web fonts. if font_family = style_attrs["font-family"] if font_family != "" && pdf_font = map_font_family_to_pdf_font(font_family) calls << ['font', [pdf_font], []] calls = calls.last.last else @warnings << "#{font_family} is not a known font." end end element.elements.each do |child| #puts child.attributes if child.name == 'flowRegion' then #element.text = child.text child.elements.each do |child2| child_attrs = child2.attributes opts = {:at => [x(child_attrs['x']), y(child_attrs['y'])]} end elsif child.name == 'flowPara' then element.text = child.text end end opts = {:at => [x(attrs['x']), y(attrs['y'])]} if size = style_attrs['font-size'] opts[:size] = size.to_f * @scale end # This is not a prawn option but we can't work out how to render it here - # it's handled by Svg#rewrite_call_arguments if anchor = style_attrs['text-anchor'] opts[:text_anchor] = anchor end calls << ['text_box', [element.text, opts], []] ##### ここまで #####
次に実行する側のソースにてフォントをあらかじめ指定しておきます。
Prawn::Document.generate("sample.pdf") do font "ipag.ttf" svg svg_data, :at => [50, 200.mm], :width => 100.mm end
これを実行するときちんと日本語が表示されて、万事OK。
現在のファイルをダウンロードできるようにおいておきます。もし、興味のある方がおられればご利用下さい。ちなみにprawn-svgのバージョンは0.9.1.8です。
prawnをインストールする必要があるから、無料や安めのレンタルサーバで使用するのは少し難しいかもしれないけれど、環境を自由に構築出来る自営のシステムではいろいろな使い道があるような気がします。
| 固定リンク
コメント