iTextSharp is not showing HTML table in PDF

心不动则不痛 提交于 2019-12-02 10:59:34

问题


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 ...


回答1:


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();


来源:https://stackoverflow.com/questions/23242103/itextsharp-is-not-showing-html-table-in-pdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!