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();
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…