HTMLWorker.Parse(StringReader) does not populate document

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

HTMLWorker.Parse(StringReader) does not populate document

joeller
This post has NOT been accepted by the mailing list yet.
I have an html report.  I want to render it as a PDF.  I have found code in a number of places including the public void generatePdf()  on http://portal-management.blogspot.com/2009/04/generating-pdf-from-net.html and the btnExport_Click(object sender, EventArgs e) method on http://www.codedigest.com/Articles/ASPNET/344_Export_to_PDF_file_in_ASPNet-Gridview_to_PDF_ASPX_Page_Content_to_PDF.aspx

All of these list the essential code for converting the HTML content of a page to a PDF fairly straight-forwardly.

However in each case either when I get to the document.close() method call, I get an error reporting "The Document has no pages."  or I open a PDF which only shows the borders of my tables with none of the cell contents.  I have searched all over the web and have only found slight modification of the basic technique.

Can someone take a look at this code to show where I am going wrong.  (POI I confirm with each step through that the Stringreader reader is properly populated with the html of the control I am rendering.)

       protected void btnPDFGen_Click(object sender, EventArgs e)
        {

            Response.Clear();
            Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
            Response.Charset = "";
            Response.ContentType = "application/pdf";
           
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);

            this.Page.FindControl("reportDiv").RenderControl(htw);
            StringReader reader = new StringReader(sw.ToString());
            String Test = reader.ReadToEnd();


            //byte[] array = Encoding.ASCII.GetBytes(Test);
            Document doc = new Document(PageSize.LETTER);

            //MemoryStream output = new MemoryStream();

            HTMLWorker worker = new HTMLWorker(doc);

            //PdfWriter writer = PdfWriter.GetInstance(doc, Response.OutputStream);
            PdfWriter.GetInstance(doc, Response.OutputStream);
            //writer.CloseStream = false;


            doc.Open();
            //doc.Add(new Paragraph(Test));
            //worker.StartDocument();

            // step 5: parse the html into the document
            worker.Parse(reader);

            //// step 6: close the document and the worker
            //worker.EndDocument();
            //worker.Close();
            doc.Close();
            //Response.OutputStream.Write(output.GetBuffer(), 0, output.GetBuffer().Length);
            //Response.OutputStream.Flush();
            //Response.OutputStream.Close();
            Response.Write(doc);
            Response.End();
            //output.Close();
        }
Reply | Threaded
Open this post in threaded view
|

Re: HTMLWorker.Parse(StringReader) does not populate document

joeller
This post has NOT been accepted by the mailing list yet.
The code above always returns document has no pages error.  

This nearly identical code from a nearly identical page always opens a PDF with only the table borders showing.

        Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);

            //this.Page.RenderControl(htw);
            this.Page.FindControl("reportdiv").RenderControl(htw);
            StringReader reader = new StringReader(sw.ToString());

            Document doc = new Document(PageSize.LETTER);
            //MemoryStream output = new MemoryStream();

            HTMLWorker worker = new HTMLWorker(doc);

            PdfWriter writer = PdfWriter.GetInstance(doc, Response.OutputStream);


            doc.Open();
            worker.StartDocument();

            // step 5: parse the html into the document
            worker.Parse(reader);

            // step 6: close the document and the worker
            worker.EndDocument();
            worker.Close();
            doc.Close();

            Response.Write(doc);
            Response.End();
      }