• 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

prawnto(プラウント?)使ってみました。まだ課題があるのですが、現時点でのメモです。

http://cracklabs.com/prawnto
http://cracklabs.com/prawnto/demos:デモ一覧
http://github.com/thorny-sun/prawnto/tree/master
git://github.com/thorny-sun/prawnto.git

生成したPDF

以前の記事をPDFにしてみました。
生成されたPDF:test.pdf

pluginのインストール

prawnライブラリを使っているので、ますprawnをインストール。その後prawnto pluginインストール。

   1  sudo gem install prawn
   2  ./script/plugin install    git://github.com/thorny-sun/prawnto.git

(日本語表示用に)IPAフォントをダウンロード

下記からダウンロードして解凍、ttfファイルをアクセス可能なところにコピーします。
http://ossipedia.ipa.go.jp/ipafont/

配置するとしたら下記でしょうか。

  • #{RAILS_ROOT}/vendor/fonts
  • #{RAILS_ROOT}/vendor/font_ttf
  • /usr/share/fonts/
  • /usr/local/share/fonts/)

Rails controller, view

まずはController
page_layoutは:portrailt(縦)か:landscape(横)のどちらかです。

   1  #controller
   2      respond_to do |format|
   3        format.html { render :action => 'show' }
   4        format.pdf {
   5          prawnto :prawn => {
   6            :page_layout => :portrait, #
   7            :page_size => "A4",
   8            :left_margin => 36,
   9            :right_margin => 24,
  10            :top_margin => 24,
  11            :bottom_margin => 24},
  12          :inline => true
  13        }
  14      end

そしてview(show.pdf.prawnという形式のファイル)

   1  #show.pdf.prawn
   2  pdf.font "#{RAILS_ROOT}/public/ipag.ttf"
   3  pdf.text @article.title, :size => 36
   4  pdf.text @article.body
   5  pdf.text "This page has very wide left and right margins, causing a squeeze. " * 10
   6  pdf.text "これはテストです。" * 20
   7  pdf.font "Times-Roman"
   8  pdf.text "abcdefghijklmnopqrstu"* 20, :wrap => :character
   9  pdf.text "http://thebogles.com/blog/2007/10/how-to-add-google-adsense-for-mobile-to-your-ruby-on-rails-site/"

現時点での課題

  • URLや日本語の文章など空白がない文字列?だと折り返されない
    => demoで紹介されている:wrap => :characterオプションが0.1.2だとなさそう?
  • レイアウト枠のpdfを重ねたりする機能はない?(発見できず)
posted by Png satoko on Wed 3 Sep 2008 at 23:28 with 2 comments
Contents
Rails:prawn & prawntoで日本語のPDF生成
Comments
KingofSmack: Here also good reads for this mobile applicatio... '14-5
satoko: stackoverflowでも同じエラーを挙げている人がいたので、1.3でアップロードしたよっ... '10-12
ujihisa: :%s/blog/glob/g '10-7
satoko: しゅが〜様 返事が遅くなって申し訳ありません。また、投稿百景ご購入ありがとうございます。 ... '09-10
しゅが~: こんにちは。投稿百景を発売日翌日から利用しています。本当にいいAppを作っていただきました。罫... '09-10
Services from s21g
twpro(ツイプロ)
Twitterプロフィールを快適検索
地価2009
土地の値段を調べてみよう
MyRestaurant
自分だけのレストラン手帳
Formula
ブログに数式を埋め込める数式コミュニティ