金子邦彦研究室プログラミングRuby プログラミングRuby で PDF ファイルを生成してみる(RubyJavaブリッヂと iTextPDF を使用)

Ruby で PDF ファイルを生成してみる(RubyJavaブリッヂと iTextPDF を使用)

Ruby で PDF ファイルを生成してみる.

前準備

  1. RubyJavaブリッヂのインストールが済んでいること
  2. iText PDFのインストールが済んでいること

プログラム例 (Sample Program)


#! ruby -Ks
# coding: windows-31j

require 'rubygems'
require 'rjb'

classpaths = Dir.glob("C:/Program Files/jasperreports-3.5.2/lib/*.jar")
classpaths.concat(Dir.glob("C:/Program Files/Java/*.jar"))
Rjb::load(classpath = classpaths.join(':'), jvmargs=[])

# PDF 出力用
Document = Rjb::import("com.lowagie.text.Document")
Paragraph = Rjb::import("com.lowagie.text.Paragraph")
Font = Rjb::import("com.lowagie.text.Font")
BaseFont = Rjb::import("com.lowagie.text.pdf.BaseFont")
PdfWriter = Rjb::import("com.lowagie.text.pdf.PdfWriter")
FileOutputStream = Rjb::import("java.io.FileOutputStream")
PageSize = Rjb::import("com.lowagie.text.PageSize")
Table = Rjb::import("com.lowagie.text.Table")
Cell = Rjb::import("com.lowagie.text.Cell")

# ヘッダ、フッタ用
Phrase = Rjb::import("com.lowagie.text.Phrase")
HeaderFooter = Rjb::import("com.lowagie.text.HeaderFooter")
Element = Rjb::import("com.lowagie.text.Element")
Rectangle = Rjb::import("com.lowagie.text.Rectangle")

# フォント生成
mincho = BaseFont.createFont("HeiseiMin-W3", "UniJIS-UCS2-HW-H", false)
gothic = BaseFont.createFont("HeiseiKakuGo-W5", "UniJIS-UCS2-H", false)
min10 = Font.new(mincho, 10)
min105 = Font.new(mincho, 10.5)
min11 = Font.new(mincho, 11)
min12 = Font.new(mincho, 12)
gothic10 = Font.new(gothic, 10)
gothic105 = Font.new(gothic, 10.5)
gothic11 = Font.new(gothic, 11)
gothic12 = Font.new(gothic, 12)

# PDF 出力
doc = Document.new(PageSize.A4)
PdfWriter.getInstance(doc, FileOutputStream.new("hello.pdf"))

# フッタ設定
footer = HeaderFooter.new( Phrase.new("--", min10), Phrase.new("--", min10) )
footer.setAlignment(Element.ALIGN_CENTER)
footer.setBorder(Rectangle.NO_BORDER)
doc.setFooter(footer)

# 出力開始
doc.open()

# 本文
p1 = Paragraph.new("報 告 書", Font.new(gothic, 20, Font.BOLD))
p1.setAlignment(Element.ALIGN_CENTER)
doc.add(p1)
p2 = Paragraph.new(50, "担当者様", Font.new(mincho, 15, Font.UNDERLINE))
p2.setAlignment(Element.ALIGN_LEFT)
doc.add(p2)
p3 = Paragraph.new("報告番号:\n報告日:", Font.new(mincho, 12))
p3.setAlignment(Element.ALIGN_RIGHT)
doc.add(p3)

# テーブル
t = Table.new(4)
t.setPadding(4)
# 各セルの横幅の比率
t.setWidths([10, 30, 15, 45])
# 全体の横幅
t.setWidth(100)
cell1 = Cell.new( Phrase.new("id", min105))
cell1.setGrayFill(0.9)
cell1.setHorizontalAlignment(Element.ALIGN_CENTER)
cell2 = Cell.new( Phrase.new("name", Font.new(gothic, 14) ) )
cell2.setGrayFill(0.9)
cell2.setHorizontalAlignment(Element.ALIGN_CENTER)
cell3 = Cell.new( Phrase.new("type", Font.new(gothic, 14) ) )
cell3.setGrayFill(0.9)
cell3.setHorizontalAlignment(Element.ALIGN_CENTER)
cell4 = Cell.new( Phrase.new("address", Font.new(gothic, 14) ) )
cell4.setGrayFill(0.9)
cell4.setHorizontalAlignment(Element.ALIGN_CENTER)
t.addCell(cell1)
t.addCell(cell2)
t.addCell(cell3)
t.addCell(cell4)

hoge1 = Cell.new( Phrase.new("hoge", min105))
hoge1.setHorizontalAlignment(Element.ALIGN_CENTER)
t.addCell(hoge1)
hoge2 = Cell.new( Phrase.new("hoge", min105))
hoge2.setHorizontalAlignment(Element.ALIGN_CENTER)
t.addCell(hoge2)
hoge3 = Cell.new( Phrase.new("hoge", min105))
hoge3.setHorizontalAlignment(Element.ALIGN_CENTER)
t.addCell(hoge3)
hoge4 = Cell.new( Phrase.new("hoge", min105))
hoge4.setHorizontalAlignment(Element.ALIGN_CENTER)
t.addCell(hoge4)

doc.add(t)

doc.close()

■ 実行結果の例(Result Example)

[image]

[image]

※ テーブルの中では日本語が表示できない.私の力ではここで力尽きる.(2010/02)