Class: ReportAot

Inherits:
Object
  • Object
show all
Defined in:
lib/majorsilence_reporting/report_aot.rb

Overview

Report class for use with the AOT or self-contained RdlCmd binary.

Unlike Report, this class does not accept a path_to_dotnet argument. rdl_cmd_path must point to the native AOT or self-contained executable (e.g. RdlCmd on Linux/macOS, RdlCmd.exe on Windows) — no .NET runtime is required.

Usage:

require 'majorsilence_reporting/report_aot'

rpt = ReportAot.new('/path/to/report.rdl', '/path/to/RdlCmd')
rpt.set_parameter('Country', 'Germany')
rpt.set_connection_string('Data Source=myserver.db')
rpt.export('pdf', '/tmp/output.pdf')

Supported export types: "pdf", "csv", "xlsx", "xlsx_table", "xml", "rtf", "tif", "tifb", "html", "mht".

Constant Summary collapse

VALID_TYPES =
%w[pdf csv xlsx xlsx_table xml rtf tif tifb html mht].freeze
BINARY_TYPES =
%w[pdf tif tifb rtf xlsx xlsx_table].freeze

Instance Method Summary collapse

Constructor Details

#initialize(report_path, rdl_cmd_path) ⇒ ReportAot

Returns a new instance of ReportAot.



25
26
27
28
29
30
# File 'lib/majorsilence_reporting/report_aot.rb', line 25

def initialize(report_path, rdl_cmd_path)
  @report_path       = report_path
  @rdl_cmd_path      = rdl_cmd_path
  @parameters        = {}
  @connection_string = nil
end

Instance Method Details

#export(type, export_path) ⇒ Object

Render the report and save it to export_path. type - output format: "pdf", "csv", "xlsx", "xlsx_table", "xml", "rtf", "tif", "tifb", "html", "mht". Defaults to "pdf". export_path - destination file path



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/majorsilence_reporting/report_aot.rb', line 48

def export(type, export_path)
  type = 'pdf' unless VALID_TYPES.include?(type)

  tmp         = Tempfile.new('majorsilencereporting')
  tmp_path    = tmp.path
  tmp_dir     = File.dirname(tmp_path)
  tmp.close
  FileUtils.cp(@report_path, tmp_path)

  rdl_arg = "/f#{tmp_path}"
  @parameters.each_with_index do |(key, value), i|
    rdl_arg += (i.zero? ? '?' : '&') + "#{key}=#{value}"
  end

  cmd = [@rdl_cmd_path, rdl_arg, "/t#{type}", "/o#{tmp_dir}"]
  cmd << "/c#{@connection_string}" if @connection_string

  system(*cmd) or raise "RdlCmd failed (exit #{$?.exitstatus})"

  tmp_out = File.join(tmp_dir, File.basename(tmp_path) + ".#{type}")
  FileUtils.cp(tmp_out, export_path)
  File.delete(tmp_path)
  File.delete(tmp_out)
end

#export_to_memory(type) ⇒ Object

Render the report and return the output. Returns binary String for pdf/tif/tifb/rtf/xlsx; UTF-8 String for text formats. type - output format (defaults to "pdf" if unrecognised)



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/majorsilence_reporting/report_aot.rb', line 76

def export_to_memory(type)
  type = 'pdf' unless VALID_TYPES.include?(type)

  tmp      = Tempfile.new('majorsilencereporting')
  tmp_path = tmp.path
  tmp.close
  tmp.unlink

  export(type, tmp_path)

  data = BINARY_TYPES.include?(type) ? File.binread(tmp_path) : File.read(tmp_path, encoding: 'UTF-8')
  File.delete(tmp_path) if File.exist?(tmp_path)
  data
end

#set_connection_string(connection_string) ⇒ Object

Override the connection string defined in the RDL.



40
41
42
# File 'lib/majorsilence_reporting/report_aot.rb', line 40

def set_connection_string(connection_string)
  @connection_string = connection_string
end

#set_parameter(name, value) ⇒ Object

Set a report parameter value. name - parameter name as declared in the RDL value - parameter value (string)



35
36
37
# File 'lib/majorsilence_reporting/report_aot.rb', line 35

def set_parameter(name, value)
  @parameters[name] = value
end