Hello Forum,
I want to convert a portion of an HTML to PDF (NOT the whole HTML page) and send the PDF to email.
I got a code to export HTML to PDF but I do not want the whole HTML to be exported to PDF, except for only a portion. Example, I have this <div> tag below and I want to convert the parent div and its content to PDF and send to email. Please I need help. Thank you
C#
protected void Button1_Click(object sender, EventArgs e)
{
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
this.Page.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
using (MemoryStream memoryStream = new MemoryStream())
{
PdfWriter.GetInstance(pdfDoc, memoryStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
byte[] bytes = memoryStream.ToArray();
memoryStream.Close();
MailMessage mm = new MailMessage("georgeakpan13@gmail.com", "ukosimons@gmail.com");
mm.Subject = "Invoice PDF";
mm.Body = "Invoice PDF Attachment";
mm.Attachments.Add(new Attachment(new MemoryStream(bytes), "Invoice.pdf"));
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = "georgeakpan13@gmail.com";
NetworkCred.Password = "etekamba233#";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
}
}
}
}