107 lines
3.3 KiB
C#
107 lines
3.3 KiB
C#
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;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
|
|
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];
|
|
}
|
|
|
|
try
|
|
{
|
|
// Leer JSON en memoria
|
|
string jsonData = File.ReadAllText(jsonPath);
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
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
|
|
// "Json=" es la clave usada por FastReport para indicar JSON embebido
|
|
jsonConn.ConnectionString = $"Json={jsonData}";
|
|
|
|
// 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}");
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.Error.WriteLine("Error generating report: " + ex.Message);
|
|
return 2;
|
|
}
|
|
}
|
|
}
|