Skip to content

Commit

Permalink
[-] Removed wkhtml
Browse files Browse the repository at this point in the history
[~] Changed PDF Test to use puppeteer
CactuseSecurity#2322
  • Loading branch information
SolidProgramming committed Sep 13, 2024
1 parent ed5ac52 commit 1c56417
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 43 deletions.
1 change: 0 additions & 1 deletion roles/lib/files/FWO.Report/FWO.Report.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Haukcode.WkHtmlToPdfDotNet" Version="1.5.90" />
<PackageReference Include="PuppeteerSharp" Version="20.0.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using FWO.Report;
using FWO.Report.Filter;
using System.Timers;
using WkHtmlToPdfDotNet;
using FWO.Config.File;
using PuppeteerSharp.Media;

Expand Down
1 change: 0 additions & 1 deletion roles/test/files/FWO.Test/FWO.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Haukcode.WkHtmlToPdfDotNet" Version="1.5.90" />
<PackageReference Include="NUnit" Version="4.1.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
Expand Down
64 changes: 25 additions & 39 deletions roles/test/files/FWO.Test/HtmlToPdfTest.cs
Original file line number Diff line number Diff line change
@@ -1,65 +1,51 @@
using NUnit.Framework;
using NUnit.Framework.Legacy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WkHtmlToPdfDotNet;
using FWO.Logging;
using PuppeteerSharp.Media;
using PuppeteerSharp;

namespace FWO.Test
{
[TestFixture]
[Parallelizable]
internal class HtmlToPdfTest
{
// Pdf converter
protected readonly SynchronizedConverter converter;

public HtmlToPdfTest()
{
converter = new SynchronizedConverter(new PdfTools());
}


[Test]
[Parallelizable]
public void GeneratePdf()
public async Task GeneratePdf()
{
Log.WriteInfo("Test Log", "starting PDF generation");
// HTML
string html = "<html> <body> <h1>test<h1> test </body> </html>";

GlobalSettings globalSettings = new GlobalSettings
using IBrowser? browser = await Puppeteer.LaunchAsync(new LaunchOptions
{
ColorMode = ColorMode.Color,
Orientation = Orientation.Landscape,
PaperSize = PaperKind.A4
};
Headless = true
});

HtmlToPdfDocument doc = new HtmlToPdfDocument()
string filePath = "pdffile.pdf";

try
{
GlobalSettings = globalSettings,
Objects =
{
new ObjectSettings()
{
PagesCount = true,
HtmlContent = html,
WebSettings = { DefaultEncoding = "utf-8" },
HeaderSettings = { FontSize = 9, Right = "Page [page] of [toPage]", Line = true, Spacing = 2.812 }
}
}
};
using IPage page = await browser.NewPageAsync();
await page.SetContentAsync(html);

PdfOptions pdfOptions = new() { DisplayHeaderFooter = true, Landscape = true, PrintBackground = true, Format = PuppeteerSharp.Media.PaperFormat.A4, MarginOptions = new MarginOptions { Top = "1cm", Bottom = "1cm", Left = "1cm", Right = "1cm" } };
await page.PdfAsync(filePath);

byte[] pdf = converter.Convert(doc);
string filePath = "test.pdf";
using (var s = File.OpenWrite(filePath)) {
var bw = new BinaryWriter(s);
bw.Write(pdf);
}
catch (Exception)
{
throw new Exception("This paper kind is currently not supported. Please choose another one or \"Custom\" for a custom size.");
}
finally
{
await browser.CloseAsync();
}

Assert.That(filePath, Does.Exist);
ClassicAssert.Greater(new System.IO.FileInfo(filePath).Length, 5000);
ClassicAssert.Greater(new FileInfo(filePath).Length, 5000);
}

[OneTimeTearDown]
Expand Down
1 change: 0 additions & 1 deletion roles/ui/files/FWO.UI/Pages/Reporting/ReportExport.razor
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
@using FWO.Report
@using FWO.Config.Api
@using System.Reflection
@using WkHtmlToPdfDotNet
@using System.Linq
@using FWO.Report.Filter
@using System.IO
Expand Down

0 comments on commit 1c56417

Please sign in to comment.