Ruby Generate Reports - majorsilence/Reporting GitHub Wiki
You can download the ruby wrapper for RdlCmd from the release page or you can download it from the master branch.
- https://github.com/majorsilence/My-FyiReporting/releases
- https://github.com/majorsilence/My-FyiReporting/tree/master/LanguageWrappers/ruby
- Examples of using ruby are in the Examples folder in the above link.
Tested with ruby-3.4.2.
The recommend way to use this code is to copy the report.rb file into your project. It is a thin layer and rarely changes.
Details
report.rb
- Class: Report(path_to_rdl_file, path_to_rdlcmd, path_to_dotnet)
- Functions:
- export(export_type, save_path)
- export_type: "pdf", "csv", "xslx", "xml", "rtf", "tif", "html"
- If type does not match it will default to pdf
- save_path: the directory path and file name that will be generated
- export_type: "pdf", "csv", "xslx", "xml", "rtf", "tif", "html"
- export_to_memory(export_type)
- export type: "pdf", "csv", "xslx", "xml", "rtf", "tif", "html"
- If type does not match it will default to pdf
- export(export_type, save_path)
Examples
The examples demonstrate setup and generating a report.
basic example
Example of calling a basic report and generating the file test1.pdf.
#!/usr/bin/env ruby
$LOAD_PATH << '../'
require 'report'
# SETUP
current_directory = File.dirname(File.expand_path(__FILE__))
db_path = File.expand_path(File.join(current_directory, '..', '..', '..', 'Examples', 'northwindEF.db'))
report_path = File.expand_path(File.join(current_directory, '..', '..', '..', 'Examples', 'SqliteExamples', 'SimpleTest1.rdl'))
if Gem.win_platform?
# If on Windows, we do not need to set the path to dotnet, RdlCmd can be run directly
path_to_dotnet = nil
path_to_rdlcmd = File.expand_path(File.join(current_directory, '..', '..', '..', 'RdlCmd', 'bin', 'Debug', 'net8.0', 'RdlCmd.exe'))
else
# dotnet is required to run RdlCmd
path_to_dotnet = 'dotnet'
path_to_rdlcmd = File.expand_path(File.join(current_directory, '..', '..', '..', 'RdlCmd', 'bin', 'Debug', 'net8.0', 'RdlCmd.dll'))
end
path_to_rdlcmd = File.absolute_path(path_to_rdlcmd)
output_directory = File.join(current_directory, 'output')
Dir.mkdir(output_directory) unless Dir.exist?(output_directory)
# REPORT EXAMPLE
rpt = Report.new(report_path, path_to_rdlcmd, path_to_dotnet)
rpt.set_connection_string('Data Source=' + db_path)
rpt.export("pdf", File.join(output_directory, 'test1.pdf'))
Example generating setting parameters and a connection string
#!/usr/bin/env ruby
$LOAD_PATH << '../'
require 'report'
# SETUP
current_directory = File.dirname(File.expand_path(__FILE__))
db_path = File.expand_path(File.join(current_directory, '..', '..', '..', 'Examples', 'northwindEF.db'))
report_path = File.expand_path(File.join(current_directory, '..', '..', '..', 'Examples', 'SqliteExamples', 'SimpleTest3WithParameters.rdl'))
if Gem.win_platform?
# If on Windows, we do not need to set the path to dotnet, RdlCmd can be run directly
path_to_dotnet = nil
path_to_rdlcmd = File.expand_path(File.join(current_directory, '..', '..', '..', 'RdlCmd', 'bin', 'Debug', 'net8.0', 'RdlCmd.exe'))
else
# dotnet is required to run RdlCmd
path_to_dotnet = 'dotnet'
path_to_rdlcmd = File.expand_path(File.join(current_directory, '..', '..', '..', 'RdlCmd', 'bin', 'Debug', 'net8.0', 'RdlCmd.dll'))
end
output_directory = File.join(current_directory, 'output')
Dir.mkdir(output_directory) unless Dir.exist?(output_directory)
# REPORT EXAMPLE
rpt = Report.new(report_path, path_to_rdlcmd, path_to_dotnet)
rpt.set_parameter("TestParam1", 'I am a parameter value.')
rpt.set_parameter("TestParam2", 'The second parameter.')
rpt.set_connection_string('Data Source=' + db_path)
rpt.export("pdf", File.join(output_directory, 'test2-parameters.pdf'))
Generate report and return the data
This example generates a report and loads it into memory using the export_to_memory function. It prints the data to the console/web.
#!/usr/bin/env ruby
$LOAD_PATH << '../'
require 'report'
# SETUP
current_directory = File.dirname(File.expand_path(__FILE__))
db_path = File.expand_path(File.join(current_directory, '..', '..', '..', 'Examples', 'northwindEF.db'))
report_path = File.expand_path(File.join(current_directory, '..', '..', '..', 'Examples', 'SqliteExamples', 'SimpleTest1.rdl'))
if Gem.win_platform?
# If on Windows, we do not need to set the path to dotnet, RdlCmd can be run directly
path_to_dotnet = nil
path_to_rdlcmd = File.expand_path(File.join(current_directory, '..', '..', '..', 'RdlCmd', 'bin', 'Debug', 'net8.0', 'RdlCmd.exe'))
else
# dotnet is required to run RdlCmd
path_to_dotnet = 'dotnet'
path_to_rdlcmd = File.expand_path(File.join(current_directory, '..', '..', '..', 'RdlCmd', 'bin', 'Debug', 'net8.0', 'RdlCmd.dll'))
end
output_directory = File.join(current_directory, 'output')
Dir.mkdir(output_directory) unless Dir.exist?(output_directory)
# REPORT EXAMPLE
rpt = Report.new(report_path, path_to_rdlcmd, path_to_dotnet)
rpt.set_connection_string('Data Source=' + db_path)
data = rpt.export_to_memory("pdf")
# show the data in the console
print(data)
# or save it to a file
File.open(File.join(output_directory, 'test3-streaming.pdf'), 'wb') do |file|
file.write(data)
end
# This is where you output data on your site using wsgi, cgi, or whatever python framework/library you are using