# Render aspx page as html

• ### 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.Save("c:\\text.pdf");
mydoc.Clear();


Wednesday, July 8, 2009 10:12 AM

• User-1938105002 posted

I got it sorted. I put the code in the wrong method. 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.Save(Server.MapPath("text.pdf"));
mydoc.Clear();

}

• Marked as answer by 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. 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();
}