FastReportCliGenerator
This commit is contained in:
parent
5696edf0b0
commit
938050055f
@ -11,9 +11,9 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<Version>1.0</Version>
|
||||
<AssemblyVersion>1.0</AssemblyVersion>
|
||||
<FileVersion>1.0</FileVersion>
|
||||
<Version>1.1</Version>
|
||||
<AssemblyVersion>1.1.0.8</AssemblyVersion>
|
||||
<FileVersion>1.1.0.8</FileVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
3
tools/fastreport-cli/FastReportCliGenerator.slnx
Normal file
3
tools/fastreport-cli/FastReportCliGenerator.slnx
Normal file
@ -0,0 +1,3 @@
|
||||
<Solution>
|
||||
<Project Path="FastReportCliGenerator.csproj" />
|
||||
</Solution>
|
||||
@ -1,106 +1,182 @@
|
||||
using System.Reflection;
|
||||
|
||||
using FastReport;
|
||||
using FastReport.Data.JsonConnection;
|
||||
using FastReport.Export.Html;
|
||||
using FastReport.Export.PdfSimple;
|
||||
using System;
|
||||
using System.Reflection;
|
||||
|
||||
class Program
|
||||
{
|
||||
static int Main(string[] args)
|
||||
{
|
||||
// Si el usuario pasa --version, mostrar versión y salir
|
||||
if (args.Length == 1 && (args[0] == "--version" || args[0] == "-v"))
|
||||
{
|
||||
var version = Assembly.GetExecutingAssembly().GetName().Version;
|
||||
Console.WriteLine($"FastReportCliGenerator version {version}");
|
||||
return 0;
|
||||
}
|
||||
static int Main(string[] args)
|
||||
{
|
||||
var options = ParseArgs(args);
|
||||
|
||||
Console.WriteLine($"Running FastReportCliGenerator version {Assembly.GetExecutingAssembly().GetName().Version}");
|
||||
if (args.Length < 4)
|
||||
{
|
||||
Console.Error.WriteLine("Usage: FastReportCliGenerator <frxFile> <jsonFile> <outputFile> --format=<html|pdf>");
|
||||
return 1;
|
||||
}
|
||||
// --version
|
||||
if (options.ContainsKey("version"))
|
||||
{
|
||||
PrintVersion();
|
||||
return 0;
|
||||
}
|
||||
|
||||
// --help o sin argumentos
|
||||
if (options.ContainsKey("help") || options.Count == 0)
|
||||
{
|
||||
PrintHelp();
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Validación de obligatorios
|
||||
if (!options.TryGetValue("template", out var frxPath) ||
|
||||
!options.TryGetValue("data", out var jsonPath) ||
|
||||
!options.TryGetValue("output", out var outputPath))
|
||||
{
|
||||
Console.Error.WriteLine("❌ Missing required arguments.\n");
|
||||
PrintHelp();
|
||||
return 1;
|
||||
}
|
||||
|
||||
var format = options.TryGetValue("format", out var f)
|
||||
? f.ToLowerInvariant()
|
||||
: "pdf";
|
||||
|
||||
try
|
||||
{
|
||||
// Leer JSON en memoria
|
||||
string jsonData = File.ReadAllText(jsonPath);
|
||||
|
||||
// 2) Busca la conexión JSON ya definida
|
||||
var builder = new JsonDataSourceConnectionStringBuilder();
|
||||
builder.Json = jsonData;
|
||||
|
||||
JsonDataSourceConnection jsonConn = new JsonDataSourceConnection();
|
||||
|
||||
using var report = new Report();
|
||||
|
||||
// Cargar el informe
|
||||
report.Load(frxPath);
|
||||
|
||||
|
||||
string frxPath = args[0];
|
||||
string jsonPath = args[1];
|
||||
string outputPath = args[2];
|
||||
string formatArg = args[3].ToLower();
|
||||
|
||||
// Extrae el formato
|
||||
string format = "html";
|
||||
if (formatArg.StartsWith("--format="))
|
||||
{
|
||||
format = formatArg.Split('=')[1];
|
||||
}
|
||||
foreach (var conn in report.Dictionary.Connections)
|
||||
{
|
||||
Console.WriteLine($"Connection found: {conn.ToString()} ({conn.GetType().Name})");
|
||||
if (conn is JsonDataSourceConnection jdc)
|
||||
{
|
||||
jsonConn = jdc;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// Leer JSON en memoria
|
||||
string jsonData = File.ReadAllText(jsonPath);
|
||||
if (jsonConn == null)
|
||||
{
|
||||
throw new Exception("No JSON data connection found in report.");
|
||||
}
|
||||
|
||||
using var report = new Report();
|
||||
|
||||
|
||||
// Cargar el informe
|
||||
report.Load(frxPath);
|
||||
|
||||
// 2) Busca la conexión JSON ya definida
|
||||
JsonDataSourceConnection? jsonConn = null;
|
||||
foreach (var conn in report.Dictionary.Connections)
|
||||
{
|
||||
if (conn is JsonDataSourceConnection jdc)
|
||||
{
|
||||
jsonConn = jdc;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// 3) Cambia la cadena de conexión para usar el JSON real
|
||||
jsonConn.ConnectionString = builder.ToString();
|
||||
|
||||
if (jsonConn == null)
|
||||
{
|
||||
throw new Exception("No JSON data connection found in report.");
|
||||
}
|
||||
// 4) Crea las tablas internas basadas en ese JSON
|
||||
jsonConn.CreateAllTables();
|
||||
|
||||
// 3) Cambia la cadena de conexión para usar el JSON real
|
||||
// "Json=" es la clave usada por FastReport para indicar JSON embebido
|
||||
jsonConn.ConnectionString = $"Json={jsonData}";
|
||||
// 5) Preparar el reporte
|
||||
report.Prepare();
|
||||
|
||||
// 4) Crea las tablas internas basadas en ese JSON
|
||||
jsonConn.CreateAllTables();
|
||||
if (format == "pdf")
|
||||
{
|
||||
// Exportar a PDF
|
||||
using var pdfExport = new PDFSimpleExport();
|
||||
using var fs = new FileStream(outputPath, FileMode.Create);
|
||||
pdfExport.Export(report, fs);
|
||||
Console.WriteLine($"Generated PDF: {outputPath}");
|
||||
}
|
||||
else
|
||||
{
|
||||
// Exportar a HTML
|
||||
using var htmlExport = new HTMLExport
|
||||
{
|
||||
EmbedPictures = true,
|
||||
SinglePage = true,
|
||||
SubFolder = false
|
||||
};
|
||||
using var fs = new FileStream(outputPath, FileMode.Create);
|
||||
report.Export(htmlExport, fs);
|
||||
Console.WriteLine($"Generated HTML: {outputPath}");
|
||||
}
|
||||
|
||||
// 5) Preparar el reporte
|
||||
report.Prepare();
|
||||
return 0;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.Error.WriteLine("Error generating report: " + ex.Message);
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
||||
if (format == "pdf")
|
||||
{
|
||||
// Exportar a PDF
|
||||
using var pdfExport = new PDFSimpleExport();
|
||||
using var fs = new FileStream(outputPath, FileMode.Create);
|
||||
pdfExport.Export(report, fs);
|
||||
Console.WriteLine($"Generated PDF: {outputPath}");
|
||||
}
|
||||
else
|
||||
{
|
||||
// Exportar a HTML
|
||||
using var htmlExport = new HTMLExport
|
||||
{
|
||||
EmbedPictures = true,
|
||||
SinglePage = true,
|
||||
SubFolder = false
|
||||
};
|
||||
using var fs = new FileStream(outputPath, FileMode.Create);
|
||||
report.Export(htmlExport, fs);
|
||||
Console.WriteLine($"Generated HTML: {outputPath}");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.Error.WriteLine("Error generating report: " + ex.Message);
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
// ----------------------------
|
||||
// Helpers
|
||||
// ----------------------------
|
||||
|
||||
static Dictionary<string, string> ParseArgs(string[] args)
|
||||
{
|
||||
var dict = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
foreach (var arg in args)
|
||||
{
|
||||
if (!arg.StartsWith("--"))
|
||||
continue;
|
||||
|
||||
var parts = arg.Substring(2).Split('=', 2);
|
||||
|
||||
if (parts.Length == 1)
|
||||
dict[parts[0]] = "true"; // flags: --help, --version
|
||||
else
|
||||
dict[parts[0]] = parts[1];
|
||||
}
|
||||
|
||||
return dict;
|
||||
}
|
||||
|
||||
static void PrintVersion()
|
||||
{
|
||||
var version = Assembly.GetExecutingAssembly().GetName().Version;
|
||||
Console.WriteLine();
|
||||
Console.WriteLine($"FastReportCliGenerator version {version}");
|
||||
}
|
||||
|
||||
static void PrintHelp()
|
||||
{
|
||||
PrintVersion();
|
||||
Console.WriteLine(@"
|
||||
Usage:
|
||||
FastReportCliGenerator \
|
||||
--template=<file.frx> \
|
||||
--data=<data.json> \
|
||||
--output=<output.html|pdf> \
|
||||
[--format=html|pdf]
|
||||
|
||||
Options:
|
||||
--template Path to FRX template (required)
|
||||
--data Path to JSON data file (required)
|
||||
--output Output file path (required)
|
||||
--format html or pdf (default)
|
||||
--version Show version and exit
|
||||
--help Show this help
|
||||
|
||||
Examples:
|
||||
FastReportCliGenerator \
|
||||
--template=invoice.frx \
|
||||
--data=invoice.json \
|
||||
--output=invoice.html
|
||||
|
||||
FastReportCliGenerator \
|
||||
--template=invoice.frx \
|
||||
--data=invoice.json \
|
||||
--output=invoice.pdf \
|
||||
--format=pdf
|
||||
");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue
Block a user