From 49ec9fbead9c6ba75763299abe540e73bac2804e Mon Sep 17 00:00:00 2001 From: TekH Date: Wed, 1 Jul 2026 01:25:24 +0200 Subject: [PATCH] Add PdfSharp font resolver for .NET 8 compatibility Added a `PdfSharpFontResolver` class to enable font resolution for PdfSharp in .NET 8, addressing the lack of system font access. The resolver reads fonts from the Windows Fonts folder and supports the Arial font family. Registered the resolver globally in `Program.cs` using `GlobalFontSettings.FontResolver`. Updated `Program.cs` with comments explaining the necessity of the resolver for .NET 8. The resolver includes methods to map font family names to specific font files and load font data. Throws a `FileNotFoundException` if required fonts are missing. Made minor formatting changes in `Program.cs` without altering the `SwaggerDoc` description functionality. --- .../EnvelopeGenerator.Server/Program.cs | 6 ++- .../Services/PdfSharpFontResolver.cs | 46 +++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 EnvelopeGenerator.Server/EnvelopeGenerator.Server/Services/PdfSharpFontResolver.cs diff --git a/EnvelopeGenerator.Server/EnvelopeGenerator.Server/Program.cs b/EnvelopeGenerator.Server/EnvelopeGenerator.Server/Program.cs index 96ba37ca..fbdb824c 100644 --- a/EnvelopeGenerator.Server/EnvelopeGenerator.Server/Program.cs +++ b/EnvelopeGenerator.Server/EnvelopeGenerator.Server/Program.cs @@ -104,7 +104,7 @@ try { Version = "v1", Title = "signFLOW Absender-API", - Description = "Eine API zur Verwaltung der Erstellung, des Versands und der Nachverfolgung von Umschlägen in der signFLOW-Anwendung.", + Description = "Eine API zur Verwaltung der Erstellung, des Versands und der Nachverfolgung von Umschl�gen in der signFLOW-Anwendung.", Contact = new OpenApiContact { Name = "Digital Data GmbH", @@ -338,6 +338,10 @@ try builder.Services.AddDevExpressBlazor(); builder.Services.AddDevExpressServerSideBlazorPdfViewer(); + // PdfSharp font resolver — required for .NET 8 (no system font access without it) + PdfSharp.Fonts.GlobalFontSettings.FontResolver = + EnvelopeGenerator.Server.Services.PdfSharpFontResolver.Instance; + // Configuration Options builder.Services.Configure( builder.Configuration.GetSection("ApiOptions")); diff --git a/EnvelopeGenerator.Server/EnvelopeGenerator.Server/Services/PdfSharpFontResolver.cs b/EnvelopeGenerator.Server/EnvelopeGenerator.Server/Services/PdfSharpFontResolver.cs new file mode 100644 index 00000000..84acfe22 --- /dev/null +++ b/EnvelopeGenerator.Server/EnvelopeGenerator.Server/Services/PdfSharpFontResolver.cs @@ -0,0 +1,46 @@ +using PdfSharp.Fonts; + +namespace EnvelopeGenerator.Server.Services; + +/// +/// PdfSharp 6.x IFontResolver for .NET 8. +/// PdfSharp cannot access system fonts on .NET Core/8 without an explicit resolver. +/// This implementation reads fonts directly from the Windows Fonts folder. +/// Register once at startup: GlobalFontSettings.FontResolver = PdfSharpFontResolver.Instance; +/// +public class PdfSharpFontResolver : IFontResolver +{ + public static readonly PdfSharpFontResolver Instance = new(); + + private static readonly string FontsFolder = + Environment.GetFolderPath(Environment.SpecialFolder.Fonts); + + public FontResolverInfo? ResolveTypeface(string familyName, bool isBold, bool isItalic) + { + var key = familyName.ToLowerInvariant() switch + { + "arial" => isBold ? "arialbd" : "arial", + _ => null + }; + + return key is null ? null : new FontResolverInfo(key); + } + + public byte[] GetFont(string faceName) + { + var fileName = faceName switch + { + "arialbd" => "arialbd.ttf", + _ => "arial.ttf", + }; + + var path = Path.Combine(FontsFolder, fileName); + + if (!File.Exists(path)) + throw new FileNotFoundException( + $"Font file not found: {path}. " + + "Ensure Arial is installed on the server."); + + return File.ReadAllBytes(path); + } +}