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




Picture showing the HTML file that is to be converted to PDF 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


Picture showing the converted the PDF file
Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Friday, October 2, 2020

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250