LoginSignup
0
1

More than 3 years have passed since last update.

RubyでExcelからPDFを作るサンプル

Last updated at Posted at 2020-12-20

背景

昨今、EXCELに書かれたリスト形式の情報を抽出、整理してPDFとして帳票形式で出力するプログラムを作ることが多い。忘れないうちにブログラムのエッセンスを記述しておきたい。

プログラム概要

EXCELにリストされるている名簿を読んで
以下のPDFファイルを名簿人数分、いっきに作成する。

実行環境一式のGitHubはこちら

image.png

開発環境

  • macOS Big Sur
  • Ruby 2.6.5

手順

  1. EXCEL名簿
  2. 背景画像
  3. 日本語フォント
  4. プロジェクト作成
  5. プログラムコード作成

1.EXCEL名簿

次のEXCELファイルを作る

  • ファイル名とバス
    ./infiles/members.xlsx

  • シート名
    2021年メッセージ
    image.png

名称 用途
A カードに出力
B カードに出力
C last名 ファイル名に使用
D first名 ファイル名に使用
E メッセージ カードに出力

サンプル氏名は gem gimei で作成

2. 背景画像

以下の名称でイメージファイルを準備する
./infiles/background.png

image.png サンプル(筆者撮影)

3. 日本語フォント

PDF出力のために 日本語フォントをダウンロードして
./infiles/ipam.ttf に置く

4.プロジェクト作成

bundlerを使用する(バージョン固定のため。必須ではない)

Gemfile
% bundle init

生成されたGemfileに以下を追記

Gemfile
gem "roo", "~> 2.8.0"
gem "prawn", "~> 2.3.0"

以下でインストール

% bundle install

Gemfile.lock が作られる

5. プログラムコード作成

cows.rb
# Gemfileのgemを一括require
require 'bundler'
Bundler.require

# EXCELファイルをOPEN
excel_path = './infiles/members.xlsx'
book = Roo::Spreadsheet.open(excel_path)

# シート名を指定する(必須ではない)
sheet = book.sheet('2021年メッセージ')

# 存在する最終列、最終行の確認
puts "最後の列番号: #{sheet.last_column}  最後の行番号: #{sheet.last_row}"
# 'B3'セルを参照する場合はsheet.cell(3, 2) or sheet.cell(3, 'B')

# シートの行数分でループ。先頭行はヘッダーとしてスキップ
(2..sheet.last_row).each do |idx|

  # PDF初期化
  # A6(はがきサイズ) 横向き 縁なし
  pdf = Prawn::Document.new(
    page_size: "A6", page_layout: :landscape,
    top_margin: 0, bottom_margin: 0, left_margin: 0, right_margin: 0)

  # 日本語フォントの読み込み
  pdf.font "./infiles/ipam.ttf"

  # 背景画像 縁なしで配置
  pdf.image("./infiles/background.png", at: [0,pdf.cursor])

  # 描画エリアを定義
  pdf.bounding_box([10, pdf.cursor-10], width: 500) do
    # 'B3'セルを参照する場合はsheet.cell(3, 2) or sheet.cell(3, 'B')

    # 名前
    pdf.font_size(20)
    sei = sheet.cell(idx, "A")
    mei = sheet.cell(idx, "B")
    pdf.text "#{sei} #{mei} さん", color: "ffffff"

    # アケオメ
    pdf.move_down 15
    pdf.font_size(24)
    greet = " あけましておめでとうございます"
    pdf.text greet, color: "ffffff"

    # メッセージ
    pdf.move_down 15
    pdf.font_size(14)
    message = sheet.cell(idx, "E")
    pdf.text message, color: "c9dfed"

    # チェック
    puts  " -- [#{idx}] #{sei} #{mei} さん  msg: #{message}"
  end

  # うし
  pdf.bounding_box([165, pdf.cursor-80], width: 500) do
    pdf.font_size(80)
    pdf.text "丑の年",  color: "ff75b1"
  end

  # PDF保存  ファイル名 "outfiles/last_first.pdf"
  file_name = "./outfiles/#{sheet.cell(idx,"C")}_#{sheet.cell(idx,"D")}.pdf"
  pdf.render_file(file_name)

end

フォルダ構成

├── Gemfile
├── Gemfile.lock
├── cows.rb (ブログラムコード)
├── infiles
│   ├── background.png (背景画像ファイル)
│   ├── ipam.ttf (日本語フォント)
│   └── members.xlsx (名簿ファイル)
└── outfiles
    ├── hara_suzuka.pdf (作成させたPDF)
    ├── .....
    └── .....

以下でrubyを実行すると、./outfies/ 以下にpdfが作成される

% ruby cows.rb

参考サイト

https://github.com/roo-rb/roo
https://github.com/prawnpdf/prawn
https://prawnpdf.org/manual.pdf
https://moji.or.jp/ipafont/ipaex00401/
https://github.com/willnet/gimei

0
1
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
0
1