I'm having an unusual issue using GemBox.Document to generate a PDF version of search results in my application. It appears to happen when I have a larger number of records returned (though the number is still pretty small). The odd part is the code works if I generate a report for subset A of all records, and for subset B of all records, but not A and B together. In all three cases the DocumentModel seems to generate properly; the error comes on the save.
Here are the relevant methods:
public MemoryStream GetPdfReport(SearchRequestData searchRequestData, HttpRequest httpRequest)
{
_stream = new MemoryStream();
_httpRequest = httpRequest;
DocumentModel dm = CreateDocxOrPdfReport(searchRequestData);
dm.Save(_stream, GemBox.Document.SaveOptions.PdfDefault);
//CreateDocxOrPdfReport(searchRequestData).Save(_stream, GemBox.Document.SaveOptions.PdfDefault);
return _stream;
}
private DocumentModel CreateDocxOrPdfReport(SearchRequestData searchRequestData)
{
SetFieldsToDisplay(searchRequestData);
searchRequestData.PageIndex = 0;
var reportData = TrackService.GetTrackReportData(searchRequestData);
var doc = new DocumentModel();
var section = new Section(doc);
doc.Sections.Add(section);
AddDocxOrPdfHeaderAndFooter(doc, "Search Report");
AddDocxOrPdfFirstPageSummary(doc, "Search Report", reportData.Count, searchRequestData, ReportType.Track);
AddDocxOrPdfReportData(doc, doc.Sections[0], reportData);
return doc;
}
And here is the error:
System.NullReferenceException was unhandled by user code
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=GemBox.Document
StackTrace:
at eh.k()
at eh.a(f6 A_0, ko A_1)
at b9.a(f6& A_0, Int32 A_1, gl A_2, v3 A_3, Int32& A_4, Boolean& A_5, ArrayList& A_6)
at b9.a(gl A_0, f6& A_1, Int32& A_2)
at b9.a(l4 A_0, gl A_1)
at ug.a(tz A_0, e6 A_1, gl A_2)
at pc.a(er A_0, er A_1, Double A_2, t1 A_3, Boolean A_4, ug A_5)
at pc.a(t1 A_0, t1 A_1, t1 A_2, Boolean A_3, ug A_4, d2 A_5, d2 A_6)
at pc.a(ir A_0, Double A_1, Boolean A_2, ug A_3)
at pc.a(x9 A_0, f6 A_1, ir A_2, ug A_3)
at pc.a(f6 A_0, ko A_1)
at b9.a(f6& A_0, Int32 A_1, gl A_2, v3 A_3, Int32& A_4, Boolean& A_5, ArrayList& A_6)
at b9.a(gl A_0, f6& A_1, Int32& A_2)
at b9.a(l4 A_0, gl A_1)
at b9.a(l4 A_0)
at qh.a(l4 A_0)
at a3.e()
at ai.a(Boolean A_0)
at ai.g()
at ai.c()
at vt.a()
at ow.a(Stream A_0, String A_1, String A_2, db A_3)
at vt.a(vw A_0, Stream A_1, String A_2, String A_3, db A_4)
at GemBox.Document.PdfSaveOptions.Save(DocumentModel document, Stream stream, String path)
at GemBox.Document.DocumentModel.Save(Stream stream, SaveOptions options)
at StateScape.API.Services.LocalTrackExportService.GetPdfReport(SearchRequestData searchRequestData, HttpRequest httpRequest) in c:\Users\Andrew\Documents\Projects\StateScape\git\statescapeweb\StateScape.API\Services\LocalTrackExportService.cs:line 27
at StateScape.API.Controllers.ExportReportController.GetLocalTrackPdf() in c:\Users\Andrew\Documents\Projects\StateScape\git\statescapeweb\StateScape.API\Controllers\ExportReportController.cs:line 244
at lambda_method(Closure , ControllerBase , Object[] )
at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41()
at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49()
InnerException:
The solution turned out to be checking for non-printable characters using a regular expression. I'm still not sure why those characters only appeared when combining result sets, but that's what worked for me.