iTextSharp的创建与空白页PDF [英] iTextSharp creates PDF with blank pages
问题描述
我刚刚加入iTextSharp的XMLWorker的NuGet包(及其依赖)到我的项目,我试图将HTML从字符串转换成PDF文件,即使没有异常被抛出,PDF文件使用两个空白页生成的。为什么呢?
I've just added the iTextSharp XMLWorker nuget package (and its dependencies) to my project and I'm trying to convert the HTML from a string into a PDF file, even though no exceptions are being thrown, the PDF file is being generated with two blank pages. Why?
代码的以前版本仅使用iTextSharp的5.5.8.0与HTMLWorker和ParseList方法,然后我切换到
The previous version of the code was using just iTextSharp 5.5.8.0 with HTMLWorker and ParseList method, then I switched to
下面是我使用的代码:
public void ExportToPdf() {
string htmlString = "";
Document document = new Document(PageSize.A4, 40, 40, 40, 40);
var memoryStream = new MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
document.Open();
htmlString = sbBodyMail.ToString();
XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, new StringReader(htmlString));
document.Close();
DownloadFile(memoryStream);
}
public void DownloadFile(MemoryStream memoryStream) {
//Clears all content output from Buffer Stream
Response.ClearContent();
//Clears all headers from Buffer Stream
Response.ClearHeaders();
//Adds an HTTP header to the output stream
Response.AddHeader("Content-Disposition", "attachment;filename=Report_Diagnosis.pdf");
//Gets or Sets the HTTP MIME type of the output stream
Response.ContentType = "application/pdf";
//Writes the content of the specified file directory to an HTTP response output stream as a file block
Response.BinaryWrite(memoryStream.ToArray());
//Response.Write(doc);
//sends all currently buffered output to the client
Response.Flush();
//Clears all content output from Buffer Stream
Response.Clear();
}
如果我把 document.Add(新段落( 只是一个测试));
权利之前 document.Close();
段落呈现在第二页,但对其余文档仍是空白。
If I place document.Add(new Paragraph("Just a test"));
right before document.Close();
the paragraph is rendered in the second page, but the rest of the document still is blank.
我已经改变了HTML中的 htmlString
变量只是一个 DIV
和 TABLE
和它的工作。所以,现在的问题就变成了:我怎么知道是什么的HTML部分在XMLWorker造成一些误差
I've changed the HTML in the htmlString
variable to just a DIV
and a TABLE
and it worked. So, now the question becomes: how do I know what part of the HTML is causing some error in the XMLWorker?
推荐答案
我'已经想通了,XMLWorkerHelper正在和 DIV
width属性(甚至设置样式属性),不幸的是它不抛出任何异常,以帮助您在这个麻烦了。
I've figured out that XMLWorkerHelper was having trouble with DIV
width attribute (even set on style attribute) and unfortunately it doesn't throw any exception to help you on this.
我发现从iTextSharp的开发者,指出中心表还不支持这个答案,所以我假定这是不支持了。
I found this answer from iTextSharp's developer that says that centering a table isn't supported yet, so I'm assuming this is not supported too.
这篇关于iTextSharp的创建与空白页PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!