locked
Render aspx page as html RRS feed

  • Question

  • User-1938105002 posted

    Hi guys,

    I'm trying to convert an aspx page to html and the create a pdf document using ABC Pdf libraries,  but for some reason my code, the StringWriter and HttpContext, enters a infinate loop. any ideas..? See code below


     

            StringWriter writer = new StringWriter();
            HttpContext.Current.Server.Execute("~/AsbDetPrint.aspx", writer);
            label.Text = writer.ToString();
    
            Doc mydoc = new Doc();
            mydoc.AddHtml(writer.ToString());
            mydoc.Save("c:\\text.pdf");
            mydoc.Clear();
    



    Wednesday, July 8, 2009 10:12 AM

Answers

  • User-1938105002 posted

    I got it sorted. I put the code in the wrong method. Embarassed The code needs to be in the Render() method of the page for it to catch the HTML then use the ABC Pdf libraries to create the pdf document. I'm sure it will work with iTextSharp libraries to. I have attached the code below.


        protected override void  Render(HtmlTextWriter writer)
    {
        StringBuilder sb = new StringBuilder();
        StringWriter sw = new StringWriter(sb);
    
        HtmlTextWriter hWriter = new HtmlTextWriter(sw);
     	base.Render(hWriter);
    
        string PageResult = sb.ToString();
        writer.Write(PageResult);
    
        Doc mydoc = new Doc();
        mydoc.AddImageHtml(sb.ToString());
        mydoc.Save(Server.MapPath("text.pdf"));
        mydoc.Clear();
         
    }


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 9, 2009 8:03 AM

All replies

  • User-584747036 posted

    Obvious question but is the sample code from AsbDetPrint.aspx?  I.e. is it calling itself over and over?

    Wednesday, July 8, 2009 12:19 PM
  • User-1938105002 posted

    I got it sorted. I put the code in the wrong method. Embarassed The code needs to be in the Render() method of the page for it to catch the HTML then use the ABC Pdf libraries to create the pdf document. I'm sure it will work with iTextSharp libraries to. I have attached the code below.


        protected override void  Render(HtmlTextWriter writer)
    {
        StringBuilder sb = new StringBuilder();
        StringWriter sw = new StringWriter(sb);
    
        HtmlTextWriter hWriter = new HtmlTextWriter(sw);
     	base.Render(hWriter);
    
        string PageResult = sb.ToString();
        writer.Write(PageResult);
    
        Doc mydoc = new Doc();
        mydoc.AddImageHtml(sb.ToString());
        mydoc.Save(Server.MapPath("text.pdf"));
        mydoc.Clear();
         
    }


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 9, 2009 8:03 AM