From 1e9ef5b0b95b4dbf7cc70b2ad34e509f3fb796a5 Mon Sep 17 00:00:00 2001 From: "luca.weidmann@gmx.de" Date: Thu, 12 Sep 2024 12:28:21 +0200 Subject: [PATCH] [+] Added overloads for pdf creation with puppeteer --- roles/lib/files/FWO.Report/ReportBase.cs | 73 +++++-------------- .../ImportChangeNotifier.cs | 16 +++- .../FWO.Middleware.Server/ReportScheduler.cs | 5 +- .../FWO.UI/Pages/Reporting/ReportExport.razor | 2 +- 4 files changed, 34 insertions(+), 62 deletions(-) diff --git a/roles/lib/files/FWO.Report/ReportBase.cs b/roles/lib/files/FWO.Report/ReportBase.cs index 149e70ecb..e9dcc1001 100644 --- a/roles/lib/files/FWO.Report/ReportBase.cs +++ b/roles/lib/files/FWO.Report/ReportBase.cs @@ -4,7 +4,6 @@ using FWO.Report.Filter; using FWO.Config.Api; using System.Text; -using WkHtmlToPdfDotNet; using PuppeteerSharp.Media; using PuppeteerSharp; @@ -90,9 +89,7 @@ public abstract class ReportBase public ReportData ReportData = new(); protected string htmlExport = ""; - - // Pdf converter - protected static readonly SynchronizedConverter converter = new (new PdfTools()); + public bool GotObjectsInReport { get; protected set; } = false; @@ -214,55 +211,7 @@ public static string ToUtcString(string? timestring) } } - public virtual byte[] ToPdf(PaperKind paperKind, int width = -1, int height = -1) - { - // HTML - if (string.IsNullOrEmpty(htmlExport)) - { - htmlExport = ExportToHtml(); - } - - GlobalSettings globalSettings = new () - { - ColorMode = ColorMode.Color, - Orientation = Orientation.Landscape, - }; - - if (paperKind == PaperKind.Custom) - { - if (width > 0 && height > 0) - { - globalSettings.PaperSize = new PechkinPaperSize(width + "mm", height + "mm"); - } - else - { - throw new Exception("Custom paper size: width or height <= 0"); - } - } - else - { - globalSettings.PaperSize = paperKind; - } - - HtmlToPdfDocument doc = new () - { - GlobalSettings = globalSettings, - Objects = - { - new ObjectSettings() - { - PagesCount = true, - HtmlContent = htmlExport, - WebSettings = { DefaultEncoding = "utf-8" }, - HeaderSettings = { FontSize = 9, Right = "Page [page] of [toPage]", Line = true, Spacing = 2.812 } - } - } - }; - - return converter.Convert(doc); - } - - public virtual async Task CreatePDFViaPuppeteer(string html) + private static async Task CreatePDFViaPuppeteer(string html, PaperFormat format) { using IBrowser? browser = await Puppeteer.LaunchAsync(new LaunchOptions { @@ -274,7 +223,7 @@ public virtual byte[] ToPdf(PaperKind paperKind, int width = -1, int height = -1 using IPage page = await browser.NewPageAsync(); await page.SetContentAsync(html); - PdfOptions pdfOptions = new PdfOptions() { DisplayHeaderFooter = true, Landscape = true, PrintBackground = true, Format = PaperFormat.A4, MarginOptions = new MarginOptions { Top = "1cm", Bottom = "1cm", Left = "1cm", Right = "1cm" } }; + PdfOptions pdfOptions = new PdfOptions() { DisplayHeaderFooter = true, Landscape = true, PrintBackground = true, Format = format, MarginOptions = new MarginOptions { Top = "1cm", Bottom = "1cm", Left = "1cm", Right = "1cm" } }; byte[] pdfData = await page.PdfDataAsync(pdfOptions); return Convert.ToBase64String(pdfData); @@ -282,7 +231,6 @@ public virtual byte[] ToPdf(PaperKind paperKind, int width = -1, int height = -1 catch (Exception) { throw new Exception("This paper kind is currently not supported. Please choose another one or \"Custom\" for a custom size."); - return default; } finally { @@ -290,6 +238,21 @@ public virtual byte[] ToPdf(PaperKind paperKind, int width = -1, int height = -1 } } + public virtual async Task ToPdf(string html, PaperFormat format) + { + return await CreatePDFViaPuppeteer(html, format); + } + + public virtual async Task ToPdf(string html) + { + return await CreatePDFViaPuppeteer(html, PaperFormat.A4); + } + + public virtual async Task ToPdf(PaperFormat format) + { + return await CreatePDFViaPuppeteer(htmlExport, format); + } + public static string GetIconClass(ObjCategory? objCategory, string? objType) { return objType switch diff --git a/roles/middleware/files/FWO.Middleware.Server/ImportChangeNotifier.cs b/roles/middleware/files/FWO.Middleware.Server/ImportChangeNotifier.cs index 4e04a02be..7a820d717 100644 --- a/roles/middleware/files/FWO.Middleware.Server/ImportChangeNotifier.cs +++ b/roles/middleware/files/FWO.Middleware.Server/ImportChangeNotifier.cs @@ -11,7 +11,7 @@ using Newtonsoft.Json; using System.Text.Json.Serialization; using System.Text.RegularExpressions; -using WkHtmlToPdfDotNet; +using PuppeteerSharp.Media; namespace FWO.Middleware.Server { @@ -175,11 +175,14 @@ private async Task SendEmail() EmailConnection emailConnection = new(globalConfig.EmailServerAddress, globalConfig.EmailPort, globalConfig.EmailTls, globalConfig.EmailUser, decryptedSecret, globalConfig.EmailSenderAddress); MailKitMailer mailer = new(emailConnection); - await mailer.SendAsync(PrepareEmail(), emailConnection, new CancellationToken(), + + MailData? mail = await PrepareEmail(); + + await mailer.SendAsync(mail, emailConnection, new CancellationToken(), globalConfig.ImpChangeNotifyType == (int)ImpChangeNotificationType.HtmlInBody); } - private MailData PrepareEmail() + private async Task PrepareEmail() { string subject = globalConfig.ImpChangeNotifySubject; string body = CreateBody(); @@ -192,7 +195,12 @@ private MailData PrepareEmail() body += changeReport?.ExportToHtml(); break; case (int)ImpChangeNotificationType.PdfAsAttachment: - attachment = CreateAttachment(Convert.ToBase64String(changeReport?.ToPdf(PaperKind.A4) ?? throw new Exception("No Pdf generated.")), GlobalConst.kPdf); + string? pdfData = await changeReport.ToPdf(PaperFormat.A4); + + if (string.IsNullOrWhiteSpace(pdfData)) + throw new Exception("No Pdf generated."); + + attachment = CreateAttachment(pdfData, GlobalConst.kPdf); break; case (int)ImpChangeNotificationType.HtmlAsAttachment: attachment = CreateAttachment(changeReport?.ExportToHtml(), GlobalConst.kHtml); diff --git a/roles/middleware/files/FWO.Middleware.Server/ReportScheduler.cs b/roles/middleware/files/FWO.Middleware.Server/ReportScheduler.cs index 441eed8db..3a26e49d6 100644 --- a/roles/middleware/files/FWO.Middleware.Server/ReportScheduler.cs +++ b/roles/middleware/files/FWO.Middleware.Server/ReportScheduler.cs @@ -10,6 +10,7 @@ using System.Timers; using WkHtmlToPdfDotNet; using FWO.Config.File; +using PuppeteerSharp.Media; namespace FWO.Middleware.Server { @@ -255,7 +256,7 @@ private static async Task AdaptDeviceFilter(ReportParams reportParams, ApiConnec } } - private static void WriteReportFile(ReportBase report, List fileFormats, ReportFile reportFile) + private static async Task WriteReportFile(ReportBase report, List fileFormats, ReportFile reportFile) { reportFile.Json = report.ExportToJson(); foreach (FileFormat format in fileFormats) @@ -271,7 +272,7 @@ private static void WriteReportFile(ReportBase report, List fileForm break; case GlobalConst.kPdf: - reportFile.Pdf = Convert.ToBase64String(report.ToPdf(PaperKind.A4)); + reportFile.Pdf = await report.ToPdf(PaperFormat.A4); break; case GlobalConst.kJson: diff --git a/roles/ui/files/FWO.UI/Pages/Reporting/ReportExport.razor b/roles/ui/files/FWO.UI/Pages/Reporting/ReportExport.razor index a3cd1d5cf..769039107 100644 --- a/roles/ui/files/FWO.UI/Pages/Reporting/ReportExport.razor +++ b/roles/ui/files/FWO.UI/Pages/Reporting/ReportExport.razor @@ -163,7 +163,7 @@ if (ExportPdf) { string html = ReportToExport.ExportToHtml(); - reportExportFile.Pdf = await ReportToExport.CreatePDFViaPuppeteer(html); + reportExportFile.Pdf = await ReportToExport.ToPdf(html); } if (ExportCsv)