我正在尝试创建一个获取PDF字节的方法:
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;
}
但它在此部分发出了NullReferenceException
XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, msHtml, Encoding.UTF8);
在我传递的字符串上,只有简单的html,如表格和标签。
这是堆栈跟踪:
[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 ...
答案 0 :(得分:2)
我曾尝试过休耕:
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;
}
private static void Main(string[] args)
{
var str = @"<!DOCTYPE html><html lang=""en"" xmlns=""http://www.w3.org/1999/xhtml""><head><meta charset=""utf-8"" /><title></title></head><body><table border=""1"" style=""width:100%""><tr><td>Jill</td><td>Smith</td><td>50</td></tr><tr><td>Eve</td><td>Jackson</td><td>94</td></tr></table></body></html>";
var s = HtmlToBytes(str);
var str2 = @"<table border=""1"" style=""width:100%""><tr><td>Jill</td><td>Smith</td><td>50</td></tr><tr><td>Eve</td><td>Jackson</td><td>94</td></tr></table>";
s = HtmlToBytes(str2);
var str3 = @"<tabl=""width:100%""><tr><td>Jill</td><td>Smith</td><td>50</td></tr><tr><td>Eve</td><td>Jackson</td><td>94</td></tr></table>";
s = HtmlToBytes(str3); //NULL HERE with corrupted html
}
所以可能的回答是,您的HTML已损坏