Skip to content

Instantly share code, notes, and snippets.

@hidakatsuya
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hidakatsuya/9173030 to your computer and use it in GitHub Desktop.
Save hidakatsuya/9173030 to your computer and use it in GitHub Desktop.
ThinReports でテキストを回転させるハック

ThinReports でテキストを回転させる

動作確認環境

  • ThinReports 0.7.6
  • Ruby 1.9.3, 2.0
  • Rails 3.2, 4.0

準備

パッチ thinreports-with-text-rotation.rb を require するだけ

使い方

  • テキストブロックの複数行モードのみ回転可能
  • 回転させたいテキストの最後に /r<回転率> を付与する
  • 付与された /r<回転率> は描画時に取り除かれる

サンプル

# coding: utf-8
require 'thinreports'
require_ralative 'thinreports-with-text-rotation'

report = ThinReports::Report.new layout: 'foo.tlf'
report.start_new_page do |page|
  page.item(:text).value("ラベル\nラベル/r90")
end

report.generate_file('foo.pdf')

回転後

結果

  • テキストボックスの 左下を起点に 回転される

Rails で使う方法

thinreports-with-text-rotation.rbconfig/initializers に配置すれば OK

# coding: utf-8
require 'active_support/core_ext'
require 'thinreports'
module ThinReportsWithTextRotation
def self.included(base)
base.class_eval do
alias_method_chain :text_box, :text_rotation
end
end
def text_box_with_text_rotation(content, x, y, w, h, attrs = {})
rotation = !attrs[:single] && content && content.match(%r!/r(?<rate>\d+)$!)
if rotation
content = content.delete(rotation.to_s)
attrs = attrs.merge :rotate => rotation[:rate].to_i,
:rotate_around => :lower_left
end
text_box_without_text_rotation(content, x, y, w, h, attrs)
end
end
ThinReports::Generator::PDF::Document.send :include, ThinReportsWithTextRotation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment