Articles → CSHARP → Convert The HTML File To PDF Using C#
Convert The HTML File To PDF Using C#
Install Htmlrenderer.Pdfsharp
Install-Package HtmlRenderer.PdfSharp -Version 1.5.0.6
HTML File
Click to Enlarge
Code
string html = File.ReadAllText(@"C:\Users\karan\Downloads\karan.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();
using(Stream file = File.OpenWrite(@"C:\Users\karan\Downloads\karan.pdf")) {
file.Write(res, 0, res.Length);
}
}
Output
Click to Enlarge