我刚刚将 iTextSharp XMLWorker nuget 包(及其依赖项)添加到我的项目中,并且我正在尝试将 HTML 从字符串转换为 PDF 文件,即使没有抛出异常,PDF 文件也正在生成两个空白页。为什么?
以前版本的代码仅使用带有 HTMLWorker 和 ParseList 方法的 iTextSharp 5.5.8.0,然后我切换到
这是我正在使用的代码:
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(new Paragraph("Just a test"));
之前放置,但文档的其余部分仍然是空白的。document.Close();
更新
我已将htmlString
变量中的 HTML 更改为 aDIV
和 aTABLE
并且它起作用了。所以,现在问题变成了:我怎么知道 HTML 的哪一部分导致了 XMLWorker 中的一些错误?