Add device detection and PDF generation features; refactor AuthDialog and update print utilities
This commit is contained in:
@@ -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;}";
|
||||
}
|
||||
Reference in New Issue
Block a user