Add device detection and PDF generation features; refactor AuthDialog and update print utilities

This commit is contained in:
masoodafar-web
2025-11-14 13:12:31 +03:30
parent e86cb7aa47
commit 230ba41113
12 changed files with 397 additions and 54 deletions
@@ -0,0 +1,36 @@
using PdfSharpCore.Pdf;
//using HtmlRendererCore.PdfSharpCore;
namespace FrontOffice.Main.Utilities.Pdf;
public interface IHtmlToPdfService
{
byte[] GeneratePdf(string html, string? fileName = null);
}
public class HtmlToPdfService : IHtmlToPdfService
{
public byte[] GeneratePdf(string html, string? fileName = null)
{
if (string.IsNullOrWhiteSpace(html))
throw new ArgumentException("Html content is empty", nameof(html));
if (!html.Contains("<html", StringComparison.OrdinalIgnoreCase))
{
html = $"<html><head><meta charset='utf-8'><style>{GetDefaultStyles()}</style></head><body>{html}</body></html>";
}
else if (!html.Contains("<style", StringComparison.OrdinalIgnoreCase))
{
// Inject default styles if none provided
html = html.Replace("<head>", "<head><style>" + GetDefaultStyles() + "</style>");
}
using var document = new PdfDocument();
//PdfGenerator.AddPdfPages(document, html, PdfSharpCore.PageSize.A4);
using var ms = new MemoryStream();
document.Save(ms);
return ms.ToArray();
}
private static string GetDefaultStyles() => @"body{font-family:'Vazir','Arial';direction:rtl;text-align:right;font-size:12px;line-height:1.6;margin:25px;} h1,h2,h3{margin:0 0 12px;} ul{margin:0 0 12px 0;padding:0 18px;} .small{font-size:10px;color:#555;}";
}