FastReportCliGenerator
This commit is contained in:
parent
5696edf0b0
commit
938050055f
@ -11,9 +11,9 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<Version>1.0</Version>
|
<Version>1.1</Version>
|
||||||
<AssemblyVersion>1.0</AssemblyVersion>
|
<AssemblyVersion>1.1.0.8</AssemblyVersion>
|
||||||
<FileVersion>1.0</FileVersion>
|
<FileVersion>1.1.0.8</FileVersion>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<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;
|
||||||
using FastReport.Data.JsonConnection;
|
using FastReport.Data.JsonConnection;
|
||||||
using FastReport.Export.Html;
|
using FastReport.Export.Html;
|
||||||
using FastReport.Export.PdfSimple;
|
using FastReport.Export.PdfSimple;
|
||||||
using System;
|
|
||||||
using System.Reflection;
|
|
||||||
|
|
||||||
class Program
|
class Program
|
||||||
{
|
{
|
||||||
static int Main(string[] args)
|
static int Main(string[] args)
|
||||||
{
|
{
|
||||||
// Si el usuario pasa --version, mostrar versión y salir
|
var options = ParseArgs(args);
|
||||||
if (args.Length == 1 && (args[0] == "--version" || args[0] == "-v"))
|
|
||||||
{
|
|
||||||
var version = Assembly.GetExecutingAssembly().GetName().Version;
|
|
||||||
Console.WriteLine($"FastReportCliGenerator version {version}");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
Console.WriteLine($"Running FastReportCliGenerator version {Assembly.GetExecutingAssembly().GetName().Version}");
|
// --version
|
||||||
if (args.Length < 4)
|
if (options.ContainsKey("version"))
|
||||||
{
|
{
|
||||||
Console.Error.WriteLine("Usage: FastReportCliGenerator <frxFile> <jsonFile> <outputFile> --format=<html|pdf>");
|
PrintVersion();
|
||||||
return 1;
|
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];
|
foreach (var conn in report.Dictionary.Connections)
|
||||||
string jsonPath = args[1];
|
{
|
||||||
string outputPath = args[2];
|
Console.WriteLine($"Connection found: {conn.ToString()} ({conn.GetType().Name})");
|
||||||
string formatArg = args[3].ToLower();
|
if (conn is JsonDataSourceConnection jdc)
|
||||||
|
{
|
||||||
// Extrae el formato
|
jsonConn = jdc;
|
||||||
string format = "html";
|
break;
|
||||||
if (formatArg.StartsWith("--format="))
|
}
|
||||||
{
|
}
|
||||||
format = formatArg.Split('=')[1];
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
if (jsonConn == null)
|
||||||
{
|
{
|
||||||
// Leer JSON en memoria
|
throw new Exception("No JSON data connection found in report.");
|
||||||
string jsonData = File.ReadAllText(jsonPath);
|
}
|
||||||
|
|
||||||
using var report = new Report();
|
|
||||||
|
|
||||||
// Cargar el informe
|
// 3) Cambia la cadena de conexión para usar el JSON real
|
||||||
report.Load(frxPath);
|
jsonConn.ConnectionString = builder.ToString();
|
||||||
|
|
||||||
// 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (jsonConn == null)
|
// 4) Crea las tablas internas basadas en ese JSON
|
||||||
{
|
jsonConn.CreateAllTables();
|
||||||
throw new Exception("No JSON data connection found in report.");
|
|
||||||
}
|
|
||||||
|
|
||||||
// 3) Cambia la cadena de conexión para usar el JSON real
|
// 5) Preparar el reporte
|
||||||
// "Json=" es la clave usada por FastReport para indicar JSON embebido
|
report.Prepare();
|
||||||
jsonConn.ConnectionString = $"Json={jsonData}";
|
|
||||||
|
|
||||||
// 4) Crea las tablas internas basadas en ese JSON
|
if (format == "pdf")
|
||||||
jsonConn.CreateAllTables();
|
{
|
||||||
|
// 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
|
return 0;
|
||||||
report.Prepare();
|
}
|
||||||
|
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;
|
// ----------------------------
|
||||||
}
|
// Helpers
|
||||||
catch (Exception ex)
|
// ----------------------------
|
||||||
{
|
|
||||||
Console.Error.WriteLine("Error generating report: " + ex.Message);
|
static Dictionary<string, string> ParseArgs(string[] args)
|
||||||
return 2;
|
{
|
||||||
}
|
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