Add Logo Resources + Convert
All checks were successful
CI/CD Pipeline / Build and Deploy with Docker Compose (push) Successful in 8m15s

This commit is contained in:
Leandro Hernan Rojas 2025-05-16 19:20:31 -03:00
parent b401941fe1
commit a0a87ab306
6 changed files with 96 additions and 25 deletions

View File

@ -1,31 +1,83 @@
using Documents.Interfaces; using Documents.Interfaces;
using Documents.Models; using Documents.Models;
using Domain.Dtos;
using System.Reflection; using System.Reflection;
using System.Text; using System.Text;
using Transversal.Interfaces; using Transversal.Interfaces;
namespace Documents.Services public class DocumentTemplateService : IDocumentTemplateService
{ {
public class DocumentTemplateService : IDocumentTemplateService private readonly ITemplateRenderer _templateRenderer;
private readonly IPdfGeneratorService _pdfGeneratorService;
public DocumentTemplateService(ITemplateRenderer templateRenderer, IPdfGeneratorService pdfGeneratorService)
{ {
private readonly ITemplateRenderer _templateRenderer; _templateRenderer = templateRenderer;
private readonly IPdfGeneratorService _pdfGeneratorService; _pdfGeneratorService = pdfGeneratorService;
}
public DocumentTemplateService( public async Task<byte[]> GenerateDocumentAsync(DocumentGenerationRequest request)
ITemplateRenderer templateRenderer, {
IPdfGeneratorService pdfGeneratorService) // Leer logo
var logoPath = Path.Combine(Directory.GetCurrentDirectory(), "Resources", "logo.png");
var logoBase64 = GetImageBase64(logoPath);
// Inyectar al modelo si corresponde
if (request.Model is QuoteDto quote)
{ {
_templateRenderer = templateRenderer; quote.LogoBase64 = logoBase64;
_pdfGeneratorService = pdfGeneratorService;
} }
public async Task<byte[]> GenerateDocumentAsync(DocumentGenerationRequest request) string html = await _templateRenderer.RenderAsync("Quotes/Template_v1.cshtml", request.Model);
{ return await _pdfGeneratorService.GeneratePdfFromHtmlAsync(html);
// 👉 Renderizar HTML usando RazorLight }
string html = await _templateRenderer.RenderAsync("Quotes/Template_v1.cshtml", request.Model);
// 👉 Generar PDF desde el HTML
return await _pdfGeneratorService.GeneratePdfFromHtmlAsync(html);
}
private static string GetImageBase64(string imagePath)
{
if (!File.Exists(imagePath))
return "";
byte[] imageBytes = File.ReadAllBytes(imagePath);
return Convert.ToBase64String(imageBytes);
} }
} }
//using Documents.Interfaces;
//using Documents.Models;
//using System.Reflection;
//using System.Text;
//using Transversal.Interfaces;
//namespace Documents.Services
//{
// public class DocumentTemplateService : IDocumentTemplateService
// {
// private readonly ITemplateRenderer _templateRenderer;
// private readonly IPdfGeneratorService _pdfGeneratorService;
// public DocumentTemplateService(
// ITemplateRenderer templateRenderer,
// IPdfGeneratorService pdfGeneratorService)
// {
// _templateRenderer = templateRenderer;
// _pdfGeneratorService = pdfGeneratorService;
// }
// public async Task<byte[]> GenerateDocumentAsync(DocumentGenerationRequest request)
// {
// // 👉 Renderizar HTML usando RazorLight
// string html = await _templateRenderer.RenderAsync("Quotes/Template_v1.cshtml", request.Model);
// // 👉 Generar PDF desde el HTML
// return await _pdfGeneratorService.GeneratePdfFromHtmlAsync(html);
// }
// private static string GetImageBase64(string imagePath)
// {
// if (!File.Exists(imagePath))
// return "";
// byte[] imageBytes = File.ReadAllBytes(imagePath);
// return Convert.ToBase64String(imageBytes);
// }
// }
//}

View File

@ -87,8 +87,12 @@
</head> </head>
<body> <body>
<div class="header"> <div class="header">
<h2>Presupuesto Nº @Model.Quotenumber</h2> <img src="data:image/png;base64,@Model.LogoBase64" alt="Logo" style="height: 60px; float: left;" />
<p>Fecha de emisión: @Model.IssueDate.ToString("dd/MM/yyyy")</p> <div style="margin-left: 70px;">
<h2>Presupuesto</h2>
<h2>@Model.Quotenumber</h2>
<p>Fecha de emisión: @Model.IssueDate.ToString("dd/MM/yyyy")</p>
</div>
</div> </div>
<div class="row"> <div class="row">
<div class="col izquierda"> <div class="col izquierda">
@ -127,10 +131,10 @@
<table> <table>
<thead> <thead>
<tr> <tr>
<th>Descripción</th> <th style="width: 40%;">Descripción</th>
<th>Cantidad</th> <th style="width: 15%; text-align: center;">Cantidad</th>
<th>Precio Unitario</th> <th style="width: 20%; text-align: right;">Precio Unitario</th>
<th>Subtotal</th> <th style="width: 25%; text-align: right;">Subtotal</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@ -138,9 +142,9 @@
{ {
<tr> <tr>
<td>@item.Description</td> <td>@item.Description</td>
<td>@item.Quantity</td> <td style="text-align: center;">@item.Quantity</td>
<td>@item.UnitPrice.ToString("C", culture)</td> <td style="text-align: right;">@item.UnitPrice.ToString("C", culture)</td>
<td>@item.Total.ToString("C", culture)</td> <td style="text-align: right;">@item.Total.ToString("C", culture)</td>
</tr> </tr>
} }
</tbody> </tbody>

View File

@ -90,5 +90,10 @@
/// Datos comerciales del cliente. /// Datos comerciales del cliente.
/// </summary> /// </summary>
public QuoteCustomerDto Customer { get; set; } = new(); public QuoteCustomerDto Customer { get; set; } = new();
/// <summary>
/// Logo de la compañia.
/// </summary>
public string LogoBase64 { get; set; } = "";
} }
} }

View File

@ -56,4 +56,6 @@ RUN dotnet publish "./phronCare.API.csproj" -c $BUILD_CONFIGURATION -o /app/publ
FROM base AS final FROM base AS final
WORKDIR /app WORKDIR /app
COPY --from=publish /app/publish . COPY --from=publish /app/publish .
# ✅ Copiar el logo al contenedor final
COPY phronCare.API/Resources/logo.png /app/Resources/logo.png
ENTRYPOINT ["dotnet", "phronCare.API.dll"] ENTRYPOINT ["dotnet", "phronCare.API.dll"]

Binary file not shown.

After

Width:  |  Height:  |  Size: 176 KiB

View File

@ -13,6 +13,14 @@
<Content Remove="App.Config" /> <Content Remove="App.Config" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Remove="Resources\logo.png" />
</ItemGroup>
<ItemGroup>
<Content Include="Resources\logo.png" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="App.Config" /> <None Include="App.Config" />
</ItemGroup> </ItemGroup>