Class: MRTExport
- Inherits:
-
Object
- Object
- MRTExport
- Defined in:
- lib/mrtexport.rb
Instance Method Summary collapse
- #generate_pdf ⇒ Object
- #get_value_from_db(name) ⇒ Object
-
#initialize(params) ⇒ MRTExport
constructor
A new instance of MRTExport.
- #initialize_database_connection ⇒ Object
- #render_background(node) ⇒ Object
- #render_backgrounds(page) ⇒ Object
- #render_band(band) ⇒ Object
- #render_image(node) ⇒ Object
- #render_images(band) ⇒ Object
- #render_text(node) ⇒ Object
- #render_texts(page) ⇒ Object
Constructor Details
#initialize(params) ⇒ MRTExport
Returns a new instance of MRTExport.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/mrtexport.rb', line 15 def initialize(params) @report_file = params[:report_file] @output_file = params[:output_file] @export_format = params[:export_format] @replacements = params[:replacements] puts "[+] Compiling new report: #{@report_file}" @xml_doc = Nokogiri::XML(File.open(@report_file)) @sql_connections = DatabaseDatabaseBuilder.get_database_database(@xml_doc) @data_sources = DatabaseDatabaseBuilder.get_data_sources(@xml_doc) puts @sql_connections initialize_database_connection if @sql_connections.length > 0 @y_off = 0 generate_pdf end |
Instance Method Details
#generate_pdf ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/mrtexport.rb', line 78 def generate_pdf @pdf = Prawn::Document.new @xml_doc.xpath("//Pages/*").each do |page| # List bands bands puts "[*] listing report bands" page.xpath("./Components/*[contains(., 'Band')]").each do |band| puts " - #{band.name}" end # render out of band stuff page.xpath("./Components/*[not(contains(.,'Band'))]").each do |component| if component.name.include?("Text") render_background(component) render_text(component) elsif component.name.include?("Image") render_image(component) end end # but we want to do bands in a specific order! # Start with header band page.xpath("./Components/*[contains(., 'ReportTitleBand')]").each do |band| puts "[*] Doing band: #{band.name}, ref: #{band.attribute("Ref")}" render_band(band) end page.xpath("./Components/*[contains(., 'HeaderBand')]").each do |band| puts "[*] Doing band: #{band.name}, ref: #{band.attribute("Ref")}" render_band(band) end page.xpath("./Components/*[contains(., 'DataBand')]").each do |band| puts "[*] Doing band: #{band.name}, ref: #{band.attribute("Ref")}" #render_band(band) data_band_renderer = DataBandRenderer.new( band, @pdf, @sql_connection, @replacements, @data_sources, @y_off ) @y_off = data_band_renderer.render end page.xpath("./Components/*[contains(., 'FooterBand')]").each do |band| puts "[*] Doing band: #{band.name}, ref: #{band.attribute("Ref")}" render_band(band) end end @pdf.render_file @output_file end |
#get_value_from_db(name) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/mrtexport.rb', line 53 def get_value_from_db(name) name.sub! "{", "" name.sub! "}", "" query_parts = name.split(".") query_name = query_parts[0] query_field = query_parts[1] sql = @data_sources[query_name]["sql"] @replacements.each do |key, val| sql.sub!("{#{key}}", val) end text = "" rs = @sql_connection.query sql rs.each do |r| text = r[query_field] text = Util.number_format(text) if Util.is_number?(text) end return text end |
#initialize_database_connection ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/mrtexport.rb', line 37 def initialize_database_connection sql_connection_string = @sql_connections["Localhost"] sql_connection_array = {} sql_connection_string.split(";").each do |x| sql_connection_array[x.split("=")[0]] = x.split("=")[1] end @sql_connection = Mysql2::Client.new( :host => sql_connection_array["Server"], :username => sql_connection_array["User"], :password => sql_connection_array["Password"], :database => sql_connection_array["Database"] ) end |
#render_background(node) ⇒ Object
178 179 180 181 182 183 184 185 186 187 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 |
# File 'lib/mrtexport.rb', line 178 def render_background(node) return if node.name == "Name" if node.name.start_with? "Text" x, y, w, h = Stylist.get_dimensions(node, @y_off) hue = node.xpath("Brush").text if hue != "Transparent" hue = hue.sub("[","").sub("]","").split(":") hex_hue = hue[0].to_i.to_s(16) << hue[1].to_i.to_s(16) << hue[2].to_i.to_s(16) @pdf.fill_color hex_hue @pdf.fill_rectangle [x,y], w, h end if node.xpath("Border").text != "" border = node.xpath("Border").text border_bits = border.split(";") sides = border_bits[0] if sides != "None" @pdf.stroke_color = Stylist.mrt_colour_to_hex(border_bits[1]) @pdf.line_width = 1 @pdf.line [x, y], [x+w, y] if sides.include? "Top" @pdf.line [x, y-h], [x+w, y-h] if sides.include? "Bottom" @pdf.line [x, y], [x, y-h] if sides.include? "Left" @pdf.line [x+w, y], [x+w, y-h] if sides.include? "Right" @pdf.stroke end end end end |
#render_backgrounds(page) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/mrtexport.rb', line 140 def render_backgrounds(page) page.xpath("Components/*[contains(.,'Text')]").each do |node| next if node.name == "Name" if node.name.start_with? "Text" x, y, w, h = Stylist.get_dimensions(node, @y_off) hue = node.xpath("Brush").text if hue != "Transparent" hue = hue.sub("[","").sub("]","").split(":") hex_hue = hue[0].to_i.to_s(16) << hue[1].to_i.to_s(16) << hue[2].to_i.to_s(16) @pdf.fill_color hex_hue @pdf.fill_rectangle [x,y], w, h end if node.xpath("Border").text != "" border = node.xpath("Border").text border_bits = border.split(";") sides = border_bits[0] if sides != "None" @pdf.stroke_color = Stylist.mrt_colour_to_hex(border_bits[1]) @pdf.line_width = 1 @pdf.line [x, y], [x+w, y] if sides.include? "Top" @pdf.line [x, y-h], [x+w, y-h] if sides.include? "Bottom" @pdf.line [x, y], [x, y-h] if sides.include? "Left" @pdf.line [x+w, y], [x+w, y-h] if sides.include? "Right" @pdf.stroke end end end end end |
#render_band(band) ⇒ Object
134 135 136 137 138 |
# File 'lib/mrtexport.rb', line 134 def render_band(band) render_backgrounds(band) render_images(band) render_texts(band) end |
#render_image(node) ⇒ Object
220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/mrtexport.rb', line 220 def render_image(node) x, y, w, h = Stylist.get_dimensions(node, @y_off) data = Base64::decode64(node.xpath("Image").text) File.open("/tmp/mrt_to_pdf_img",'wb') { |file| file.puts data } @pdf.image "/tmp/mrt_to_pdf_img", :at => [x, 0 + h], :width => w File.delete("/tmp/mrt_to_pdf_img") end |
#render_images(band) ⇒ Object
214 215 216 217 218 |
# File 'lib/mrtexport.rb', line 214 def render_images(band) band.xpath("Components/*[contains(.,'Image')]").each do |node| render_image(node) end end |
#render_text(node) ⇒ Object
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/mrtexport.rb', line 242 def render_text(node) last_y_off = @y_off return if node.name == "Name" if node.name.start_with? "Text" x, y, w, h = Stylist.get_dimensions(node, @y_off) # Text text = node.xpath("Text").text # SQL replacements if text.include?("{") text.gsub!(/{.*}/) { |m| get_value_from_db(m) } end # Do painting @pdf.fill_color = Stylist.get_text_colour(node) @pdf.text_box text, :at => [x + 1, y - 1], :height => h, :width => w, :align => Stylist.get_horizontal_align(node), :valign => Stylist.get_vertical_align(node), :size => Stylist.get_font_size(node), :font => Stylist.get_font_face(node), :style => Stylist.get_font_style(node) last_y_off = (740 - y) if (740 - y) > @y_off end return last_y_off end |
#render_texts(page) ⇒ Object
232 233 234 235 236 237 238 239 240 |
# File 'lib/mrtexport.rb', line 232 def render_texts(page) last_y_off = @y_off page.xpath("Components/*[contains(.,'Text')]").each do |node| last_y_off = render_text(node) end @y_off = last_y_off end |