Sunday, 19 February 2017

Convert HTML to PDF in .NET

1. Download HtmlRenderer.PdfSharp Nuget packages: more...
Install-Package HtmlRenderer.PdfSharp -Version 1.5.0.6

2. Create a method in C# to convert html to pdf
public static void CreatePdfFromHtml (string html)
{
    Byte[] res = null;
    using (MemoryStream ms = new MemoryStream())
    {
var pdf = TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerator.GeneratePdf (html, PdfSharp.PageSize.A4);
pdf.Save(ms);
res = ms.ToArray();
    }
    File.WriteAllBytes("c:\\output.pdf", res);
}


No comments:

SQL: Generate a range of numbers

SELECT ones.n + 10*tens.n + 100*hundreds.n + 1000*thousands.n FROM       (VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) ones(n),      (VALU...