Skip to content
Merged
Changes from all commits
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
42 changes: 39 additions & 3 deletions Source/HtmlRenderer.PdfSharp/PdfGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,44 @@ public static PdfDocument GeneratePdf(string html, PdfGenerateConfig config, Css
{
// create PDF document to render the HTML into
var document = new PdfDocument();


// add rendered PDF pages to document
AddPdfPages(document, html, config, cssData, stylesheetLoad, imageLoad);

return document;
}

/// <summary>
/// Create PDF pages from given HTML and appends them to the provided PDF document.<br/>
/// </summary>
/// <param name="document">PDF document to append pages to</param>
/// <param name="html">HTML source to create PDF from</param>
/// <param name="pageSize">the page size to use for each page in the generated pdf </param>
/// <param name="margin">the margin to use between the HTML and the edges of each page</param>
/// <param name="cssData">optional: the style to use for html rendering (default - use W3 default style)</param>
/// <param name="stylesheetLoad">optional: can be used to overwrite stylesheet resolution logic</param>
/// <param name="imageLoad">optional: can be used to overwrite image resolution logic</param>
/// <returns>the generated image of the html</returns>
public static void AddPdfPages(PdfDocument document, string html, PageSize pageSize, int margin = 20, CssData cssData = null, EventHandler<HtmlStylesheetLoadEventArgs> stylesheetLoad = null, EventHandler<HtmlImageLoadEventArgs> imageLoad = null)
{
var config = new PdfGenerateConfig();
config.PageSize = pageSize;
config.SetMargins(margin);
AddPdfPages(document, html, config, cssData, stylesheetLoad, imageLoad);
}

/// <summary>
/// Create PDF pages from given HTML and appends them to the provided PDF document.<br/>
/// </summary>
/// <param name="document">PDF document to append pages to</param>
/// <param name="html">HTML source to create PDF from</param>
/// <param name="config">the configuration to use for the PDF generation (page size/page orientation/margins/etc.)</param>
/// <param name="cssData">optional: the style to use for html rendering (default - use W3 default style)</param>
/// <param name="stylesheetLoad">optional: can be used to overwrite stylesheet resolution logic</param>
/// <param name="imageLoad">optional: can be used to overwrite image resolution logic</param>
/// <returns>the generated image of the html</returns>
public static void AddPdfPages(PdfDocument document, string html, PdfGenerateConfig config, CssData cssData = null, EventHandler<HtmlStylesheetLoadEventArgs> stylesheetLoad = null, EventHandler<HtmlImageLoadEventArgs> imageLoad = null)
{
XSize orgPageSize;
// get the size of each page to layout the HTML in
if (config.PageSize != PageSize.Undefined)
Expand Down Expand Up @@ -146,11 +183,10 @@ public static PdfDocument GeneratePdf(string html, PdfGenerateConfig config, Css
HandleLinks(document, container, orgPageSize, pageSize);
}
}

return document;
}



#region Private/Protected methods

/// <summary>
Expand Down