LoginSignup
3
3

More than 5 years have passed since last update.

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

Last updated at Posted at 2014-02-24

やりたいこと

ThinReports のテキストブロックを任意の角度で回転させたい
( 0.7.7 時点で回転機能はない)

動作確認環境

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

準備

以下のパッチを require する:

# 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

使い方

  • (前提)テキストブロックの複数行モードのみ回転可能
  • 回転させたいテキストの最後に /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") # 90度回転させる
end

report.generate_file('foo.pdf')

回転後

回転結果

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

Rails で使う方法

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

最後に

ご利用は計画的に

3
3
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
3
3