Amit Shrivastava

Amit Shrivastava

  • 1.8k
  • 293
  • 160.8k

How to convert html to pdf in c#

Feb 3 2015 2:48 AM
Hi,
how can i convert html template to pdf file in c# windows application
 using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter hw = new HtmlTextWriter(sw))
            {
           using (StreamReader sr= new StreamReader(Server.MapPath("~/EmailTemplate.htm")))
                Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 10f, 0f);
                HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
                PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
                pdfDoc.Open();
                htmlparser.Parse(sr);
                pdfDoc.Add(pdfTable);
                pdfDoc.Close();
                Response.ContentType = "application/pdf";
                Response.AddHeader("content-disposition", "attachment;filename=HTMLExport.pdf");
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Response.Write(pdfDoc);
                Response.End();
            }
        }

Answers (12)