You will need to Post the HTML string to server and convert it to PDF.
HTML
<div id="dvTable">
<table cellspacing="0" rules="all" border="1">
<tr>
<th scope="col" style="width: 120px;background-color:#D20B0C">
Customer Id
</th>
<th scope="col" style="width: 150px;background-color:#D20B0C">
Name
</th>
<th scope="col" style="width: 100px;background-color:#D20B0C">
Country
</th>
</tr>
<tr>
<td>
ALFKI
</td>
<td>
Maria Anders
</td>
<td>
Germany
</td>
</tr>
<tr>
<td>
ANATR
</td>
<td>
Ana Trujillo
</td>
<td>
Mexico
</td>
</tr>
</table>
</div>
<input type="hidden" id="hfTableHTML" name="Table_HTML" />
<hr />
<asp:Button Text="Export" runat="server" OnClick="Export" OnClientClick="SetHTML()" />
<script type="text/javascript">
function SetHTML() {
document.getElementById("hfTableHTML").value = document.getElementById("dvTable").innerHTML;
}
</script>
Namespaces
using System.IO;
using System.Text;
using System.Data;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
Code
protected void Export(object sender, EventArgs e)
{
//Export HTML String as PDF.
string html = Request.Form["Table_HTML"];
StringReader sr = new StringReader(html);
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Table.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
}
And if you get error refer
ASP.Net Error: A potentially dangerous Request.Form value was detected from the client