Class: Ruport::Formatter::PrawnPDF

Inherits:
Ruport::Formatter show all
Defined in:
lib/ruport/formatter/prawn_pdf.rb

Instance Attribute Summary collapse

Attributes inherited from Ruport::Formatter

#data, #format, #options

Instance Method Summary collapse

Methods inherited from Ruport::Formatter

build, #clear_output, #erb, formats, #output, renders, save_as_binary_file, #save_output, #template

Methods included from RenderingTools

#render_group, #render_grouping, #render_inline_grouping, #render_row, #render_table

Constructor Details

#initializePrawnPDF

Returns a new instance of PrawnPDF.



13
14
15
16
# File 'lib/ruport/formatter/prawn_pdf.rb', line 13

def initialize
  require 'prawn'
  require 'prawn/table'
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(id, *args, &block) ⇒ Object



9
10
11
# File 'lib/ruport/formatter/prawn_pdf.rb', line 9

def method_missing(id,*args, &block)
  pdf.send(id,*args, &block)
end

Instance Attribute Details

#pdfObject



18
19
20
21
# File 'lib/ruport/formatter/prawn_pdf.rb', line 18

def pdf
  @pdf ||= (options.formatter || 
    ::Prawn::Document.new(options[:pdf_format] || {} ))
end

Instance Method Details

#apply_templateObject

Hook for setting available options using a template. See the template documentation for the available options and their format.



74
75
76
77
78
79
80
81
# File 'lib/ruport/formatter/prawn_pdf.rb', line 74

def apply_template
  apply_page_format_template(template.page)
  apply_text_format_template(template.text)
  apply_table_format_template(template.table)
  apply_column_format_template(template.column)
  apply_heading_format_template(template.heading)
  apply_grouping_format_template(template.grouping)
end

#build_group_bodyObject



55
56
57
# File 'lib/ruport/formatter/prawn_pdf.rb', line 55

def build_group_body
  render_table data, options.to_hash.merge(:formatter => pdf)
end

#build_grouping_body(&block) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ruport/formatter/prawn_pdf.rb', line 59

def build_grouping_body(&block)
  data.each do |name,group|

    # Group heading
    move_down(20)
    text name, :style => :bold, :size => 15

    # Table
    move_down(10)
    draw_table group, &block
  end
end

#build_table_body(&block) ⇒ Object



51
52
53
# File 'lib/ruport/formatter/prawn_pdf.rb', line 51

def build_table_body(&block)
  draw_table(data, &block)
end

#draw_table(table, format_opts = {}, &block) ⇒ Object

Raises:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ruport/formatter/prawn_pdf.rb', line 23

def draw_table(table, format_opts={}, &block)
  m = "PDF Formatter requires column_names to be defined"
  raise FormatterError, m if table.column_names.empty?

  table.rename_columns { |c| c.to_s }

  table_array = [table.column_names]
  table_array += table_to_array(table)
  table_array.map { |array| array.map! { |elem| elem.class != String ? elem.to_s : elem }}

  if options[:table_format]
    opt = options[:table_format] 
  else
    opt = format_opts
  end

  pdf.table(table_array, opt, &block)

end

#finalizeObject



47
48
49
# File 'lib/ruport/formatter/prawn_pdf.rb', line 47

def finalize
  output << pdf.render
end

#table_to_array(tbl) ⇒ Object



43
44
45
# File 'lib/ruport/formatter/prawn_pdf.rb', line 43

def table_to_array(tbl)
  tbl.map { |row| row.to_a}
end