diff --git a/Source/HtmlRenderer.PdfSharp/Adapters/PdfSharpAdapter.cs b/Source/HtmlRenderer.PdfSharp/Adapters/PdfSharpAdapter.cs
index 16652f550..4e74601bc 100644
--- a/Source/HtmlRenderer.PdfSharp/Adapters/PdfSharpAdapter.cs
+++ b/Source/HtmlRenderer.PdfSharp/Adapters/PdfSharpAdapter.cs
@@ -33,6 +33,11 @@ internal sealed class PdfSharpAdapter : RAdapter
///
private static readonly PdfSharpAdapter _instance = new PdfSharpAdapter();
+ ///
+ /// Font resolver instance for managing font discovery and resolution.
+ ///
+ private FontResolver _fontResolver;
+
#endregion
@@ -41,12 +46,12 @@ internal sealed class PdfSharpAdapter : RAdapter
///
private PdfSharpAdapter()
{
- var fontResolver = FontResolver.Register();
+ _fontResolver = FontResolver.Register();
AddFontFamilyMapping("monospace", "Courier New");
AddFontFamilyMapping("Helvetica", "Arial");
- var fontFamilies = fontResolver.DiscoverFontFamilies();
+ var fontFamilies = _fontResolver.DiscoverFontFamilies();
foreach (var fontFamily in fontFamilies)
{
@@ -62,6 +67,14 @@ public static PdfSharpAdapter Instance
get { return _instance; }
}
+ ///
+ /// Get the FontResolver instance for advanced font management.
+ ///
+ internal FontResolver FontResolver
+ {
+ get { return _fontResolver; }
+ }
+
protected override RColor GetColorInt(string colorName)
{
try
diff --git a/Source/HtmlRenderer.PdfSharp/FontResolution/FontResolver.cs b/Source/HtmlRenderer.PdfSharp/FontResolution/FontResolver.cs
index 373f9a1c7..189acc045 100644
--- a/Source/HtmlRenderer.PdfSharp/FontResolution/FontResolver.cs
+++ b/Source/HtmlRenderer.PdfSharp/FontResolution/FontResolver.cs
@@ -101,6 +101,9 @@ public byte[] GetFont(string faceName)
public void RegisterCustomFontDirectory(string fontDirectory)
{
_fontDiscoveryService.RegisterCustomFontDirectory(fontDirectory);
+
+ // Invalidate the cache to trigger discovery
+ _fontMetadataCache.Clear();
}
public List DiscoverFontFamilies()
diff --git a/Source/HtmlRenderer.PdfSharp/PdfGenerator.cs b/Source/HtmlRenderer.PdfSharp/PdfGenerator.cs
index 3e63ab1cc..6cdb74831 100644
--- a/Source/HtmlRenderer.PdfSharp/PdfGenerator.cs
+++ b/Source/HtmlRenderer.PdfSharp/PdfGenerator.cs
@@ -14,6 +14,7 @@
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using System;
+using TheArtOfDev.HtmlRenderer.Adapters;
using TheArtOfDev.HtmlRenderer.Core;
using TheArtOfDev.HtmlRenderer.Core.Entities;
using TheArtOfDev.HtmlRenderer.Core.Utils;
@@ -28,11 +29,11 @@ public static class PdfGenerator
{
///
/// Adds a font mapping from to iff the is not found.
- /// When the font is used in rendered html and is not found in existing
+ /// When the font is used in rendered HTML and is not found in existing
/// fonts (installed or added) it will be replaced by .
///
///
- /// This fonts mapping can be used as a fallback in case the requested font is not installed in the client system.
+ /// This font mapping can be used as a fallback in case the requested font is not installed in the client system.
///
/// the font family to replace
/// the font family to replace with
@@ -44,15 +45,38 @@ public static void AddFontFamilyMapping(string fromFamily, string toFamily)
PdfSharpAdapter.Instance.AddFontFamilyMapping(fromFamily, toFamily);
}
+ ///
+ /// Registers a custom directory to search for font files (TTF/OTF).
+ /// Fonts in this directory will be discovered and made available for rendering.
+ ///
+ ///
+ /// This will automatically trigger a rediscovery of all fonts.
+ ///
+ /// the directory path containing font files
+ public static void RegisterCustomFontDirectory(string fontDirectory)
+ {
+ ArgChecker.AssertArgNotNullOrEmpty(fontDirectory, "fontDirectory");
+
+ PdfSharpAdapter.Instance.FontResolver.RegisterCustomFontDirectory(fontDirectory);
+
+ // Trigger a rediscovery
+ var fontFamilies = PdfSharpAdapter.Instance.FontResolver.DiscoverFontFamilies();
+
+ foreach (var fontFamily in fontFamilies)
+ {
+ PdfSharpAdapter.Instance.AddFontFamily(new FontFamilyAdapter(new XFontFamily(fontFamily)));
+ }
+ }
+
///
/// Parse the given stylesheet to object.
- /// If is true the parsed css blocks are added to the
- /// default css data (as defined by W3), merged if class name already exists. If false only the data in the given stylesheet is returned.
+ /// If is true the parsed CSS blocks are added to the
+ /// default CSS data (as defined by W3), merged if class name already exists. If false only the data in the given stylesheet is returned.
///
///
/// the stylesheet source to parse
- /// true - combine the parsed css data with default css data, false - return only the parsed css data
- /// the parsed css data
+ /// true - combine the parsed CSS data with default CSS data, false - return only the parsed CSS data
+ /// the parsed CSS data
public static CssData ParseStyleSheet(string stylesheet, bool combineWithDefault = true)
{
return CssData.Parse(PdfSharpAdapter.Instance, stylesheet, combineWithDefault);
@@ -64,10 +88,10 @@ public static CssData ParseStyleSheet(string stylesheet, bool combineWithDefault
/// HTML source to create PDF from
/// the page size to use for each page in the generated pdf
/// the margin to use between the HTML and the edges of each page
- /// optional: the style to use for html rendering (default - use W3 default style)
+ /// optional: the style to use for HTML rendering (default - use W3 default style)
/// optional: can be used to overwrite stylesheet resolution logic
/// optional: can be used to overwrite image resolution logic
- /// the generated image of the html
+ /// the generated image of the HTML
public static PdfDocument GeneratePdf(string html, PageSize pageSize, int margin = 20, CssData cssData = null, EventHandler stylesheetLoad = null, EventHandler imageLoad = null)
{
var config = new PdfGenerateConfig();
@@ -81,10 +105,10 @@ public static PdfDocument GeneratePdf(string html, PageSize pageSize, int margin
///
/// HTML source to create PDF from
/// the configuration to use for the PDF generation (page size/page orientation/margins/etc.)
- /// optional: the style to use for html rendering (default - use W3 default style)
+ /// optional: the style to use for HTML rendering (default - use W3 default style)
/// optional: can be used to overwrite stylesheet resolution logic
/// optional: can be used to overwrite image resolution logic
- /// the generated image of the html
+ /// the generated image of the HTML
public static PdfDocument GeneratePdf(string html, PdfGenerateConfig config, CssData cssData = null, EventHandler stylesheetLoad = null, EventHandler imageLoad = null)
{
// create PDF document to render the HTML into
@@ -103,10 +127,10 @@ public static PdfDocument GeneratePdf(string html, PdfGenerateConfig config, Css
/// HTML source to create PDF from
/// the page size to use for each page in the generated pdf
/// the margin to use between the HTML and the edges of each page
- /// optional: the style to use for html rendering (default - use W3 default style)
+ /// optional: the style to use for HTML rendering (default - use W3 default style)
/// optional: can be used to overwrite stylesheet resolution logic
/// optional: can be used to overwrite image resolution logic
- /// the generated image of the html
+ /// the generated image of the HTML
public static void AddPdfPages(PdfDocument document, string html, PageSize pageSize, int margin = 20, CssData cssData = null, EventHandler stylesheetLoad = null, EventHandler imageLoad = null)
{
var config = new PdfGenerateConfig();
@@ -121,10 +145,10 @@ public static void AddPdfPages(PdfDocument document, string html, PageSize pageS
/// PDF document to append pages to
/// HTML source to create PDF from
/// the configuration to use for the PDF generation (page size/page orientation/margins/etc.)
- /// optional: the style to use for html rendering (default - use W3 default style)
+ /// optional: the style to use for HTML rendering (default - use W3 default style)
/// optional: can be used to overwrite stylesheet resolution logic
/// optional: can be used to overwrite image resolution logic
- /// the generated image of the html
+ /// the generated image of the HTML
public static void AddPdfPages(PdfDocument document, string html, PdfGenerateConfig config, CssData cssData = null, EventHandler stylesheetLoad = null, EventHandler imageLoad = null)
{
XSize orgPageSize;