Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8bf427e

Browse files
committed
feat(PdfGenerator): allow generated pages to be appended to a PdfDocument
Adds a new method AddPdfPages that generates and appends pages to the specified PdfDocument. This allows the creation of a PdfDocument page by page, where each set of pages can have different layouts and allows better control of page breaks/contents.
1 parent ccc164c commit 8bf427e

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

Source/HtmlRenderer.PdfSharp/PdfGenerator.cs

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,44 @@ public static PdfDocument GeneratePdf(string html, PdfGenerateConfig config, Css
8989
{
9090
// create PDF document to render the HTML into
9191
var document = new PdfDocument();
92-
92+
93+
// add rendered PDF pages to document
94+
AddPdfPages(document, html, config, cssData, stylesheetLoad, imageLoad);
95+
96+
return document;
97+
}
98+
99+
/// <summary>
100+
/// Create PDF pages from given HTML and appends them to the provided PDF document.<br/>
101+
/// </summary>
102+
/// <param name="document">PDF document to append pages to</param>
103+
/// <param name="html">HTML source to create PDF from</param>
104+
/// <param name="pageSize">the page size to use for each page in the generated pdf </param>
105+
/// <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>
107+
/// <param name="stylesheetLoad">optional: can be used to overwrite stylesheet resolution logic</param>
108+
/// <param name="imageLoad">optional: can be used to overwrite image resolution logic</param>
109+
/// <returns>the generated image of the html</returns>
110+
public static void AddPdfPages(PdfDocument document, string html, PageSize pageSize, int margin = 20, CssData cssData = null, EventHandler<HtmlStylesheetLoadEventArgs> stylesheetLoad = null, EventHandler<HtmlImageLoadEventArgs> imageLoad = null)
111+
{
112+
var config = new PdfGenerateConfig();
113+
config.PageSize = pageSize;
114+
config.SetMargins(margin);
115+
AddPdfPages(document, html, config, cssData, stylesheetLoad, imageLoad);
116+
}
117+
118+
/// <summary>
119+
/// Create PDF pages from given HTML and appends them to the provided PDF document.<br/>
120+
/// </summary>
121+
/// <param name="document">PDF document to append pages to</param>
122+
/// <param name="html">HTML source to create PDF from</param>
123+
/// <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>
125+
/// <param name="stylesheetLoad">optional: can be used to overwrite stylesheet resolution logic</param>
126+
/// <param name="imageLoad">optional: can be used to overwrite image resolution logic</param>
127+
/// <returns>the generated image of the html</returns>
128+
public static void AddPdfPages(PdfDocument document, string html, PdfGenerateConfig config, CssData cssData = null, EventHandler<HtmlStylesheetLoadEventArgs> stylesheetLoad = null, EventHandler<HtmlImageLoadEventArgs> imageLoad = null)
129+
{
93130
XSize orgPageSize;
94131
// get the size of each page to layout the HTML in
95132
if (config.PageSize != PageSize.Undefined)
@@ -146,11 +183,10 @@ public static PdfDocument GeneratePdf(string html, PdfGenerateConfig config, Css
146183
HandleLinks(document, container, orgPageSize, pageSize);
147184
}
148185
}
149-
150-
return document;
151186
}
152187

153188

189+
154190
#region Private/Protected methods
155191

156192
/// <summary>

0 commit comments

Comments
 (0)