1

I have a email that's send out as a Receipt to a payment. This email contains a HTML Table that contains all the information.It writes that table as a body of the email.

         protected void btnSendEmail_Click(object sender, EventArgs e)
        {
          var stringWrite = new StringWriter();
            var htmlWrite = new HtmlTextWriter(stringWrite);
            Table1.RenderControl(htmlWrite);
 string sBody = stringWrite.ToString();
    }

So the body of the email is the Receipt. I want to convert that HTML table that goes in the email as a body with all the values in it into a pdf and send it as a attachment. Please let me know if you know how to. Thanks

Simple Code
  • 558
  • 9
  • 24

2 Answers2

1

Use the .Net Library hiqpdf (you can install it from nuget: https://www.nuget.org/packages/hiqpdf/). It can generate pdf from the HTML you provide to it. Then use the byte array returned by that method and add it as attachment with the name containing .pdf extension:

Attachment att = new Attachment(new MemoryStream(bytes), name);

Gurvinder Singh
  • 91
  • 2
  • 10
1

So i found a way of doing it so. Pick up where i left off.

    string sBody = stringWrite.ToString();
  StringReader sr = new StringReader(stringWrite.ToString());
        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);

Where we send email

        msg.Subject = sSubject;

        msg.Body = sBody;

        msg.IsBodyHtml = true;

        var smpt = new SmtpClient();
        smpt.Port = ###;
        using (MemoryStream memoryStream = new MemoryStream())
        {
            PdfWriter writer = PdfWriter.GetInstance(pdfDoc, memoryStream);
            pdfDoc.Open();
            htmlparser.Parse(sr);
            pdfDoc.Close();
            byte[] bytes = memoryStream.ToArray();
            memoryStream.Close();
            msg.Attachments.Add(new Attachment(new MemoryStream(bytes), "RECEIPT.pdf"));
            smpt.DeliveryMethod = SmtpDeliveryMethod.Network;
 smpt.Send(msg);

very Simple you need iTextSharp for this that's all and it has a pdfwriter option that will write it for you and send it in email. thanks for viewing. Credit to this POST

Simple Code
  • 558
  • 9
  • 24