我試圖做的方法來獲取PDF字節:iTextSharp的System.NullReferenceException
public static Byte[] HtmlToBytes(string htmlText)
{
Byte[] bytes;
using (var ms = new MemoryStream())
{
using (var doc = new Document(PageSize.A4, 10, 10, 10, 10))
{
using (var writer = PdfWriter.GetInstance(doc, ms))
{
writer.CloseStream = false;
doc.Open();
using (var msHtml = new MemoryStream(Encoding.UTF8.GetBytes(htmlText)))
{
XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, msHtml, Encoding.UTF8);
}
}
}
bytes = ms.ToArray();
}
return bytes;
}
但它在這部分
XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, msHtml, Encoding.UTF8);
給出了一個NullReferenceException在我路過那裏的字符串只是像表格和標籤一樣的純html。
Here`s的堆棧跟蹤:
[NullReferenceException: Object reference not set to an instance of an object.]
iTextSharp.tool.xml.pipeline.html.HtmlPipeline.Close(IWorkerContext context, Tag t, ProcessObject po) +76
iTextSharp.tool.xml.XMLWorker.EndElement(String tag, String ns) +186
iTextSharp.tool.xml.parser.XMLParser.EndElement() +111
iTextSharp.tool.xml.parser.state.ClosingTagState.Process(Char character) +61
iTextSharp.tool.xml.parser.XMLParser.ParseWithReader(TextReader reader) +247
iTextSharp.tool.xml.parser.XMLParser.Parse(TextReader reader) +5
iTextSharp.tool.xml.XMLWorkerHelper.ParseXHtml(PdfWriter writer, Document doc, TextReader inp) +453
TCC.Globals.HtmlToBytes(String htmlText) in C:\Users\Felipe\Source\Workspaces\Workspace\SgLeitos\TCC\TCC\Helpers\Globals.cs:118
TCC.Controllers.RelatoriosController.Leitos(Nullable`1 id) in C:\Users\Felipe\Source\Workspaces\Workspace\SgLeitos\TCC\TCC\Controllers\RelatoriosController.cs:34
lambda_method(Closure , ControllerBase , Object[]) +107
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +14
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +157 ...
如果你打的XMLWorkerHelper上的斷點,msHtml變量事實上已被實例化了嗎? – gardarvalur
是的,它不是空的, –