Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
Hi Experts,
In .aspx page I have asp.Panel inside the panel there's a repeater generated from DB.
On button click I convert the html to PDF Which work very well.
When I try to do the same on Page_Load it gives me error:
C#
The document has no pages.

Tried to fix this by adding:
C#
Document.Add(new Paragraph("Hello World!"));.

but it gives empty PDF file.
So I tried to run my converting function after page load:
C#
protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            base.Render(writer);
            Response.Flush();
            ExportPDF();
        }

It gives error:
C#
Server cannot set content type after HTTP headers have been sent

The converting function:
C#
private void ExportPDF()
        {
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=Invoice.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            pnlContents.RenderControl(hw); //the containing panel
            StringReader sr = new StringReader(sw.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.Write(pdfDoc);
            Response.End();
        }

So It looks like something wrong with the asp.Panel.
Please advise why it would work on button click but not on page load or Render. What I am missing?

Thanks for help
Samira
Posted

1 solution

Found the problem. Had to set Render event like the following code:
C#
protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            ExportPDF();
            //base.Render(writer);
            //Response.Flush();            
        }


Hope will help others.

Thanks,
Samira
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900