locked
convert generated html to pdf RRS feed

  • Question

  • User-1474096950 posted
      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();
                
    
            }
    

    with the above i am getting blank pdf

      protected void Page_Load(object sender, EventArgs e)
            {
                convert2pdf();
            }
    

    Thursday, July 5, 2018 10:17 AM

Answers

  • User-1171043462 posted

    In HTML Table it is called TFOOT

     sb.Append("<tfoot>");
            sb.Append("<tr><td></td><td colspan='3'>footer </td><td></td></tr>.");
            sb.Append("</tfoot>");  

    and since it is working. Kindly mark as Answer. and for more questions. Create a new thread.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 5, 2018 12:43 PM

All replies

  • User-492460945 posted

    Hi svibuk,

    It seems you're not calling htmlstr() in oage load. Your pageload event should be something like this.

      protected void Page_Load(object sender, EventArgs e)
            {
                htmlstr();
                convert2pdf();
            }

    Thanks,

    RajeshV.

    Thursday, July 5, 2018 11:04 AM
  • User-1171043462 posted

    Mistakes in your code.

    1. HTML incorrect. TH placed below TDs.
    2.  Missing End Tags.
    3. Image Tag Path wrong. Server.MapPath gets the File Path on Disk (eg. D:\images\logo.png) while PDF needs a URL eg. http://localhost:4503/images/logo.png

    Here is the working solution

        protected void ExportToPDF(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<table width='100%' cellspacing='0' cellpadding='2'>");
            sb.Append("<tr><td colspan='4'></td></tr>");
            sb.Append("<tr><td></td><td><img alt='alogo' height = '50' width = '50' src=" + GetUrl("images/logo.png") + " /></td><td><img alt='xlogo' height = '50' width = '50' src=" + GetUrl("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>");
            StringReader sr = new StringReader(sb.ToString());
            Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
            HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
            PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
            pdfDoc.Open();
            htmlparser.Parse(sr);
            pdfDoc.Close();
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=HTML.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Write(pdfDoc);
            Response.End();
        }
    
        private string GetUrl(string imagePath)
        {
            string[] splits = Request.Url.AbsoluteUri.Split('/');
            if (splits.Length >= 2)
            {
                string url = splits[0] + "//";
                for (int i = 2; i < splits.Length - 1; i++)
                {
                    url += splits[i];
                    url += "/";
                }
                return url + imagePath;
            }
            return imagePath;
        }
    

    Thursday, July 5, 2018 11:06 AM
  • User-1474096950 posted

    thanks a lot  its working now

     sb.Append("<footer>");
            sb.Append("<tr><td></td><td colspan='3'>footer </td><td></td></tr>.");
            sb.Append("</footer>");  

    using this for footer but not getting at the footer

    Thursday, July 5, 2018 11:52 AM
  • User-1171043462 posted

    In HTML Table it is called TFOOT

     sb.Append("<tfoot>");
            sb.Append("<tr><td></td><td colspan='3'>footer </td><td></td></tr>.");
            sb.Append("</tfoot>");  

    and since it is working. Kindly mark as Answer. and for more questions. Create a new thread.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 5, 2018 12:43 PM
  • User-1171043462 posted

    Please mark all correct replies as answer so that people know what replies are helpful

    Thursday, July 5, 2018 4:15 PM