Hi ihechi,
Using the article i have modified the sample code which suit your requirement.
Note: If you save the file in the folder path, then it will be saved in the server folder after publish.
Refer below sample.
HTML
<div id="Grid">
<table cellspacing="0" cellpadding="2" style="border-collapse: collapse; border: 1px solid #ccc; font-size: 9pt;">
<tr>
<th style="background-color: #B8DBFD; border: 1px solid #ccc">Customer Id</th>
<th style="background-color: #B8DBFD; border: 1px solid #ccc">Name</th>
<th style="background-color: #B8DBFD; border: 1px solid #ccc">Country</th>
</tr>
<tr>
<td style="width: 120px; border: 1px solid #ccc">1</td>
<td style="width: 150px; border: 1px solid #ccc">John Hammond</td>
<td style="width: 120px; border: 1px solid #ccc">United States</td>
</tr>
<tr>
<td style="width: 120px; border: 1px solid #ccc">2</td>
<td style="width: 150px; border: 1px solid #ccc">Mudassar Khan</td>
<td style="width: 120px; border: 1px solid #ccc">India</td>
</tr>
<tr>
<td style="width: 120px; border: 1px solid #ccc">3</td>
<td style="width: 150px; border: 1px solid #ccc">Suzanne Mathews</td>
<td style="width: 120px; border: 1px solid #ccc">France</td>
</tr>
<tr>
<td style="width: 120px; border: 1px solid #ccc">4</td>
<td style="width: 150px; border: 1px solid #ccc">Robert Schidner</td>
<td style="width: 120px; border: 1px solid #ccc">Russia</td>
</tr>
</table>
</div>
<br />
<asp:HiddenField ID="hfGridHtml" runat="server" />
<asp:Button ID="btnExport" runat="server" Text="Send Email" OnClick="SendEmail" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("[id*=btnExport]").click(function () {
$("[id*=hfGridHtml]").val($("#Grid").html());
});
});
</script>
Namespaces
C#
using System.IO;
using System.Net;
using System.Net.Mail;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.tool.xml;
VB.Net
Imports System.IO
Imports System.Net
Imports System.Net.Mail
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.tool.xml
Code
C#
protected void SendEmail(object sender, EventArgs e)
{
string fileName = "HTML.pdf";
string filePath = Server.MapPath(string.Format("~/Files/{0}", fileName));
using (FileStream stream = new FileStream(filePath, FileMode.Create))
{
StringReader sr = new StringReader(Request.Form[hfGridHtml.UniqueID]);
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream);
pdfDoc.Open();
XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
pdfDoc.Close();
stream.Close();
}
// Sending email code.
MailMessage mm = new MailMessage("sender.com", "receiver@gmail.com");
mm.Subject = "HTML Exported PDF";
mm.Body = "HTML Exported PDF Attachment";
mm.Attachments.Add(new Attachment(filePath));
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential();
NetworkCred.UserName = "sender@gmail.com";
NetworkCred.Password = "<passord>";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
}
VB.Net
Protected Sub SendEmail(ByVal sender As Object, ByVal e As EventArgs)
Dim fileName As String = "HTML.pdf"
Dim filePath As String = Server.MapPath(String.Format("~/Files/{0}", fileName))
Using stream As FileStream = New FileStream(filePath, FileMode.Create)
Dim sr As StringReader = New StringReader(Request.Form(hfGridHtml.UniqueID))
Dim pdfDoc As Document = New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0F)
Dim writer As PdfWriter = PdfWriter.GetInstance(pdfDoc, stream)
pdfDoc.Open()
XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr)
pdfDoc.Close()
stream.Close()
End Using
' Sending email code.
Dim mm As MailMessage = New MailMessage("sender.com", "receiver@gmail.com")
mm.Subject = "HTML Exported PDF"
mm.Body = "HTML Exported PDF Attachment"
mm.Attachments.Add(New Attachment(filePath))
mm.IsBodyHtml = True
Dim smtp As SmtpClient = New SmtpClient()
smtp.Host = "smtp.gmail.com"
smtp.EnableSsl = True
Dim NetworkCred As NetworkCredential = New NetworkCredential()
NetworkCred.UserName = "sender@gmail.com"
NetworkCred.Password = "<passord>"
smtp.UseDefaultCredentials = True
smtp.Credentials = NetworkCred
smtp.Port = 587
smtp.Send(mm)
End Sub
Screenshot