我有一个帮助程序类,可从生成的html片段创建pdf文档。当html很长但我得到
时Object reference not set to an instance of an object.
这是帮助程序类:
public static void CreatePdf(ContractPdf model, string htmlPath, string cssPath, string savePath)
{
using (var document = new Document())
{
// Create the pdf
var pdfWriter = PdfWriter.GetInstance(document, new FileStream(savePath, FileMode.OpenOrCreate));
//pdfWriter.Open();
document.Open();
// CSS
var cssResolver = new StyleAttrCSSResolver();
var cssStream = new FileStream(cssPath, FileMode.Open);
var cssFile = XMLWorkerHelper.GetCSS(cssStream);
cssResolver.AddCss(cssFile);
// HTMLStringBuilder
var cssApplier = new CssAppliersImpl();
var htmlPipelineContext = new HtmlPipelineContext(cssApplier);
htmlPipelineContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
// PIPELINES
var pdfWriterPipeline = new PdfWriterPipeline(document, pdfWriter);
var htmlPipeline = new HtmlPipeline(htmlPipelineContext, pdfWriterPipeline);
var cssResolverPipeline = new CssResolverPipeline(cssResolver, htmlPipeline);
var xmlWorker = new XMLWorker(cssResolverPipeline, true);
var xmlParser = new XMLParser(xmlWorker);
var html = File.ReadAllText(htmlPath);
var st = new StringTemplate(html);
st.SetAttribute("Dto", model);
html = st.ToString();
var textReader = new StringReader(html);
xmlParser.Parse(textReader);
}
这是引发错误的最后一行。很抱歉含糊,但这是我继承的代码。总体来说还可以,但是当有很多html时,我会得到上面的错误。
这是堆栈跟踪:
at iTextSharp.text.pdf.ColumnText.SetSimpleVars(ColumnText org)
at iTextSharp.text.pdf.ColumnText.SetACopy(ColumnText org)
at iTextSharp.text.pdf.PdfPCell.GetMaxHeight()
at iTextSharp.text.pdf.PdfPRow.CalculateHeights()
at iTextSharp.text.pdf.PdfPRow.get_MaxHeights()
at iTextSharp.text.pdf.PdfPTable.GetRowHeight(Int32 idx, Boolean firsttime)
at iTextSharp.text.pdf.ColumnText.GoComposite(Boolean simulate)
at iTextSharp.text.pdf.ColumnText.Go(Boolean simulate, IElement elementToGo)
at iTextSharp.text.pdf.PdfPRow.SplitRow(PdfPTable table, Int32 rowIndex, Single new_height)
at iTextSharp.text.pdf.ColumnText.GoComposite(Boolean simulate)
at iTextSharp.text.pdf.ColumnText.Go(Boolean simulate, IElement elementToGo)
at iTextSharp.text.pdf.PdfDocument.AddPTable(PdfPTable ptable)
at iTextSharp.text.pdf.PdfDocument.Add(IElement element)
at iTextSharp.text.Document.Add(IElement element)
at iTextSharp.tool.xml.pipeline.end.PdfWriterPipeline.Write(IWorkerContext context, ProcessObject po)
at iTextSharp.tool.xml.pipeline.end.PdfWriterPipeline.Close(IWorkerContext context, Tag t, ProcessObject po)
at iTextSharp.tool.xml.XMLWorker.EndElement(String tag, String ns)
at iTextSharp.tool.xml.parser.XMLParser.EndElement()
at iTextSharp.tool.xml.parser.state.ClosingTagState.Process(Char character)
at iTextSharp.tool.xml.parser.XMLParser.ParseWithReader(TextReader reader)
at Domain.Utils.PdfHelper.CreatePdf(ContractPdf model, String htmlPath, String cssPath, String savePath) in D:\\Projects\\Zest4\\Domain\\Utils\\PDFHelper.cs:line 87
at Zest4.Helpers.OrderHelper.Print(ContractPdf model, String templateDir, String pdfPath) in D:\\Projects\\Zest4\\Zest4\\Helpers\\OrderHelper.cs:line 120
at Zest4.Helpers.OrderHelper.Print(Controller controller, HttpServerUtilityBase server, Order order, IOrderRepository _orderRep, ICustomerRepository _customerRep, IMobileConnectionRepository _mobileRep, IContactRepository _contactRep, IAddressRepository _addressRep, IPartnerRepository _partnerRep, IUserRepository _userRep) in D:\\Projects\\Zest4\\Zest4\\Helpers\\OrderHelper.cs:line 101
at Zest4.Controllers.OrderController.Print(OrderPrintModel model) in D:\\Projects\\Zest4\\Zest4\\Controllers\\OrderController.cs:line 429"