简体   繁体   中英

iTextSharp is not showing HTML table in PDF

I don't understand why it is not working. Here is my code:

Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
string htmlstr = "<html><body><h1>My First Heading</h1><p>My first paragraph.</p><table border=1><tr><td>1st</td><td>2nd</td></tr><tr><td>3rd</td><td>4th</td></tr></table></body></html>";

Panel panel1 = new Panel();
panel1.Controls.Add(new LiteralControl(htmlstr));

panel1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();

My PDF is generated but the table is not showing ...

You've got some crazy code there (which appears to be from here ). You've got an HTML string but then your dumping it into an ASP.Net control which you're further dumping into another ASP.Net control. Then you're asking ASP.Net to render the control back to HTML. That's three or four lines you can kill off.

Also, you're writing to the raw HTTP response stream and then sending the PDF to the same stream. This is your bigger problem, actually. I strongly suggest never writing to the raw stream until you're absolutely done processing things. You've also changed the HTTP headers which can cause problems if there are ASP.Net errors.

The below code is a rework of what you've got. I switched it over to using statements to ensure that things get cleaned up. If you're using an older unsupported version of iTextSharp you'll want to switch those back.

string htmlstr = "<html><body><h1>My First Heading</h1><p>My first paragraph.</p><table border=1><tr><td>1st</td><td>2nd</td></tr><tr><td>3rd</td><td>4th</td></tr></table></body></html>";

//We'll store our final PDF in this
byte[] bytes;

//Read our HTML as a .Net stream
using (var sr = new StringReader(htmlstr)) {

    //Standard PDF setup using a MemoryStream, nothing special
    using (var ms = new MemoryStream()) {
        using (var pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f)) {

            //Bind a parser to our PDF document
            using (var htmlparser = new HTMLWorker(pdfDoc)) {

                //Bind the writer to our document and our final stream
                using (var w = PdfWriter.GetInstance(pdfDoc, ms)) {

                    pdfDoc.Open();

                    //Parse the HTML directly into the document
                    htmlparser.Parse(sr);

                    pdfDoc.Close();

                    //Grab the bytes from the stream before closing it
                    bytes = ms.ToArray();
                }
            }
        }
    }
}

//Assuming that the above worked we can now finally modify the HTTP response
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=BusinessUnit.pdf");
Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
//Send the bytes from the PDF
Response.BinaryWrite(bytes);
Response.End();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM