185 lines
3.9 KiB
C#
185 lines
3.9 KiB
C#
using System.Reflection;
|
|
|
|
using FastReport;
|
|
using FastReport.Data.JsonConnection;
|
|
using FastReport.Export.Html;
|
|
using FastReport.Export.PdfSimple;
|
|
|
|
class Program
|
|
{
|
|
static int Main(string[] args)
|
|
{
|
|
var options = ParseArgs(args);
|
|
|
|
// --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);
|
|
|
|
|
|
foreach (var conn in report.Dictionary.Connections)
|
|
{
|
|
Console.WriteLine($"Connection found: {conn.ToString()} ({conn.GetType().Name})");
|
|
if (conn is JsonDataSourceConnection jdc)
|
|
{
|
|
jsonConn = jdc;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (jsonConn == null)
|
|
{
|
|
throw new Exception("No JSON data connection found in report.");
|
|
}
|
|
|
|
|
|
|
|
// 3) Cambia la cadena de conexión para usar el JSON real
|
|
jsonConn.ConnectionString = builder.ToString();
|
|
|
|
// 4) Crea las tablas internas basadas en ese JSON
|
|
jsonConn.CreateAllTables();
|
|
|
|
// 5) Preparar el reporte
|
|
report.Prepare();
|
|
|
|
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}");
|
|
}
|
|
|
|
report.Dispose();
|
|
|
|
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
|
|
");
|
|
}
|
|
}
|
|
|
|
|