Class: ReportBuilder::Builder::Rtf

Inherits:
ReportBuilder::Builder show all
Includes:
RTF
Defined in:
lib/reportbuilder/builder/rtf.rb

Overview

Rtf Builder. Based on ruby-rtf (ruby-rtf.rubyforge.org/).

Instance Attribute Summary collapse

Attributes inherited from ReportBuilder::Builder

#options, #parse_level, #toc

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ReportBuilder::Builder

#graph, #graph_entry, #image, #image_blob, #image_filename, inherited, inherited_classes, #parse_cycle, #parse_element, #section, #table, #table_entry, #toc_entry

Constructor Details

#initialize(builder, options) ⇒ Rtf

Creates a new Rtf object Params:

  • builder: A ReportBuilder::Builder object or other with same interface

  • options: Hash of options.

    • :font: Font family. Default to “Times New Roman”

    • :font_size: Font size. Default to 20

    • :table_border_width

    • :table_hr_width



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/reportbuilder/builder/rtf.rb', line 21

def initialize(builder, options)
  super
  @font=Font.new(Font::ROMAN, @options[:font])
  
  @rtf = Document.new(@font)
  @pre_char = CharacterStyle.new
  
  @pre_char.font = Font.new(Font::MODERN, 'Courier')
  @pre_char.font_size=@options[:font_size]
  
  @pre_par  = ParagraphStyle.new
  
  @header_styles=Hash.new {|h,k|
    cs=CharacterStyle.new
    cs.font=@font
    cs.font_size=@options[:font_size]+(8-k)*1.5
    cs.bold=true
    ps=ParagraphStyle.new
    ps.justification = ParagraphStyle::CENTER_JUSTIFY
    h[k]={:cs=>cs, :ps=>ps}
  }        
end

Instance Attribute Details

#rtfObject

RTF::Document object. See ruby-rtf.rubyforge.org/ for documentation



11
12
13
# File 'lib/reportbuilder/builder/rtf.rb', line 11

def rtf
  @rtf
end

Class Method Details

.codeObject



49
50
51
# File 'lib/reportbuilder/builder/rtf.rb', line 49

def self.code
  %w{rtf}
end

Instance Method Details

#default_optionsObject



54
55
56
57
58
59
60
61
62
# File 'lib/reportbuilder/builder/rtf.rb', line 54

def default_options
  
  {
    :font=>'Times New Roman',
    :font_size=>20,
    :table_border_width=>3,
    :table_hr_width=>25
  }
end

#header(level, t) ⇒ Object

Add a header of level level with text t



72
73
74
75
76
77
78
79
80
# File 'lib/reportbuilder/builder/rtf.rb', line 72

def header(level,t)
  @rtf.paragraph(@header_styles[level][:ps]) do |n1|
    n1.apply(@header_styles[level][:cs]) do |n2|
      n2.line_break
      n2 << t
      n2.line_break
    end
  end
end

#html(t) ⇒ Object

Do nothing on this builder



107
108
109
# File 'lib/reportbuilder/builder/rtf.rb', line 107

def html(t)
  # Nothing
end

#new_pageObject

Do nothing



98
99
100
# File 'lib/reportbuilder/builder/rtf.rb', line 98

def new_page
  
end

#outObject

Returns rtf code for report



94
95
96
# File 'lib/reportbuilder/builder/rtf.rb', line 94

def out
  @rtf.to_rtf
end

#parseObject



43
44
45
46
47
48
# File 'lib/reportbuilder/builder/rtf.rb', line 43

def parse
   unless @builder.no_title
     header(0,@builder.name)
   end
  parse_cycle(@builder)
end

#preformatted(t) ⇒ Object

Add preformatted text. By default, uses Courier



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/reportbuilder/builder/rtf.rb', line 82

def preformatted(t)
  @rtf.paragraph(@pre_par) do |n1|
    n1.apply(@pre_char) do |n2|
      t.split("\n").each do |line|
        n2 << line
        n2.line_break
      end
    end
  end
  
end

#save(filename) ⇒ Object

Save rtf file



102
103
104
105
# File 'lib/reportbuilder/builder/rtf.rb', line 102

def save(filename)
  File.open(filename,'wb')  {|file| file.write(@rtf.to_rtf)
  }
end

#text(*args, &block) ⇒ Object

Add a paragraph of text.



64
65
66
67
68
69
70
# File 'lib/reportbuilder/builder/rtf.rb', line 64

def text(*args,&block)
  if args.size==1 and args[0].is_a? String and !block
    @rtf.paragraph << args[0]
  else
    @rtf.paragraph(*args,&block)
  end
end