iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml无法正常工作 [英] iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml not working
问题描述
我正在使用iTextSharp版本:5.5.6; iTextSharp XML Worker版本:5.5.6
I'm using iTextSharp version: 5.5.6; iTextSharp XML Worker version: 5.5.6
我从此处,但是运行代码后,PDF文件将永远不会打开
I got some code from Here, but after I run the code, the PDF file will never open
:文件已损坏,无法修复.本地\ EWHvxm9t5 ++
:The file is damaged and could not be repaired. Local\EWHvxm9t5++
htmltext ="\ r \ n \ r \ n \ r \ n \ r \ n \ r \ n
\ r \ n \ r \ n \ r \ n \ r \ n \ r \ n \ r \ n
\ r \ n项目\ r \ n说明\ r \ n
批号\ r \ n修订版\ r \ n名称PlatSN \ r \ n
DateCreated \ r \ n CreatedBy \ r \ n \ r \ n \ r \ n
\ r \ n \ r \ n 100-817412-001 \ r \ n X500-G02-
ENV DWG \ r \ n 15020008 \ r \ n B
\ r \ n testing123 \ r \ n 2015年4月9日12:00:00
AM \ r \ n ULTRATCS \ xma \ r \ n \ r \ n \ r \ n
\ r \ n \ r \ n \ r \ n \ r \ n \ r \ n \ r \ n"
htmltext="\r\n\r\n\r\n\r\n\r\n
\r\n \r\n \r\n \r\n \r\n \r\n
\r\n Item \r\n Description\r\n
LotNo\r\n Revision\r\n NamePlatSN\r\n
DateCreated\r\n CreatedBy\r\n \r\n \r\n
\r\n \r\n 100-817412-001\r\n X500-G02 -
ENV DWG \r\n 15020008\r\n B
\r\n testing123\r\n 4/9/2015 12:00:00
AM\r\n ULTRATCS\xma\r\n \r\n \r\n
\r\n \r\n \r\n\r\n\r\n\r\n"
HTML字符串(格式更好)如下:
The HTML string (better formatted) looks like this:
<!DOCTYPE html>
<html lang=\"en\" >
<body>
<table>
<tr>
<th> Item </th>
<th> Description</th>
<th> LotNo</th>
<th>Revision</th>
<th>NamePlatSN</th>
<th>DateCreated</th>
<th>CreatedBy</th>
</tr>
<tr>
<td> 100-817412-001</td>
<td> X500-G02 - ENV DWG </td>
<td>15020008</td>
<td> B </td>
<td>testing123</td>
<td> 4/9/2015 12:00:00 AM</td>
<td> ULTRATCS\\xma</td>
</tr>
</table>
</body>
</html>
这是代码:
protected ActionResult ViewPdf(object model)
{
// Create the iTextSharp document.
Document doc = new Document();
byte[] buf;
// Set the document to write to memory.
MemoryStream memStream = new MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(doc, memStream);
writer.CloseStream = false;
doc.Open();
string htmltext = this.RenderActionResultToString(this.View(model));
using (var srHtml = new StringReader(htmltext))
{
//Parse the HTML
XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, srHtml);
//buf = new byte[memStream.Position];
//memStream.Position = 0;
//memStream.Read(buf, 0, buf.Length);
buf = memStream.ToArray();
doc.Close();
}
// System.IO.File.WriteAllBytes(@"c:\\temp\test.pdf", buf);
// Send the binary data to the browser.
return new BinaryContentResult(buf, "application/pdf");
}
}
}
怎么了?
推荐答案
(通过评论发现)
您需要先调用doc.Close()
,然后再调用buf = memStream.ToArray();
.这样一来,iTextSharp便知道您已经完成了操作,应该刷新所有缓冲区并编写PDF预告片.
You need to call doc.Close()
before calling buf = memStream.ToArray();
. This let's iTextSharp know that you are actually done and it should flush any buffers and write the PDF trailer.
这篇关于iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!