Click here to Skip to main content
14,983,551 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Dear All

I am trying to export html to pdf in following way

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
using System.IO;
using System.Collections;



public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    // Below code is used to convert your string HTML value to PDF
    protected void btnExportPDF_Click(object sender, EventArgs e)
    {        
        
        //Set page size as A4
        Document pdfDoc = new Document(PageSize.A4, 10, 10, 10, 10);

        try
        {
            PdfWriter.GetInstance(pdfDoc, System.Web.HttpContext.Current.Response.OutputStream);            
            
            //Open PDF Document to write data
            pdfDoc.Open(); 
          
            //Assign Html content in a string to write in PDF
            string contents = "<h5>EXPORT HTML CONTENT TO PDF</h5><br /><br />This content is convert from html string to PDF<br /><br /><br /><font color="red">Samples from Ravi!!!</font>";
           
            //Read string contents using stream reader and convert html to parsed conent 
            var parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(contents), null);

            //Get each array values from parsed elements and add to the PDF document
            foreach (var htmlElement in parsedHtmlElements)
            pdfDoc.Add(htmlElement as IElement);

            //Close your PDF
            pdfDoc.Close();

            Response.ContentType = "application/pdf";

            //Set default file Name as current datetime
            Response.AddHeader("content-disposition", "attachment; filename=" + DateTime.Now.ToString("yyyyMMdd") + ".pdf");
            System.Web.HttpContext.Current.Response.Write(pdfDoc);

            Response.Flush();
            Response.End();
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }

    // Below code is read content from HTML file and convert to PDF 

    protected void Button1_Click(object sender, EventArgs e)
    {
        //Set page size as A4
        Document pdfDoc = new Document(PageSize.A4, 10, 10, 10, 10);

        try
        {
            PdfWriter.GetInstance(pdfDoc, System.Web.HttpContext.Current.Response.OutputStream);

            //Open PDF Document to write data
            pdfDoc.Open();

            //Assign Html content in a string to write in PDF
            string contents = "";

            StreamReader sr;
            try
            {
                //Read file from server path
                sr = File.OpenText(Server.MapPath("sample.html"));
                //store content in the variable
                contents = sr.ReadToEnd();              
                sr.Close();                
            }
            catch (Exception ex)
            {
              
            }


            var parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(contents), null);
           // ArrayList parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(contents), null);

            //Get each array values from parsed elements and add to the PDF document
            foreach (var htmlElement in parsedHtmlElements)
            pdfDoc.Add(htmlElement as IElement);




            //Close your PDF
            pdfDoc.Close();

            Response.ContentType = "application/pdf";

            //Set default file Name as current datetime
            Response.AddHeader("content-disposition", "attachment; filename=" + DateTime.Now.ToString("yyyyMMdd") + ".pdf");
            System.Web.HttpContext.Current.Response.Write(pdfDoc);

            Response.Flush();
            Response.End();

        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }
}



It works fine for

following sample.html

XML
<html>
<head>
    <title></title>
</head>
<body>
    <h5>EXPORT HTML CONTENT TO PDF</h5><br /><br /><br />
    <b>This content is convert from html file to PDF file</b><br /><br /><br />
    <font color="red">Samples from Ravi!!!</font>
</body>
</html>




but gives error in following line for following html file contents

var parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(contents), null);


error: Input string was not in a correct format.

contents in html file giving error

XML
<html>
<table width="300px" border="1" style="border-collapse: collapse; background-color: #E8DCFF">
        <tr>
            <td width="40px">1</td>
            <td>Number</td>
            <td><input class="txt" type="text" name="txt" /></td>
        </tr>
        <tr>
            <td>2</td>
            <td>Number</td>
            <td><input class="txt" type="text" name="txt" /></td>
        </tr>
        <tr>
            <td>3</td>
            <td>Number</td>
            <td><input class="txt" type="text" name="txt" /></td>
        </tr>
        <tr id="summation">
            <td  colspan ="2" align="right">
                Sum :
            </td>
            <td align="center"><span id="sum">0</span></td>
        </tr>
</table>

</html>
Posted
Updated 2-Jun-16 1:01am
v2

1 solution

Hi, You can see this discussion,Html to PDF[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900