Skip to content

Commit

Permalink
[+] Added puppeteersharp for pdf creation CactuseSecurity#2322
Browse files Browse the repository at this point in the history
  • Loading branch information
SolidProgramming committed Sep 10, 2024
1 parent cb8445b commit f1126c6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
1 change: 1 addition & 0 deletions roles/ui/files/FWO.UI/FWO.Ui.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<ItemGroup>
<PackageReference Include="IPAddressRange" Version="6.0.0" />
<PackageReference Include="BlazorTable" Version="1.17.0" />
<PackageReference Include="PuppeteerSharp" Version="20.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
42 changes: 33 additions & 9 deletions roles/ui/files/FWO.UI/Pages/Reporting/ReportExport.razor
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
@using WkHtmlToPdfDotNet
@using System.Linq
@using FWO.Report.Filter
@using PuppeteerSharp
@using PuppeteerSharp.Media
@using System.IO


@inject ApiConnection apiConnection
@inject UserConfig userConfig
Expand Down Expand Up @@ -159,15 +163,9 @@
}

if (ExportPdf)
{
try
{
reportExportFile.Pdf = Convert.ToBase64String(ReportToExport.ToPdf(SelectedPaperKind, PaperSizeWidth, PaperSizeHeight));
}
catch (KeyNotFoundException)
{
throw new Exception("This paper kind is currently not supported. Please choose another one or \"Custom\" for a custom size.");
}
{
string html = ReportToExport.ExportToHtml();
reportExportFile.Pdf = await CreatePDFViaPuppeteer(html);
}

if (ExportCsv)
Expand Down Expand Up @@ -211,4 +209,30 @@
DisplayMessageInUi(null, userConfig.GetText("export_report"), userConfig.GetText("E1002"), true);
}
}

private async Task<string?> CreatePDFViaPuppeteer(string html)
{
using IBrowser? browser = await Puppeteer.LaunchAsync(new LaunchOptions
{
Headless = true
});

try
{
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" }};
byte[] pdfData = await page.PdfDataAsync(pdfOptions);

return Convert.ToBase64String(pdfData);
}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
{
await browser.CloseAsync();
}
}
}
5 changes: 5 additions & 0 deletions roles/ui/files/FWO.UI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@
using Microsoft.AspNetCore.Components.Server.Circuits;
using RestSharp;
using System.Diagnostics;
using PuppeteerSharp;


// Implicitly call static constructor so background lock process is started
// (static constructor is only called after class is used in any way)
Log.WriteInfo("Startup", "Starting FWO UI Server...");

Log.WriteInfo("Startup", "Downloading headless Browser...");
BrowserFetcher? browserFetcher = new();
await browserFetcher.DownloadAsync();

var builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseWebRoot("wwwroot").UseStaticWebAssets();

Expand Down

0 comments on commit f1126c6

Please sign in to comment.