Class: ReportBuilder::Builder::Rtf
- Inherits:
-
ReportBuilder::Builder
- Object
- ReportBuilder::Builder
- ReportBuilder::Builder::Rtf
- Includes:
- RTF
- Defined in:
- lib/reportbuilder/builder/rtf.rb
Overview
Rtf Builder. Based on ruby-rtf (ruby-rtf.rubyforge.org/).
Instance Attribute Summary collapse
-
#rtf ⇒ Object
RTF::Document object.
Attributes inherited from ReportBuilder::Builder
Class Method Summary collapse
Instance Method Summary collapse
- #default_options ⇒ Object
-
#header(level, t) ⇒ Object
Add a header of level
level
with textt
. -
#html(t) ⇒ Object
Do nothing on this builder.
-
#initialize(builder, options) ⇒ Rtf
constructor
Creates a new Rtf object Params: *
builder
: A ReportBuilder::Builder object or other with same interface *options
: Hash of options. -
#out ⇒ Object
Returns rtf code for report.
-
#preformatted(t) ⇒ Object
Add preformatted text.
-
#save(filename) ⇒ Object
Save rtf file.
-
#text(*args, &block) ⇒ Object
Add a paragraph of text.
Methods inherited from ReportBuilder::Builder
#graph, #graph_entry, #image, #image_blob, #image_filename, inherited, inherited_classes, #parse, #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, ) 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)*2 cs.bold=true ps=ParagraphStyle.new ps.justification = ParagraphStyle::CENTER_JUSTIFY h[k]={:cs=>cs, :ps=>ps} } end |
Instance Attribute Details
#rtf ⇒ Object
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
.code ⇒ Object
44 45 46 |
# File 'lib/reportbuilder/builder/rtf.rb', line 44 def self.code %w{rtf} end |
Instance Method Details
#default_options ⇒ Object
49 50 51 52 53 54 55 56 57 |
# File 'lib/reportbuilder/builder/rtf.rb', line 49 def { :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
67 68 69 70 71 72 73 74 75 |
# File 'lib/reportbuilder/builder/rtf.rb', line 67 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
98 99 100 |
# File 'lib/reportbuilder/builder/rtf.rb', line 98 def html(t) # Nothing end |
#out ⇒ Object
Returns rtf code for report
89 90 91 |
# File 'lib/reportbuilder/builder/rtf.rb', line 89 def out @rtf.to_rtf end |
#preformatted(t) ⇒ Object
Add preformatted text. By default, uses Courier
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/reportbuilder/builder/rtf.rb', line 77 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
93 94 95 96 |
# File 'lib/reportbuilder/builder/rtf.rb', line 93 def save(filename) File.open(filename,'wb') {|file| file.write(@rtf.to_rtf) } end |
#text(*args, &block) ⇒ Object
Add a paragraph of text.
59 60 61 62 63 64 65 |
# File 'lib/reportbuilder/builder/rtf.rb', line 59 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 |