Class: TnPDF::Report

Inherits:
Object
  • Object
show all
Defined in:
lib/tn_pdf/report.rb

Overview

The Report class is the easier, best supported and more complete way to access the TnPDF API. It delegates most of it’s work to fellow classes such as Table and PageSection, but the delegation itself and all the necessary setup is made under the hood. This means that everything the code needs to do is access methods such as #record_collection and #table_columns, and then it is forwarded to the appropriate member.

For more specific needs, methods such as #table and #page_header, which give direct access to the report’s members, are provided, so that the user can make low-level/bleeding edge adjustments without being restricted to this classe’s interface.

Author:

  • Renato Zannon

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(properties = {}) ⇒ Report

Returns a new instance of Report.



118
119
120
121
122
123
# File 'lib/tn_pdf/report.rb', line 118

def initialize(properties = {})
  @page_header = PageSection.new
  @page_footer = PageSection.new
  @record_collection = Array.new
  initialize_properties(properties)
end

Instance Attribute Details

#bottom_marginDouble

Sets the document’s bottom margin. Accepts a Double value, defined in PDF points (1/72 inch) or a String “in” cm or mm, such as “1.5cm” and “50mm”.

Returns:

  • (Double)

    the current value of bottom_margin



79
80
81
# File 'lib/tn_pdf/report.rb', line 79

def bottom_margin
  @bottom_margin
end

#documentPrawn::Document (readonly)

The underlying Prawn::Document. Direct manipulation is highly disencouraged, except for querying information.

Returns:

  • (Prawn::Document)


218
219
220
# File 'lib/tn_pdf/report.rb', line 218

def document
  @document
end

#fontString

The default font to be used on the report. The only (currently) supported choices are “Helvetica” and “Courier”, although Prawn’s font embedding mechanism is a probable upcoming addition

Returns:

  • (String)

    the current value of font



79
80
81
# File 'lib/tn_pdf/report.rb', line 79

def font
  @font
end

#font_sizeFixnum

The default report font size. In the usual “points” unit.

Returns:

  • (Fixnum)

    the current value of font_size



79
80
81
# File 'lib/tn_pdf/report.rb', line 79

def font_size
  @font_size
end

#images_pathString

The path from where we will search for the requested images. Defaults to the current path, “./”. In a Rails application, for instance, you would probably want to set this guy to RAILS_ROOT+“public/images”

Returns:

  • (String)

    the current value of images_path



79
80
81
# File 'lib/tn_pdf/report.rb', line 79

def images_path
  @images_path
end

#left_marginDouble

Sets the document’s left margin. Accepts a Double value, defined in PDF points (1/72 inch) or a String “in” cm or mm, such as “1.5cm” and “50mm”.

Returns:

  • (Double)

    the current value of left_margin



79
80
81
# File 'lib/tn_pdf/report.rb', line 79

def left_margin
  @left_margin
end

The underlying PageSection that represents the report pages’ footers. Direct manipulation is disencouraged.

Returns:



89
90
91
# File 'lib/tn_pdf/report.rb', line 89

def page_footer
  @page_footer
end

#page_headerPageSection (readonly)

The underlying PageSection that represents the report pages’ headers. Direct manipulation is disencouraged.

Returns:



84
85
86
# File 'lib/tn_pdf/report.rb', line 84

def page_header
  @page_header
end

#page_layoutSymbol

The resulting pages’ layout. Must be :landscape or :portrait

Returns:

  • (Symbol)

    the current value of page_layout



79
80
81
# File 'lib/tn_pdf/report.rb', line 79

def page_layout
  @page_layout
end

#page_sizeString

The report’s page size, in paper sizes such as “A4” and “Letter”. Supports as much as much paper sizes as Prawn does.

Returns:

  • (String)

    the current value of page_size



79
80
81
# File 'lib/tn_pdf/report.rb', line 79

def page_size
  @page_size
end

#record_collectionArray

The underlying Table‘s collection of objects. Each of these objects will be represented as a table row, by the application of the procedures described using #table_columns=. The order in which this property and #table_columns are called isn’t an issue, as soon as both are set before #render is called.

Returns:

  • (Array)


107
108
109
# File 'lib/tn_pdf/report.rb', line 107

def record_collection
  @record_collection
end

#right_marginDouble

Sets the document’s right margin. Accepts a Double value, defined in PDF points (1/72 inch) or a String “in” cm or mm, such as “1.5cm” and “50mm”.

Returns:

  • (Double)

    the current value of right_margin



79
80
81
# File 'lib/tn_pdf/report.rb', line 79

def right_margin
  @right_margin
end

#tableTable (readonly)

The underlying Table. Direct manipulation is disencouraged, except in cases where fine adjustments are required, or when some (possibly bleeding-edge) functionality is not implemented on TnPDF::Report (yet).

Returns:



95
96
97
# File 'lib/tn_pdf/report.rb', line 95

def table
  @table
end

#text_after_tableString, Array

Some text to be rendered after the report’s table. Can be used as a confirmation, a conclusion, an acceptance term etc. It accepts a normal string, that will be rendered using the settings on #font and #font_size, or an Array containing a String and a Hash, the last being an hash of options, as accepted by Prawn’s Document#text method.

Examples:

report.text_after_table = "Some text"
report.text_after_table = ["Some text", :size => 20]

Returns:

  • (String, Array)

    the current value of text_after_table



79
80
81
# File 'lib/tn_pdf/report.rb', line 79

def text_after_table
  @text_after_table
end

#text_before_tableString, Array

Some text to be rendered before the report’s table. Can be used as some kind of prelude/explanation/introduction etc. It accepts a normal string, that will be rendered using the settings on #font and #font_size, or an Array containing a String and a Hash, the last being an hash of options, as accepted by Prawn’s Document#text method.

Examples:

report.text_before_table = "Some text"
report.text_before_table = ["Some text", :size => 20]

Returns:

  • (String, Array)

    the current value of text_before_table



79
80
81
# File 'lib/tn_pdf/report.rb', line 79

def text_before_table
  @text_before_table
end

#top_marginDouble

Sets the document’s top margin. Accepts a Double value, defined in PDF points (1/72 inch) or a String “in” cm or mm, such as “1.5cm” and “50mm”.

Returns:

  • (Double)

    the current value of top_margin



79
80
81
# File 'lib/tn_pdf/report.rb', line 79

def top_margin
  @top_margin
end

Instance Method Details

Forwards the arguments to the underlying Table. Refer to Table#add_footer for a full description of the required syntax



178
179
180
# File 'lib/tn_pdf/report.rb', line 178

def add_table_footer(row=nil, &block)
  table.add_footer(row, &block)
end

#render(filename) ⇒ Object

Renders the report on filename. This method also calls render on the report’s members - #table, #page_header and #page_footer - which means that, on a straightforward report, the process is reduced to set up #table_columns and #record_collection, and then calling #render. This alone already does all the trick to generate the PDF.

Parameters:

  • filename (String, IO)

    The file on which the report will render



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/tn_pdf/report.rb', line 188

def render(filename)
  document_width = document.bounds.width
  page_header_position = [0, document.cursor]
  page_footer_position = [0, Configuration[:page_footer_height]]

  document.font(font)
  document.font_size(font_size)

  document.repeat :all do
    page_header.render(document, page_header_position)
    document.stroke_horizontal_rule
  end

  table_height = page_body_height

  document.bounding_box([0, document.cursor],
                        :width  => document.bounds.width) do
    table.render(table_height)
  end

  document.repeat :all do
    page_footer.render(document, page_footer_position)
  end

  document.render_file filename
end

#table_columnsArray

The underlying Table‘s collection of columns. Refer to Table#add_column for reference on the Array’s structure.

Returns:

  • (Array)


157
158
159
# File 'lib/tn_pdf/report.rb', line 157

def table_columns
  table.columns || Array.new
end

#table_columns=(columns) ⇒ Object

Forwards the received arguments to Table. Refer to Table#add_column for a reference on the valid structure. Be advised that calling this method resets the columns set previously, insted of just adding new ones.

Parameters:

Raises:

  • (ArgumentError)

    When the argument isn’t an Array



167
168
169
170
171
172
173
# File 'lib/tn_pdf/report.rb', line 167

def table_columns=(columns)
  raise ArgumentError unless columns.kind_of? Array
  table.reset_columns
  columns.each do |column|
    table.add_column column
  end
end