[Solved] Convert HTML to PDF using iTextSharp creates blank PDF in ASP.Net

Last Reply one year ago By dharmendr

Posted one year ago

Hi again, experts,

I am still having problem with this xmlworker.

I have tried to followed the examples on the web but continue to have problems.

When I run this code, it serves up an empty PDF but I want it the PDF to contain the contents of body which is everything inside the

body = body + " ...."

Any ideas what I am doing wrong?

Many thanks in advance

            body = body + "  <div align='center'>";
            body = body + "  <table border='1' style='text-align:left; font-size:8pt;' cellspacing='0' width='101.6%'>";
            body = body + "   <tr>";
            body = body + "  <td class='auto-style5'><span id='lblTaxYr' style='color: #0093B2;font-weight:bold;font-size:8px;'>" + lblTaxYr.Text + "</span></td>";
            body = body + "  <td class='auto-style1'><div class='input text'> <span id='lblAcctNo' style='width:155px; color: #0093B2;font-weight:bold;font-size:8px;'> " + accountnumber.Text + "</span></div></td>";
            body = body + "</tr>";
            body = body + " <tr>";
            body = body + "      <td class='auto-style5'><span style='color:red;font-size:8px;'>DUE DATE</span></td>";
            body = body + "      <td colspan='2'><span style='font-size:8px;'>OWNERS PHONE NUMBER (LIST)</span></td>";
            body = body + "    </tr>";
            body = body + "    <tr>";
            body = body + "      <td class='auto-style5'><span id='lblDueDate' style='color: #0093B2;font-weight:bold;font-size:8px;'>" + lblDueDate.Text + "</span></td>";
            body = body + "      <td colspan='2' ><span id='lblOwnerPhone' style='color: #0093B2;font-weight:bold;font-size:8px;'>" + lblOwnerPhone.Text + "</span></td>";
            body = body + "    </tr>";
            body = body + "   </table>";
            body = body + "   <table height='1' cellspacing='0'><tr><td></td></tr></table>";
            body = body + "   <table border='0' style='width:100%;font-size:8pt;'>";
            body = body + "    <tr>";
            body = body + "     <td style='text-align:center'><br />Taxpayer NAME AND ADDRESS<hr /></td>";
            body = body + "    </tr>";
            body = body + "    <tr>";
            body = body + "      <td>";
            body = body + "       <table border='0' style='font-size:8px;' width='100%' cellspacing='0'>";
            body = body + "    <tr>";
            body = body + "      <td width='30%'>NAME:</td><td width='60%'><div class='input text'> <span id='lblfName' style='color: #0093B2;font-weight:bold;'>" + lblfName.Text + "</span></div></td>";
            body = body + "    </tr>";
            body = body + "    <tr>";
            body = body + "      <td width='30%'>ADDRESS:</td><td width='60%'><div class='input text'> <span id='lblfAddress' style='color: #0093B2;font-weight:bold;'>" + lblfAddress.Text + "</span></div></td>";
            body = body + "    </tr>";
            body = body + "     </table>";
            body = body + "     <table border='0' style='font-size:8pt;' width='100%' cellspacing='0'>";
            body = body + "     <tr>";
            body = body + "     <td width='15%'><span style='margin-left:2px;color:#000;font-size:8pt'>CITY:</span> </td><td width='25%'><div class='input text'> <span id='lblfCity' style='color: #0093B2;font-weight:bold;'>" + lblfCity.Text + "</span></div></td><td width='20%' style='font-size:10pt'>&nbsp;STATE:</td><td width='25%'><div class='input select'> ";
            body = body + "      <span id='lblfStates' CssClass='Label'>" + lblfStates.Text + "</span></div></td><td width='10%'>&nbsp;&nbsp;ZIP:</td><td width='15%'><div class='input text'> <span id='lblfZip' style='color: #0093B2;font-weight:bold;font-size:8pt'>" + lblfZip.Text + "</span></div></td>";
            body = body + "    </tr>";
            body = body + "     </table>";
            body = body + "    </td>";
            body = body + "    </tr>";
            body = body + "   <tr>";
            body = body + "   <td><br /><br />";
            body = body + "   <span style='font-weight:bold'>PERSONAL PROPERTY STRATA</span>";
            body = body + "   </td>";
            body = body + "   </tr>";
            body = body + "  </table> ";
           
        }
        strHtml = strWriter.ToString();
        strWriter.Close();
        strWriter.Dispose();
        string strFileShortName = accountnumber.Text + DateTime.Now.Ticks + ".pdf";
        string strFileName = HttpContext.Current.Server.MapPath("PDF_FIles\\" + strFileShortName);
        iTextSharp.text.Document docWorkingDocument = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4.Rotate(), 1, 1, 0, 0);
        StringReader srdDocToString = new StringReader(body.ToString()); 
        try
        {
            PdfWriter pdfWrite;
            pdfWrite = PdfWriter.GetInstance(docWorkingDocument, new FileStream(strFileName, FileMode.Create));
            XMLWorkerHelper.GetInstance().ParseXHtml(pdfWrite, docWorkingDocument, srdDocToString);
            srdDocToString = new StringReader(strHtml);
            docWorkingDocument.Open();
            XMLWorkerHelper.GetInstance().ParseXHtml(pdfWrite, docWorkingDocument, srdDocToString);
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
        //finally
        //{
        
        //}

        using (MemoryStream memoryStream = new MemoryStream())
        {
            PdfWriter writer = PdfWriter.GetInstance(docWorkingDocument, memoryStream);
            docWorkingDocument.Open();
            var logo = iTextSharp.text.Image.GetInstance(Server.MapPath("~/Images/Receipt.png")); //header logo
            logo.SetAbsolutePosition(29, 680);
            docWorkingDocument.Add(logo);
            docWorkingDocument.Close();
            byte[] bytes = memoryStream.ToArray();
            memoryStream.Close();
            //Address of email sender1+/
            string yourEmail = txtEmail.Text;
            //Create the msg object to be sent
            MailMessage msg = new MailMessage();

            //Let's ready image to be sent as and displayed as logo.
            //first grab as an attachment
            string contentID = "Image";
            Attachment inlineLogo = new Attachment("C:\\Inetpub\\wwwroot\\images\\Receipt.png");  

            msg.Attachments.Add(inlineLogo);

            inlineLogo.ContentId = contentID;

            //To make the image display as inline and not as attachment

            inlineLogo.ContentDisposition.Inline = true;
            inlineLogo.ContentDisposition.DispositionType = DispositionTypeNames.Inline;

            //Add your email address to the recipients
            msg.To.Add(yourEmail);

            MailAddress fromEmail = new MailAddress("John.doe@domain.com", "Jhon Doe");
            msg.From = fromEmail;
            //Append their name in the beginning of the subject
            msg.Subject = "Marine Form Completion Notification";

            //To embed image in email
            msg.Body = (Convert.ToString("<htm><body> <img src=\"cid:") + contentID) + "\"> </body></html><br>" + body;
            msg.Attachments.Add(new Attachment(new MemoryStream(bytes), accountnumber.Text + DateTime.Now.Ticks + ".pdf"));
            msg.IsBodyHtml = true;
            //Configure an SmtpClient to send the mail.
            SmtpClient client = new SmtpClient("xxx.xx.xxx.xxxx");   
            client.EnableSsl = false;

            ////Display completed disclosure to employee
            FillSummary();
            //Send the msg
            client.Send(msg);
        }
    }
You are viewing reply posted by: dharmendr one year ago.