Saving Panel to PDF not working nor getting errors

人盡茶涼 提交于 2019-11-28 14:39:25

You've got a couple of problems. If you're getting a The document has no pages message that means no content was added to the PDF which almost definitely means that there's a problem with the RenderControl method and nothing to do with iTextSharp. Another problem is that you are writing to the Response.OutputStream as well as outputting the literal document by using Response.Write(pdfDoc). You only need to do the former, I'm not actually sure what would happen with the latter.

The The document has no pages message also could mean that iTextSharp couldn't find anything to create content from. So you might have valid HTML but iTextSharp just doesn't know what to do with it. This probably isn't the case but I just had to mention it.

I'm going to break your code up into steps to help debug things. The first two blocks convert the control to HTML and then sanity checks it to make sure we've got something. You should inspect the contents of buf if you have any problems.

The third block creates a PDF from a MemoryStream and then dumps it into a byte array instead of writing directly to the Response.OutputStream. Although there's nothing incorrect with writing to the Response.OutputStream you will often find that you're swallowing errors and in general debugging is much harder. Once this block is done you should inspect bytes to make sure it has something.

The last block does the final push to the browser.

//Convert our control to HTML
string buf;
using (var sw = new StringWriter()) {
    using (var hw = new HtmlTextWriter(sw)) {
        Panel1.RenderControl(hw);
    }
    buf = sw.ToString();
}

//Sanity check
if (String.IsNullOrWhiteSpace(buf)) {
    throw new ApplicationException("No content found");
}

//Create our PDF and get a byte array
byte[] bytes;
using (var ms = new MemoryStream()) {
    using (var pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f)) {
        using (var writer = PdfWriter.GetInstance(pdfDoc, ms)) {
            pdfDoc.Open();
            using (var htmlparser = new HTMLWorker(pdfDoc)) {
                using (var sr = new StringReader(buf)) {
                    htmlparser.Parse(sr);
                }
            }
            pdfDoc.Close();
        }
    }
    bytes = ms.ToArray();
}


//Output the PDF
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=Quote.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(bytes);
Response.End();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!