ThinReportsで、reportの作成方法やpageの作成方法はいくつも書き方があるので、
自分の使った書き方をメモ。
- 同一ファイル内に複数レイアウトを使用
- 一覧表を使用
- 一覧表のフッタに合計を表示
user_contents = { user_name: 'ユーザー名', user_address: 'sample@example.com' }
report = ThinReports::Report.create do |r|
# ユーザーページレイアウト
r.start_new_page :layout => File.join(Rails.root, 'app', 'reports', 'user.tlf') do |page|
page.values(user_contents)
end
# 本一覧ページレイアウト(表id: details)
r.start_new_page :layout => File.join(Rails.root, 'app', 'reports', 'books.tlf') do |page|
# フッタ作成時の処理
page.layout.config.list(:details) do
use_stores :total_cost => 0
events.on :footer_insert do |e|
e.section.item(:total_cost).value(total_cost)
end
end
# リスト作成処理
@books.each do |book|
book_contents = {title: book.title, cost: book.cost}
page.list(:details).add_row(book_contents)
page.list(:details).store.total_cost += cost
end
end
end
send_data report.generate,
filename: 'report.pdf',
type: 'application/pdf',
disposition: 'attachment'