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:
The document has no pages.
Tried to fix this by adding:
Document.Add(new Paragraph("Hello World!"));.
but it gives empty PDF file.
So I tried to run my converting function after page load:
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
base.Render(writer);
Response.Flush();
ExportPDF();
}
It gives error:
Server cannot set content type after HTTP headers have been sent
The converting function:
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);
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