1414using PdfSharp . Drawing ;
1515using PdfSharp . Pdf ;
1616using System ;
17+ using TheArtOfDev . HtmlRenderer . Adapters ;
1718using TheArtOfDev . HtmlRenderer . Core ;
1819using TheArtOfDev . HtmlRenderer . Core . Entities ;
1920using TheArtOfDev . HtmlRenderer . Core . Utils ;
@@ -28,11 +29,11 @@ public static class PdfGenerator
2829 {
2930 /// <summary>
3031 /// Adds a font mapping from <paramref name="fromFamily"/> to <paramref name="toFamily"/> iff the <paramref name="fromFamily"/> is not found.<br/>
31- /// When the <paramref name="fromFamily"/> font is used in rendered html and is not found in existing
32+ /// When the <paramref name="fromFamily"/> font is used in rendered HTML and is not found in existing
3233 /// fonts (installed or added) it will be replaced by <paramref name="toFamily"/>.<br/>
3334 /// </summary>
3435 /// <remarks>
35- /// This fonts mapping can be used as a fallback in case the requested font is not installed in the client system.
36+ /// This font mapping can be used as a fallback in case the requested font is not installed in the client system.
3637 /// </remarks>
3738 /// <param name="fromFamily">the font family to replace</param>
3839 /// <param name="toFamily">the font family to replace with</param>
@@ -44,15 +45,44 @@ public static void AddFontFamilyMapping(string fromFamily, string toFamily)
4445 PdfSharpAdapter . Instance . AddFontFamilyMapping ( fromFamily , toFamily ) ;
4546 }
4647
48+ /// <summary>
49+ /// Adds a custom font family to be used during HTML rendering.<br/>
50+ /// </summary>
51+ /// <remarks>
52+ /// This allows you to register fonts that are not installed on the system.
53+ /// </remarks>
54+ /// <param name="fontFamily">the font family to add</param>
55+ public static void AddFontFamily ( RFontFamily fontFamily )
56+ {
57+ ArgChecker . AssertArgNotNull ( fontFamily , "fontFamily" ) ;
58+
59+ PdfSharpAdapter . Instance . AddFontFamily ( fontFamily ) ;
60+ }
61+
62+ /// <summary>
63+ /// Registers a custom directory to search for font files (TTF/OTF).<br/>
64+ /// Fonts in this directory will be discovered and made available for rendering.
65+ /// </summary>
66+ /// <remarks>
67+ /// After calling this method, you should call <see cref="AddFontFamily"/> with fonts from this directory.
68+ /// </remarks>
69+ /// <param name="fontDirectory">the directory path containing font files</param>
70+ public static void RegisterCustomFontDirectory ( string fontDirectory )
71+ {
72+ ArgChecker . AssertArgNotNullOrEmpty ( fontDirectory , "fontDirectory" ) ;
73+
74+ PdfSharpAdapter . Instance . FontResolver . RegisterCustomFontDirectory ( fontDirectory ) ;
75+ }
76+
4777 /// <summary>
4878 /// Parse the given stylesheet to <see cref="CssData"/> object.<br/>
49- /// If <paramref name="combineWithDefault"/> is true the parsed css blocks are added to the
50- /// default css data (as defined by W3), merged if class name already exists. If false only the data in the given stylesheet is returned.
79+ /// If <paramref name="combineWithDefault"/> is true the parsed CSS blocks are added to the
80+ /// default CSS data (as defined by W3), merged if class name already exists. If false only the data in the given stylesheet is returned.
5181 /// </summary>
5282 /// <seealso cref="http://www.w3.org/TR/CSS21/sample.html"/>
5383 /// <param name="stylesheet">the stylesheet source to parse</param>
54- /// <param name="combineWithDefault">true - combine the parsed css data with default css data, false - return only the parsed css data</param>
55- /// <returns>the parsed css data</returns>
84+ /// <param name="combineWithDefault">true - combine the parsed CSS data with default CSS data, false - return only the parsed CSS data</param>
85+ /// <returns>the parsed CSS data</returns>
5686 public static CssData ParseStyleSheet ( string stylesheet , bool combineWithDefault = true )
5787 {
5888 return CssData . Parse ( PdfSharpAdapter . Instance , stylesheet , combineWithDefault ) ;
@@ -64,10 +94,10 @@ public static CssData ParseStyleSheet(string stylesheet, bool combineWithDefault
6494 /// <param name="html">HTML source to create PDF from</param>
6595 /// <param name="pageSize">the page size to use for each page in the generated pdf </param>
6696 /// <param name="margin">the margin to use between the HTML and the edges of each page</param>
67- /// <param name="cssData">optional: the style to use for html rendering (default - use W3 default style)</param>
97+ /// <param name="cssData">optional: the style to use for HTML rendering (default - use W3 default style)</param>
6898 /// <param name="stylesheetLoad">optional: can be used to overwrite stylesheet resolution logic</param>
6999 /// <param name="imageLoad">optional: can be used to overwrite image resolution logic</param>
70- /// <returns>the generated image of the html </returns>
100+ /// <returns>the generated image of the HTML </returns>
71101 public static PdfDocument GeneratePdf ( string html , PageSize pageSize , int margin = 20 , CssData cssData = null , EventHandler < HtmlStylesheetLoadEventArgs > stylesheetLoad = null , EventHandler < HtmlImageLoadEventArgs > imageLoad = null )
72102 {
73103 var config = new PdfGenerateConfig ( ) ;
@@ -81,10 +111,10 @@ public static PdfDocument GeneratePdf(string html, PageSize pageSize, int margin
81111 /// </summary>
82112 /// <param name="html">HTML source to create PDF from</param>
83113 /// <param name="config">the configuration to use for the PDF generation (page size/page orientation/margins/etc.)</param>
84- /// <param name="cssData">optional: the style to use for html rendering (default - use W3 default style)</param>
114+ /// <param name="cssData">optional: the style to use for HTML rendering (default - use W3 default style)</param>
85115 /// <param name="stylesheetLoad">optional: can be used to overwrite stylesheet resolution logic</param>
86116 /// <param name="imageLoad">optional: can be used to overwrite image resolution logic</param>
87- /// <returns>the generated image of the html </returns>
117+ /// <returns>the generated image of the HTML </returns>
88118 public static PdfDocument GeneratePdf ( string html , PdfGenerateConfig config , CssData cssData = null , EventHandler < HtmlStylesheetLoadEventArgs > stylesheetLoad = null , EventHandler < HtmlImageLoadEventArgs > imageLoad = null )
89119 {
90120 // create PDF document to render the HTML into
@@ -103,10 +133,10 @@ public static PdfDocument GeneratePdf(string html, PdfGenerateConfig config, Css
103133 /// <param name="html">HTML source to create PDF from</param>
104134 /// <param name="pageSize">the page size to use for each page in the generated pdf </param>
105135 /// <param name="margin">the margin to use between the HTML and the edges of each page</param>
106- /// <param name="cssData">optional: the style to use for html rendering (default - use W3 default style)</param>
136+ /// <param name="cssData">optional: the style to use for HTML rendering (default - use W3 default style)</param>
107137 /// <param name="stylesheetLoad">optional: can be used to overwrite stylesheet resolution logic</param>
108138 /// <param name="imageLoad">optional: can be used to overwrite image resolution logic</param>
109- /// <returns>the generated image of the html </returns>
139+ /// <returns>the generated image of the HTML </returns>
110140 public static void AddPdfPages ( PdfDocument document , string html , PageSize pageSize , int margin = 20 , CssData cssData = null , EventHandler < HtmlStylesheetLoadEventArgs > stylesheetLoad = null , EventHandler < HtmlImageLoadEventArgs > imageLoad = null )
111141 {
112142 var config = new PdfGenerateConfig ( ) ;
@@ -121,10 +151,10 @@ public static void AddPdfPages(PdfDocument document, string html, PageSize pageS
121151 /// <param name="document">PDF document to append pages to</param>
122152 /// <param name="html">HTML source to create PDF from</param>
123153 /// <param name="config">the configuration to use for the PDF generation (page size/page orientation/margins/etc.)</param>
124- /// <param name="cssData">optional: the style to use for html rendering (default - use W3 default style)</param>
154+ /// <param name="cssData">optional: the style to use for HTML rendering (default - use W3 default style)</param>
125155 /// <param name="stylesheetLoad">optional: can be used to overwrite stylesheet resolution logic</param>
126156 /// <param name="imageLoad">optional: can be used to overwrite image resolution logic</param>
127- /// <returns>the generated image of the html </returns>
157+ /// <returns>the generated image of the HTML </returns>
128158 public static void AddPdfPages ( PdfDocument document , string html , PdfGenerateConfig config , CssData cssData = null , EventHandler < HtmlStylesheetLoadEventArgs > stylesheetLoad = null , EventHandler < HtmlImageLoadEventArgs > imageLoad = null )
129159 {
130160 XSize orgPageSize ;
0 commit comments