[Solved] Converting HTML to PDF generated blank PDF using iTextSharp in ASP.Net

Last Reply 11 months ago By pandeyism

Posted 11 months ago

having the below code blank pdf gets generated without data

        public void htmlstr()
        {
            string filename = "A"; 
           sb.Append(" <html><body><table width='100%' cellspacing='0' cellpadding='2'  >");
            sb.Append("<tr><td colspan='4'></td></tr>");
            sb.Append("<tr><td></td><th><img id='alogo'  height='75' width=75' src=" + Server.MapPath("~/images/ logo.png") + "></th><td><img id='xlogo'  height='75' width=75' src=" + Server.MapPath("~/images/ logo.png") + "></td><td></td></tr>");
          
            sb.Append("<tr><td colspan='4'>Date :"  + DateTime.Now.ToString("dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture) +"</td></tr>");
            sb.Append("<tr><td colspan='4'></td></tr>");
            sb.Append("<tr><td colspan='4'>Hello</td></tr>");
            sb.Append("<tr><td colspan='4'></td></tr>");
            sb.Append("<tr><td colspan='4'></td></tr>");
            sb.Append("<tr><td></td><td colspan='2'>test matter</td><td></td></tr>");
            sb.Append("<tr><td></td><td colspan='2'>test matter</td><td></td></tr>");
            sb.Append("<tr><td></td><td colspan='2'>test matter</td><td></td></tr>");
           
            sb.Append("<tr><td colspan='4'></td></tr>");
            sb.Append("<tr><td></td><td colspan='2'> test</td><td></td></tr>");
            sb.Append("<tr><td></td><td colspan='2'>test</td><td></td></tr>");
            sb.Append("<tr><td></td><td colspan='2'>test</td><td></td></tr>");
            sb.Append("<tr><td colspan='4'></td></tr>");
            sb.Append("<tr><td colspan='4'></td></tr>");         

            sb.Append("<tr><td></td><td colspan='2'>Test footer</td><td></td></tr>");
            sb.Append(" </table></body></html>");

            divpdf.InnerHtml = sb.ToString();
            System.IO.StreamWriter s = new System.IO.StreamWriter( filename, false);
            s.WriteLine(divpdf.InnerHtml);
            s.Close();        
        }
        public void convert2pdf()
        {
            
            string filename =  "A";
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=" + filename + ".pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            divpdf.RenderControl(hw);
            StringReader sr = new StringReader(sw.ToString());
            Document pdfDoc = new Document(PageSize.A4.Rotate(), 10f, 10f, 100f, 0f);
            HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
            PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
            pdfDoc.Open();
            htmlparser.Parse(sr);
            pdfDoc.Close();
            Response.Write(pdfDoc);
            Response.End(); 
        }
Posted 11 months ago Modified on 11 months ago

Hi svibuk,

Please take reference to the below code and correct your code.

HTML

<div id="divpdf" runat="server">
</div>
<asp:Button ID="btnHTML" Text="Create HTML" runat="server" OnClick="btnHTML_Click" />

Namespaces

C#

using System.Text;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;

Code

C#

public void htmlstr()
{
    StringBuilder sb = new StringBuilder();
    string filename = "A";
    sb.Append(" <html><body><table width='100%' cellspacing='0' cellpadding='2'  >");
    sb.Append("<tr><td colspan='4'></td></tr>");
    sb.Append("<tr><td></td><th><img id='alogo'  height='75' width=75' src=" + Server.MapPath("~/images/ logo.png") + "></th><td><img id='xlogo'  height='75' width=75' src=" + Server.MapPath("~/images/ logo.png") + "></td><td></td></tr>");
    sb.Append("<tr><td colspan='4'>Date :" + DateTime.Now.ToString("dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture) + "</td></tr>");
    sb.Append("<tr><td colspan='4'></td></tr>");
    sb.Append("<tr><td colspan='4'>Hello</td></tr>");
    sb.Append("<tr><td colspan='4'></td></tr>");
    sb.Append("<tr><td colspan='4'></td></tr>");
    sb.Append("<tr><td></td><td colspan='2'>test matter</td><td></td></tr>");
    sb.Append("<tr><td></td><td colspan='2'>test matter</td><td></td></tr>");
    sb.Append("<tr><td></td><td colspan='2'>test matter</td><td></td></tr>");

    sb.Append("<tr><td colspan='4'></td></tr>");
    sb.Append("<tr><td></td><td colspan='2'> test</td><td></td></tr>");
    sb.Append("<tr><td></td><td colspan='2'>test</td><td></td></tr>");
    sb.Append("<tr><td></td><td colspan='2'>test</td><td></td></tr>");
    sb.Append("<tr><td colspan='4'></td></tr>");
    sb.Append("<tr><td colspan='4'></td></tr>");
    sb.Append("<tr><td></td><td colspan='2'>Test footer</td><td></td></tr>");
    sb.Append(" </table></body></html>");
    divpdf.InnerHtml = sb.ToString();
}
protected void btnHTML_Click(object sender, EventArgs e)
{
    htmlstr();
    string filename = "A";
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=" + filename + ".pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    divpdf.RenderControl(hw);
    StringReader sr = new StringReader(sw.ToString());
    Document pdfDoc = new Document(PageSize.A4.Rotate(), 10f, 10f, 100f, 0f);
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    pdfDoc.Open();
    htmlparser.Parse(sr);
    pdfDoc.Close();
    Response.Write(pdfDoc);
    Response.End();
}