Convert HTML to PDF using HtmlRenderer

slayernoah picture slayernoah · Nov 4, 2016 · Viewed 55.2k times · Source

I am looking to use HtmlRenderer to convert an HTML file to a PDF file. I have seen it being mentioned on several sites that it is possible. However, I don't seem to be able to find any basic sample code to do this.

I have added the following NuGet package.

Install-Package HtmlRenderer.PdfSharp

That's about as far I have got. Any help will be greatly appreciated.

Answer

slayernoah picture slayernoah · Nov 4, 2016

Found a solution:

using TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerator;

string html = File.ReadAllText("input.htm");
PdfDocument pdf = PdfGenerator.GeneratePdf(html, PageSize.Letter);
pdf.Save("document.pdf");