Mayur  Gujrathi

Mayur Gujrathi

  • 386
  • 3.9k
  • 1m

Trying to export html to pdf

Jul 20 2012 3:07 AM
Dear All
 
I am trying to export html to pdf in following way
 
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 = "
EXPORT HTML CONTENT TO PDF


This content is convert from html string to PDF


Samples from Ravi!!!";

//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
 
<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
 
<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>

Answers (2)