Class: TnPDF::Report
- Inherits:
-
Object
- Object
- TnPDF::Report
- 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.
Instance Attribute Summary collapse
-
#bottom_margin ⇒ Double
Sets the document’s bottom margin.
-
#document ⇒ Prawn::Document
readonly
The underlying Prawn::Document.
-
#font ⇒ String
The default font to be used on the report.
-
#font_size ⇒ Fixnum
The default report font size.
-
#images_path ⇒ String
The path from where we will search for the requested images.
-
#left_margin ⇒ Double
Sets the document’s left margin.
-
#page_footer ⇒ PageSection
readonly
The underlying PageSection that represents the report pages’ footers.
-
#page_header ⇒ PageSection
readonly
The underlying PageSection that represents the report pages’ headers.
-
#page_layout ⇒ Symbol
The resulting pages’ layout.
-
#page_size ⇒ String
The report’s page size, in paper sizes such as “A4” and “Letter”.
-
#record_collection ⇒ Array
The underlying Table‘s collection of objects.
-
#right_margin ⇒ Double
Sets the document’s right margin.
-
#table ⇒ Table
readonly
The underlying Table.
-
#text_after_table ⇒ String, Array
Some text to be rendered after the report’s table.
-
#text_before_table ⇒ String, Array
Some text to be rendered before the report’s table.
-
#top_margin ⇒ Double
Sets the document’s top margin.
Instance Method Summary collapse
-
#add_table_footer(row = nil, &block) ⇒ Object
Forwards the arguments to the underlying Table.
-
#initialize(properties = {}) ⇒ Report
constructor
A new instance of Report.
-
#render(filename) ⇒ Object
Renders the report on filename.
-
#table_columns ⇒ Array
The underlying Table‘s collection of columns.
-
#table_columns=(columns) ⇒ Object
Forwards the received arguments to Table.
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_margin ⇒ Double
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”.
79 80 81 |
# File 'lib/tn_pdf/report.rb', line 79 def bottom_margin @bottom_margin end |
#document ⇒ Prawn::Document (readonly)
The underlying Prawn::Document. Direct manipulation is highly disencouraged, except for querying information.
218 219 220 |
# File 'lib/tn_pdf/report.rb', line 218 def document @document end |
#font ⇒ String
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
79 80 81 |
# File 'lib/tn_pdf/report.rb', line 79 def font @font end |
#font_size ⇒ Fixnum
The default report font size. In the usual “points” unit.
79 80 81 |
# File 'lib/tn_pdf/report.rb', line 79 def font_size @font_size end |
#images_path ⇒ String
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”
79 80 81 |
# File 'lib/tn_pdf/report.rb', line 79 def images_path @images_path end |
#left_margin ⇒ Double
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”.
79 80 81 |
# File 'lib/tn_pdf/report.rb', line 79 def left_margin @left_margin end |
#page_footer ⇒ PageSection (readonly)
The underlying PageSection that represents the report pages’ footers. Direct manipulation is disencouraged.
89 90 91 |
# File 'lib/tn_pdf/report.rb', line 89 def @page_footer end |
#page_header ⇒ PageSection (readonly)
The underlying PageSection that represents the report pages’ headers. Direct manipulation is disencouraged.
84 85 86 |
# File 'lib/tn_pdf/report.rb', line 84 def page_header @page_header end |
#page_layout ⇒ Symbol
The resulting pages’ layout. Must be :landscape or :portrait
79 80 81 |
# File 'lib/tn_pdf/report.rb', line 79 def page_layout @page_layout end |
#page_size ⇒ String
The report’s page size, in paper sizes such as “A4” and “Letter”. Supports as much as much paper sizes as Prawn does.
79 80 81 |
# File 'lib/tn_pdf/report.rb', line 79 def page_size @page_size end |
#record_collection ⇒ Array
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.
107 108 109 |
# File 'lib/tn_pdf/report.rb', line 107 def record_collection @record_collection end |
#right_margin ⇒ Double
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”.
79 80 81 |
# File 'lib/tn_pdf/report.rb', line 79 def right_margin @right_margin end |
#table ⇒ Table (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).
95 96 97 |
# File 'lib/tn_pdf/report.rb', line 95 def table @table end |
#text_after_table ⇒ String, 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.
79 80 81 |
# File 'lib/tn_pdf/report.rb', line 79 def text_after_table @text_after_table end |
#text_before_table ⇒ String, 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.
79 80 81 |
# File 'lib/tn_pdf/report.rb', line 79 def text_before_table @text_before_table end |
#top_margin ⇒ Double
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”.
79 80 81 |
# File 'lib/tn_pdf/report.rb', line 79 def top_margin @top_margin end |
Instance Method Details
#add_table_footer(row = nil, &block) ⇒ Object
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 (row=nil, &block) table.(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.
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] = [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 .render(document, ) end document.render_file filename end |
#table_columns ⇒ Array
The underlying Table‘s collection of columns. Refer to Table#add_column for reference on the Array’s structure.
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.
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 |