21
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

RubyからPDFを生成 - prawnを使って

Last updated at Posted at 2012-05-21

gemをインストール

Gemfile
gem 'prawn'

フォントの設定

http://ossipedia.ipa.go.jp/ipafont/index.html
からフォントをダウンロードしアプリからアクセス可能な適当なディレクトリに置く

今回はとりあえずvendor/fontsにしたに下記のファイルを置いた

vendor/fonts/
├── ipaexg.ttf
└── ipaexm.ttf

rubyのサンプル

sinatraを使ってPDFを表示するサンプル

app.rb
require 'prawn'
require 'open-uri'
require 'sinatra'

get '/' do
  pdf = Prawn::Document.new do
    font "Helvetica"
    text "Test PDF"
    stroke_horizontal_rule
    move_down 30
    font "vendor/fonts/ipaexm.ttf"
    font_size 12
    text "これで日本語が使えるようになる"

    move_down 20
    image open("http://exsample.com/foo.jpg")
    text "画像はローカルからでもopen-uriを使ってurlからでも指定できる"
  end

  content_type "pdf"
  response["Content-Disposition"] = "inline"
  pdf.render
end

参考

21
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
21
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?