Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add tests for the PdfGenerator
  • Loading branch information
eXpl0it3r committed Jul 26, 2026
commit 62343397c532a6b5eb7e4bb7f75a96df26ff15de
13 changes: 13 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ jobs:
- name: Build
run: dotnet build Source/HtmlRenderer.sln --configuration Release --no-restore

- name: Run Tests
run: dotnet test Source/HtmlRenderer.sln --configuration Release --no-build --logger trx --results-directory ${{ github.workspace }}/TestResults
env:
PDF_OUTPUT_DIRECTORY: ${{ github.workspace }}/pdf-artifacts/${{ matrix.platform.name }}-${{ matrix.dotnet.name }}

- name: Create HtmlRenderer.Core NuGet package
run: dotnet pack Source/HtmlRenderer/HtmlRenderer.csproj --configuration Release --include-symbols -p:SymbolPackageFormat=snupkg --no-build --verbosity normal --output ${{ env.NuGetDirectory }}

Expand All @@ -69,6 +74,14 @@ jobs:
name: "HTML Renderer (${{ matrix.platform.name }} ${{ matrix.dotnet.name }})"
path: ${{ env.NuGetDirectory }}/*.*nupkg

- name: Upload PDF artifacts
if: always()
uses: actions/upload-artifact@v7
with:
name: "PDF artifacts (${{ matrix.platform.name }} ${{ matrix.dotnet.name }})"
path: ${{ github.workspace }}/pdf-artifacts/**/*.pdf
if-no-files-found: ignore

- name: NuGet Login
if: startsWith(github.ref, 'refs/tags/') && matrix.dotnet.name == '.NET 8' && runner.os == 'Windows'
uses: NuGet/login@v1
Expand Down
4 changes: 2 additions & 2 deletions Source/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<Authors>Arthur Teplitzki</Authors>
<Owners>Arthur Teplitzki</Owners>
<Company>Open source hosted on GitHub</Company>
<Copyright>Copyright © 2008-2025</Copyright>
<Copyright>Copyright © 2008-2026</Copyright>
<Version>1.6.0-dev</Version>
<AssemblyVersion>1.6.0.0</AssemblyVersion>
<FileVersion>1.6.0.0</FileVersion>
Expand All @@ -16,7 +16,7 @@
<PackageReleaseNotes>See https://github.com/ArthurHub/HTML-Renderer/releases.</PackageReleaseNotes>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
</PropertyGroup>
<ItemGroup>
<ItemGroup Condition="'$(IsTestProject)' != 'true'">
<None Include="../../html.png" Pack="true" PackagePath="\"/>
</ItemGroup>
</Project>
17 changes: 17 additions & 0 deletions Source/HtmlRenderer.sln
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HtmlRenderer", "HtmlRendere
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HtmlRenderer.PdfSharp", "HtmlRenderer.PdfSharp\HtmlRenderer.PdfSharp.csproj", "{CA249F5D-9285-40A6-B217-5889EF79FD7E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Test", "Test", "{E508FA78-D57E-492F-9BF7-23640001848A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HtmlRenderer.PdfSharp.Test", "Test\HtmlRenderer.PdfSharp.Test\HtmlRenderer.PdfSharp.Test.csproj", "{AF39851E-37B9-409E-A34D-CE091A098C86}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -99,6 +103,18 @@ Global
{CA249F5D-9285-40A6-B217-5889EF79FD7E}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{CA249F5D-9285-40A6-B217-5889EF79FD7E}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{CA249F5D-9285-40A6-B217-5889EF79FD7E}.Release|x86.ActiveCfg = Release|Any CPU
{AF39851E-37B9-409E-A34D-CE091A098C86}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AF39851E-37B9-409E-A34D-CE091A098C86}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AF39851E-37B9-409E-A34D-CE091A098C86}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{AF39851E-37B9-409E-A34D-CE091A098C86}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{AF39851E-37B9-409E-A34D-CE091A098C86}.Debug|x86.ActiveCfg = Debug|Any CPU
{AF39851E-37B9-409E-A34D-CE091A098C86}.Debug|x86.Build.0 = Debug|Any CPU
{AF39851E-37B9-409E-A34D-CE091A098C86}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AF39851E-37B9-409E-A34D-CE091A098C86}.Release|Any CPU.Build.0 = Release|Any CPU
{AF39851E-37B9-409E-A34D-CE091A098C86}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{AF39851E-37B9-409E-A34D-CE091A098C86}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{AF39851E-37B9-409E-A34D-CE091A098C86}.Release|x86.ActiveCfg = Release|Any CPU
{AF39851E-37B9-409E-A34D-CE091A098C86}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -107,6 +123,7 @@ Global
{2390B71F-9400-47F4-B23A-7F2649C87D35} = {E263EA16-2E6A-4269-A319-AA2F97ADA8E1}
{8AD34FE8-8382-4A8A-B3AA-A0392ED42423} = {E263EA16-2E6A-4269-A319-AA2F97ADA8E1}
{F02E0216-4AE3-474F-9381-FCB93411CDB0} = {E263EA16-2E6A-4269-A319-AA2F97ADA8E1}
{AF39851E-37B9-409E-A34D-CE091A098C86} = {E508FA78-D57E-492F-9BF7-23640001848A}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {902788D6-3165-491D-B860-DF2F74E66C1D}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MSTest" Version="4.3.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\HtmlRenderer.PdfSharp\HtmlRenderer.PdfSharp.csproj" />
<ProjectReference Include="..\..\Demo\Common\HtmlRenderer.Demo.Common.csproj" />
</ItemGroup>

<ItemGroup>
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting"/>
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions Source/Test/HtmlRenderer.PdfSharp.Test/MSTestSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[assembly: Parallelize(Scope = ExecutionScope.ClassLevel)]
150 changes: 150 additions & 0 deletions Source/Test/HtmlRenderer.PdfSharp.Test/PdfGeneratorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
using System.Runtime.InteropServices;
using PdfSharp;
using PdfSharp.Drawing;
using TheArtOfDev.HtmlRenderer.Core.Entities;
using TheArtOfDev.HtmlRenderer.Demo.Common;
using TheArtOfDev.HtmlRenderer.PdfSharp;

namespace HtmlRenderer.PdfSharp.Test;

[TestClass]
public sealed class PdfGeneratorTests
{
[TestMethod]
public void GeneratePdf_FromHtml_CreatesPdfDocument()
{
// Arrange
var config = new PdfGenerateConfig
{
PageSize = PageSize.A4
};
config.SetMargins(20);

const string html = """
<html>
<head>
<link rel="Stylesheet" href="StyleSheet" />
</head>
<body>
<h1>PdfSharp</h1>
<p>This is html rendered text <img src="ImageIcon" /></p>
</body>
</html>
""";

// Act
using var document = PdfGenerator.GeneratePdf(html, config, null, DemoUtils.OnStylesheetLoad, OnImageLoadPdfSharp);

// Assert
Assert.AreEqual(1, document.Pages.Count);

using var stream = new MemoryStream();
document.Save(stream, false);

var pdf = stream.ToArray();
Assert.IsGreaterThan(4, pdf.Length);
Assert.AreEqual("%PDF", System.Text.Encoding.ASCII.GetString(pdf, 0, 4));

var pdfOutputDirectory = Environment.GetEnvironmentVariable("PDF_OUTPUT_DIRECTORY");
if (string.IsNullOrWhiteSpace(pdfOutputDirectory))
{
pdfOutputDirectory = Path.Combine(Path.GetTempPath(), "html-renderer-pdf-artifacts");
}

Directory.CreateDirectory(pdfOutputDirectory);

var pdfPath = Path.Combine(
pdfOutputDirectory,
$"{nameof(GeneratePdf_FromHtml_CreatesPdfDocument)}-{GetPlatformName()}.pdf");
File.WriteAllBytes(pdfPath, pdf);
}

[TestMethod]
public void GeneratePdf_FromHtml_WithMultipleFonts_CreatesPdfDocument()
{
// Arrange
var config = new PdfGenerateConfig
{
PageSize = PageSize.A4
};
config.SetMargins(20);

const string html = """
<html>
<head>
<link rel="Stylesheet" href="StyleSheet" />
<style>
.helvetica { font-family: Helvetica; }
.mono { font-family: monospace; }
</style>
</head>
<body>
<p class="helvetica">Helvetica text</p>
<p class="mono">Monospace text</p>
</body>
</html>
""";

// Act
using var document = PdfGenerator.GeneratePdf(html, config, null, DemoUtils.OnStylesheetLoad, null);

// Assert
Assert.AreEqual(1, document.Pages.Count);

using var stream = new MemoryStream();
document.Save(stream, false);

var pdf = stream.ToArray();
Assert.IsGreaterThan(4, pdf.Length);
Assert.AreEqual("%PDF", System.Text.Encoding.ASCII.GetString(pdf, 0, 4));

var pdfOutputDirectory = Environment.GetEnvironmentVariable("PDF_OUTPUT_DIRECTORY");
if (string.IsNullOrWhiteSpace(pdfOutputDirectory))
{
pdfOutputDirectory = Path.Combine(Path.GetTempPath(), "html-renderer-pdf-artifacts");
}

Directory.CreateDirectory(pdfOutputDirectory);

var pdfPath = Path.Combine(
pdfOutputDirectory,
$"{nameof(GeneratePdf_FromHtml_WithMultipleFonts_CreatesPdfDocument)}-{GetPlatformName()}.pdf");
File.WriteAllBytes(pdfPath, pdf);
}

private static void OnImageLoadPdfSharp(object? sender, HtmlImageLoadEventArgs e)
{
if (!string.Equals(e.Src, "ImageIcon", StringComparison.OrdinalIgnoreCase))
{
return;
}

using var imageStream = DemoUtils.GetImageStream(e.Src);
if (imageStream == null)
{
throw new InvalidOperationException("Expected demo image resource was not found.");
}

e.Callback(XImage.FromStream(imageStream));
}

private static string GetPlatformName()
{
if (OperatingSystem.IsWindows())
{
return "windows";
}

if (OperatingSystem.IsMacOS())
{
return "macos";
}

if (OperatingSystem.IsLinux())
{
return "linux";
}

return RuntimeInformation.RuntimeIdentifier;
}
}
Loading