Class: Report

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

Overview

Generates reports via the RdlCmd .NET command-line tool.

Requires a .NET runtime on the host. For self-contained or AOT builds that need no runtime, use ReportAot instead.

Usage:

require 'majorsilence_reporting/report'
# or: require 'majorsilence_reporting'  (loads all three wrappers)

rpt = Report.new('/path/to/report.rdl', '/path/to/RdlCmd.dll', 'dotnet')
rpt.set_connection_string('Data Source=/path/to/db.sqlite')
rpt.set_parameter('Country', 'Germany')
rpt.export('pdf', '/tmp/output.pdf')
data = rpt.export_to_memory('pdf')

Supported export types: "pdf", "csv", "xlsx", "xml", "rtf", "tif", "html"

Constant Summary collapse

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

Instance Method Summary collapse

Constructor Details

#initialize(report_path, rdl_cmd_path, path_to_dotnet = nil) ⇒ Report

Returns a new instance of Report.



26
27
28
29
30
31
32
# File 'lib/majorsilence_reporting/report.rb', line 26

def initialize(report_path, rdl_cmd_path, path_to_dotnet = nil)
  @report_path       = report_path
  @rdl_cmd_path      = rdl_cmd_path
  @path_to_dotnet    = path_to_dotnet
  @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", "xml", "rtf", "tif", "html". Defaults to "pdf". export_path - destination file path



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

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 = []
  cmd << @path_to_dotnet if @path_to_dotnet
  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/rtf; UTF-8 String for text formats. type - output format (defaults to "pdf" if unrecognised)



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/majorsilence_reporting/report.rb', line 79

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.



42
43
44
# File 'lib/majorsilence_reporting/report.rb', line 42

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)



37
38
39
# File 'lib/majorsilence_reporting/report.rb', line 37

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