prueba fallo de imprimir
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.11.35312.102
|
||||
# Visual Studio Version 18
|
||||
VisualStudioVersion = 18.1.11312.151 d18.0
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HerramientaCASA", "HerramientaCASA\HerramientaCASA.csproj", "{60B569DA-FC18-45B9-8ACC-8E29D8C2EA2C}"
|
||||
EndProject
|
||||
|
||||
@@ -147,7 +147,7 @@
|
||||
{
|
||||
urlAnterior = await JS.InvokeAsync<string>("eval", "document.referrer");
|
||||
bd = tsHerramientasCACOA.NuevoContexto(SoloLectura: false);
|
||||
string urlColegio = bd.enumeraciones.First(x => x.Codigo.Contains("CW.URLCOLEGIO")).ValorAlfabetico1;
|
||||
string urlColegio = bd.enumeraciones.FirstOrDefault(x => x.Codigo.Contains("CW.URLCOLEGIO")).ValorAlfabetico1;
|
||||
|
||||
|
||||
if (!string.IsNullOrEmpty(urlAnterior))
|
||||
@@ -158,18 +158,13 @@
|
||||
|
||||
if (rutaActual != "Denegado")
|
||||
{
|
||||
if (!urlAnterior.Contains(urlColegio))
|
||||
if (urlColegio != null && !urlAnterior.Contains(urlColegio))
|
||||
{
|
||||
Navigation.NavigateTo("/Denegado", true);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
Navigation.NavigateTo("/Denegado", true);
|
||||
}
|
||||
|
||||
|
||||
if (userState.idUser == 0)
|
||||
{
|
||||
|
||||
@@ -51,33 +51,36 @@
|
||||
|
||||
protected override async Task OnAfterRenderAsync(bool firstRender)
|
||||
{
|
||||
// if (firstRender)
|
||||
// {
|
||||
// urlAnterior = await JS.InvokeAsync<string>("eval", "document.referrer");
|
||||
// bd = tsHerramientasCACOA.NuevoContexto(SoloLectura: false);
|
||||
// string urlColegio = bd.enumeraciones.First(x => x.Codigo.Contains("CW.URLCOLEGIO")).ValorAlfabetico1;
|
||||
if (firstRender)
|
||||
{
|
||||
urlAnterior = await JS.InvokeAsync<string>("eval", "document.referrer");
|
||||
bd = tsHerramientasCACOA.NuevoContexto(SoloLectura: false);
|
||||
string urlColegio = bd.enumeraciones.First(x => x.Codigo.Contains("CW.URLCOLEGIO")).ValorAlfabetico1;
|
||||
|
||||
// if (!string.IsNullOrEmpty(urlAnterior))
|
||||
// {
|
||||
if (!string.IsNullOrEmpty(urlAnterior))
|
||||
{
|
||||
|
||||
// rutaActual = Navigation.ToBaseRelativePath(Navigation.Uri);
|
||||
rutaActual = Navigation.ToBaseRelativePath(Navigation.Uri);
|
||||
|
||||
|
||||
// if (rutaActual != "Denegado")
|
||||
// {
|
||||
// if (!urlAnterior.Contains(urlColegio))
|
||||
// {
|
||||
// Navigation.NavigateTo("/Denegado", true);
|
||||
// }
|
||||
// }
|
||||
if (rutaActual != "Denegado")
|
||||
{
|
||||
if (urlColegio != null && !urlAnterior.Contains(urlColegio))
|
||||
{
|
||||
Navigation.NavigateTo("/Denegado", true);
|
||||
}
|
||||
}
|
||||
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// Navigation.NavigateTo("/Denegado", true);
|
||||
// }
|
||||
// StateHasChanged();
|
||||
// }
|
||||
}
|
||||
else
|
||||
{
|
||||
if (urlColegio != null)
|
||||
{
|
||||
Navigation.NavigateTo("/Denegado", true);
|
||||
}
|
||||
}
|
||||
StateHasChanged();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -140,7 +140,7 @@
|
||||
|
||||
if (rutaActual != "Denegado")
|
||||
{
|
||||
if (!urlAnterior.Contains(urlColegio))
|
||||
if (urlColegio != null && !urlAnterior.Contains(urlColegio))
|
||||
{
|
||||
Navigation.NavigateTo("/Denegado", true);
|
||||
}
|
||||
|
||||
@@ -375,6 +375,17 @@
|
||||
|
||||
|
||||
public async Task imprimir()
|
||||
{
|
||||
|
||||
var a = PdfService.obtenerURL();
|
||||
|
||||
mensajes.Add(new ToastMessage
|
||||
{
|
||||
Type = ToastType.Danger,
|
||||
Message = a,
|
||||
});
|
||||
|
||||
try
|
||||
{
|
||||
var cssPath = Path.Combine(Environment.CurrentDirectory, "wwwroot", "Content", "Site.css");
|
||||
var cssContent = await File.ReadAllTextAsync(cssPath);
|
||||
@@ -392,14 +403,22 @@
|
||||
</body>
|
||||
</html>";
|
||||
|
||||
var logoByte = bd.ficheros.First(x => x.NombreFichero.Contains("LogoColegio2")).Fichero;
|
||||
|
||||
string logo64 = Convert.ToBase64String(logoByte);
|
||||
var arrayPDF = PdfService.GenerarPdf("fullHtml");
|
||||
|
||||
|
||||
var arrayPDF = PdfService.GenerarPdf(fullHtml, logo64);
|
||||
|
||||
string base64Pdf = Convert.ToBase64String(arrayPDF);
|
||||
|
||||
await JSA.InvokeVoidAsync("descargarPdf", "SimulacionHerramientaCASA_" + DateTime.Now , base64Pdf);
|
||||
await JSA.InvokeVoidAsync("descargarPdf", "SimulacionHerramientaCASA_" + DateTime.Now , null);
|
||||
}catch(Exception ex)
|
||||
{
|
||||
mensajes.Add(new ToastMessage
|
||||
{
|
||||
Type = ToastType.Danger,
|
||||
Message = ex.Message,
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -372,11 +372,8 @@
|
||||
</html>";
|
||||
|
||||
|
||||
var logoByte = bd.ficheros.First(x => x.NombreFichero.Contains("LogoColegio2")).Fichero;
|
||||
|
||||
string logo64 = Convert.ToBase64String(logoByte);
|
||||
|
||||
var arrayPDF = PdfService.GenerarPdf(fullHtml, logo64);
|
||||
var arrayPDF = PdfService.GenerarPdf(fullHtml);
|
||||
|
||||
string base64Pdf = Convert.ToBase64String(arrayPDF);
|
||||
|
||||
|
||||
@@ -540,11 +540,7 @@
|
||||
</html>";
|
||||
|
||||
|
||||
var logoByte = bd.ficheros.First(x => x.NombreFichero.Contains("LogoColegio2")).Fichero;
|
||||
|
||||
string logo64 = Convert.ToBase64String(logoByte);
|
||||
|
||||
var arrayPDF = PdfService.GenerarPdf(fullHtml, logo64);
|
||||
var arrayPDF = PdfService.GenerarPdf(fullHtml);
|
||||
|
||||
string base64Pdf = Convert.ToBase64String(arrayPDF);
|
||||
|
||||
|
||||
@@ -611,11 +611,7 @@
|
||||
</html>";
|
||||
|
||||
|
||||
var logoByte = bd.ficheros.First(x => x.NombreFichero.Contains("LogoColegio2")).Fichero;
|
||||
|
||||
string logo64 = Convert.ToBase64String(logoByte);
|
||||
|
||||
var arrayPDF = PdfService.GenerarPdf(fullHtml, logo64);
|
||||
var arrayPDF = PdfService.GenerarPdf(fullHtml);
|
||||
|
||||
string base64Pdf = Convert.ToBase64String(arrayPDF);
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Globalization;
|
||||
using System.Runtime.InteropServices;
|
||||
using tsUtilidades;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
@@ -41,6 +42,7 @@ builder.Services.AddRazorPages()
|
||||
options.JsonSerializerOptions.ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.IgnoreCycles;
|
||||
});
|
||||
|
||||
|
||||
// Necesario para ver porqu<71> est<73> fallando ciertas cosas que dan el error Circuit
|
||||
builder.Services.AddServerSideBlazor().AddCircuitOptions(option => { option.DetailedErrors = true; });
|
||||
builder.Services.AddHttpContextAccessor();
|
||||
@@ -87,6 +89,8 @@ app.UseRouting();
|
||||
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
app.MapRazorPages();
|
||||
|
||||
app.UseAntiforgery();
|
||||
|
||||
app.Use(async (context, next) =>
|
||||
@@ -117,6 +121,43 @@ app.Use(async (context, next) =>
|
||||
await next();
|
||||
});
|
||||
|
||||
|
||||
app.MapGet("/HeaderPdf", () =>
|
||||
{
|
||||
var bd = tsHerramientasCACOA.NuevoContexto(SoloLectura: true);
|
||||
|
||||
var logoByte = bd.ficheros
|
||||
.First(x => x.NombreFichero.Contains("LogoColegio2"))
|
||||
.Fichero;
|
||||
|
||||
var logoBase64 = Convert.ToBase64String(logoByte);
|
||||
|
||||
string html = $"<!DOCTYPE html>\r\n" +
|
||||
$"<html>\r\n" +
|
||||
$"<head>\r\n" +
|
||||
$"<meta charset='utf-8' />\r\n" +
|
||||
$"<title></title>\r\n" +
|
||||
$"</head>\r\n" +
|
||||
$"<body style='margin:0px !important;'>\r\n" +
|
||||
$"<div style='height:55px; width:100%; font-size:12px; color:#078b98; font-weight:bold; background-color: #D7ECEF; display:flex;'>\r\n" +
|
||||
$"<table style='width:100%; border:0; border-collapse:collapse;'>\r\n" +
|
||||
$" <tr>\r\n" +
|
||||
$"<td style='text-align: left; width: 50%; padding-left: 10px; padding-top:8px;'>\r\n" +
|
||||
$"<img style='height: 40px; border-radius: 10px !important;' src='data:image/jpeg;base64,{logoBase64}' alt='Image'>\r\n" +
|
||||
$"</td>\r\n" +
|
||||
$"<td style='text-align: right; width: 50%; padding: 10px;'>\r\n" +
|
||||
$"Costes Asociados a los Servicios de Arquitectura - CASA\r\n" +
|
||||
$"</td>\r\n" +
|
||||
$"</tr>\r\n" +
|
||||
$"</table>\r\n" +
|
||||
$"</div>\r\n" +
|
||||
$"</body>\r\n" +
|
||||
$"</html>";
|
||||
|
||||
return Results.Content(html, "text/html");
|
||||
});
|
||||
|
||||
|
||||
app.MapRazorComponents<App>()
|
||||
.AddInteractiveServerRenderMode();
|
||||
|
||||
|
||||
@@ -1,29 +1,32 @@
|
||||
using DinkToPdf;
|
||||
using bdHerramientaCACOA.dbcontext;
|
||||
using BlazorBootstrap;
|
||||
using DinkToPdf;
|
||||
using DinkToPdf.Contracts;
|
||||
using DocumentFormat.OpenXml.Packaging;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Internal;
|
||||
using System.Text;
|
||||
namespace HerramientaCASA
|
||||
{
|
||||
public class ServicioPDF
|
||||
{
|
||||
private readonly IConverter _converter;
|
||||
private readonly IWebHostEnvironment _env;
|
||||
|
||||
public ServicioPDF(IConverter converter)
|
||||
public ServicioPDF(IConverter converter, IWebHostEnvironment env)
|
||||
{
|
||||
_converter = converter;
|
||||
_env = env;
|
||||
}
|
||||
|
||||
public byte[] GenerarPdf(string html, string logoBase64)
|
||||
public byte[] GenerarPdf(string html)
|
||||
{
|
||||
var headerPath = Path.Combine(Path.GetTempPath(), $"header_{Guid.NewGuid()}.html");
|
||||
File.WriteAllText(headerPath, rellenarHeader(logoBase64));
|
||||
try
|
||||
{
|
||||
var doc = new HtmlToPdfDocument()
|
||||
{
|
||||
GlobalSettings = {
|
||||
PaperSize = PaperKind.A4,
|
||||
Orientation = Orientation.Portrait,
|
||||
Orientation = DinkToPdf.Orientation.Portrait,
|
||||
Margins= new MarginSettings
|
||||
{
|
||||
Top = 11.8,
|
||||
@@ -37,138 +40,37 @@ namespace HerramientaCASA
|
||||
HtmlContent = html,
|
||||
HeaderSettings = new HeaderSettings
|
||||
{
|
||||
HtmUrl = headerPath
|
||||
//HtmUrl = Path.Combine(Environment.CurrentDirectory, "wwwroot", "Imprimir", "HeaderImprimir.html")
|
||||
//HtmUrl = "https://localhost:7275/HeaderPdf"
|
||||
HtmUrl = obtenerURL()
|
||||
},
|
||||
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
return _converter.Convert(doc);
|
||||
}
|
||||
finally
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (headerPath != null && File.Exists(headerPath))
|
||||
throw new Exception(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public string obtenerURL()
|
||||
{
|
||||
File.Delete(headerPath);
|
||||
}
|
||||
}
|
||||
string url = "";
|
||||
var bd = tsHerramientasCACOA.NuevoContexto(SoloLectura: false);
|
||||
|
||||
}
|
||||
|
||||
private string rellenarHeader(string logoBase64)
|
||||
if (_env.IsDevelopment())
|
||||
{
|
||||
string html = "";
|
||||
|
||||
html = $"<!DOCTYPE html>\r\n" +
|
||||
$"<html>\r\n" +
|
||||
$"<head>\r\n" +
|
||||
$"<meta charset='utf-8' />\r\n" +
|
||||
$"<title></title>\r\n"+
|
||||
$"</head>\r\n"+
|
||||
$"<body style='margin:0px !important;'>\r\n" +
|
||||
$"<div style='height:55px; width:100%; font-size:12px; color:#078b98; font-weight:bold; background-color: #D7ECEF; display:flex;'>\r\n"+
|
||||
$"<table style='width:100%; border:0; border-collapse:collapse;'>\r\n" +
|
||||
$" <tr>\r\n"+
|
||||
$"<td style='text-align: left; width: 50%; padding-left: 10px; padding-top:8px;'>\r\n" +
|
||||
$"<img style='height: 40px; border-radius: 10px !important;' src='data:image/jpeg;base64,{logoBase64}' alt='Image'>\r\n" +
|
||||
$"</td>\r\n"+
|
||||
$"<td style='text-align: right; width: 50%; padding: 10px;'>\r\n"+
|
||||
$"Costes Asociados a los Servicios de Arquitectura - CASA\r\n"+
|
||||
$"</td>\r\n"+
|
||||
$"</tr>\r\n"+
|
||||
$"</table>\r\n"+
|
||||
$"</div>\r\n"+
|
||||
$"</body>\r\n"+
|
||||
$"</html>";
|
||||
|
||||
return html;
|
||||
url = "https://localhost:7275/HeaderPdf";
|
||||
}
|
||||
else
|
||||
{
|
||||
url = bd.enumeraciones.First(x => x.Codigo.Contains("CW.DOMINIO")).ValorAlfabetico1 + "/HeaderPdf";
|
||||
}
|
||||
|
||||
// /// <summary>
|
||||
// /// Convierte HTML a PDF (bytes) usando HtmlRenderer.PdfSharpCore, inyectando site.css.
|
||||
|
||||
// /// </summary>
|
||||
// /// <param name="htmlBody">
|
||||
// /// Contenido HTML del body (o el documento completo). Si pasas solo el body, se envolverá automáticamente.
|
||||
// /// </param>
|
||||
// /// <param name="siteCssPath">Ruta al archivo site.css en el servidor (p. ej. "wwwroot/css/site.css").</param>
|
||||
// /// <param name="baseUri">
|
||||
// /// (Opcional) Base para resolver rutas relativas.
|
||||
// /// Se inserta como <base href="..."> en <head>.
|
||||
// /// </param>
|
||||
// /// <param name="pageSize">Tamaño de página (A4 por defecto).</param>
|
||||
// /// <param name="margin">Margen en puntos (40 por defecto).</param>
|
||||
// public byte[] HtmlToPdfWithSiteCss(
|
||||
// string htmlBody,
|
||||
// string siteCssPath,
|
||||
// string? baseUri = null,
|
||||
// PdfSharpCore.PageSize pageSize = PdfSharpCore.PageSize.A4,
|
||||
// int margin = 40
|
||||
// )
|
||||
// {
|
||||
// if (string.IsNullOrWhiteSpace(htmlBody))
|
||||
// throw new ArgumentException("El HTML no puede estar vacío.", nameof(htmlBody));
|
||||
|
||||
// if (string.IsNullOrWhiteSpace(siteCssPath) || !File.Exists(siteCssPath))
|
||||
// throw new FileNotFoundException("No se encontró el archivo CSS indicado.", siteCssPath);
|
||||
|
||||
// // Evitar problemas de codificación (tildes, ñ)
|
||||
// Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
|
||||
|
||||
// // Lee el CSS local
|
||||
// var css = File.ReadAllText(siteCssPath);
|
||||
|
||||
// // HTML mínimo
|
||||
// bool isFullHtml = htmlBody.Contains("<html", StringComparison.OrdinalIgnoreCase);
|
||||
// string headExtras = $"<meta charset='utf-8'><style>{css}</style>";
|
||||
// if (!string.IsNullOrWhiteSpace(baseUri))
|
||||
// headExtras = $"<base href=\"{baseUri}\">{headExtras}";
|
||||
|
||||
// string fullHtml = isFullHtml
|
||||
// ? InjectIntoHead(htmlBody, headExtras)
|
||||
// : $"<!doctype html><html><head>{headExtras}</head><body>{htmlBody}</body></html>";
|
||||
|
||||
// var cfg = new PdfGenerateConfig
|
||||
// {
|
||||
// PageSize = (PdfSharp.PageSize)pageSize,
|
||||
// MarginLeft = margin,
|
||||
// MarginRight = margin,
|
||||
// MarginTop = margin,
|
||||
// MarginBottom = margin
|
||||
// };
|
||||
|
||||
// using var doc = PdfGenerator.GeneratePdf(fullHtml, cfg);
|
||||
// using var ms = new MemoryStream();
|
||||
// doc.Save(ms);
|
||||
// return ms.ToArray();
|
||||
|
||||
// // Inserta contenido en <head> (si existe); si no, lo crea.
|
||||
// static string InjectIntoHead(string html, string toInject)
|
||||
// {
|
||||
// int headOpen = html.IndexOf("<head", StringComparison.OrdinalIgnoreCase);
|
||||
// if (headOpen >= 0)
|
||||
// {
|
||||
// int headClose = html.IndexOf('>', headOpen);
|
||||
// if (headClose > headOpen)
|
||||
// {
|
||||
// return html.Insert(headClose + 1, toInject);
|
||||
// }
|
||||
// }
|
||||
// // No hay <head>, lo creamos
|
||||
// int htmlOpen = html.IndexOf("<html", StringComparison.OrdinalIgnoreCase);
|
||||
// if (htmlOpen >= 0)
|
||||
// {
|
||||
// int htmlTagEnd = html.IndexOf('>', htmlOpen);
|
||||
// if (htmlTagEnd > htmlOpen)
|
||||
// {
|
||||
// return html.Insert(htmlTagEnd + 1, $"<head>{toInject}</head>");
|
||||
// }
|
||||
// }
|
||||
// // Documento raro: envolvemos completo
|
||||
// return $"<!doctype html><html><head>{toInject}</head><body>{html}</body></html>";
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
return url;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,8 +14,11 @@
|
||||
//"ReadOnlyConnection": "Server=77.229.174.85;Port=22222;Database=herramientascacoa;User Id=cacoa;Password=cacoa2018-;"
|
||||
|
||||
// CONEXIÓN COACYLE
|
||||
"WriteConnection": "Server=77.229.174.85;Port=22222;Database=herramientascoacyle;User Id=cacoa;Password=cacoa2018-;",
|
||||
"ReadOnlyConnection": "Server=77.229.174.85;Port=22222;Database=herramientascoacyle;User Id=cacoa;Password=cacoa2018-;"
|
||||
//"WriteConnection": "Server=77.229.174.85;Port=22222;Database=herramientascoacyle;User Id=cacoa;Password=cacoa2018-;",
|
||||
//"ReadOnlyConnection": "Server=77.229.174.85;Port=22222;Database=herramientascoacyle;User Id=cacoa;Password=cacoa2018-;"
|
||||
|
||||
"WriteConnection": "Server=localhost;Port=22222;Database=herramientascoacyle;User Id=cacoa;Password=cacoa2018-;",
|
||||
"ReadOnlyConnection": "Server=localhost;Port=22222;Database=herramientascoacyle;User Id=cacoa;Password=cacoa2018-;"
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user