A long time favorite PDF reporting tool of mine is ActiveReports for .NET. It's relatively painless to integrate into your project, has a great designer tool, and has plenty of power for more advanced reporting needs. When I started becoming an ASP.NET MVC zealot, I was afraid of losing the ability to use this great tool. With a little tinkering, I got it to play nicely within an MVC project and have included the steps below for future reference:
- The first step is to add ActiveReports to your project as you normally would. This includes updating your web.config following the installation guidelines such as adding the ActiveReports handlers to the HttpHanders section and adding the appropriate assembly references.
- The next step is getting the MVC framework to ignore calls to any ActiveReport reports. To do this, we need to ignore a couple of routes in your routing configuration as follows:
routes.IgnoreRoute ( "{*allActiveReportRpxHandler}",
new { allActiveReportRpxHandler = @".*\.rpx(/.*)?" } );
routes.IgnoreRoute ( "{*allActiveReport}",
new { allActiveReport = @".*\.ActiveReport(/.*)?" } );
routes.IgnoreRoute ( "{*allArCacheItem}",
new { allArCacheItem = @".*\.ArCacheItem(/.*)?" } );
- Add your ActiveReports as you normally would to your project.
- Finally, and this is the important bit, you cannot use the ActiveReports web viewer in the MVC environment because it assumes use of the ASP.NET page lifecycle. Accordingly, I haven't found a way to render the generated PDF directly to the web page without first saving it somewhere on the file system. So I've taken to streaming the PDF to the client when they request a report. Here's an example action method within a controller which does this using MvcContrib's BinaryResult (you could also use MVC's native BinaryResult):
public ActionResult GenerateMyActiveReport() {
MyActiveReport3Class myReport =
new MyActiveReport3Class(someViewModelOrServiceDependency);
myReport.Run();
MemoryStream reportStream = new MemoryStream();
new DataDynamics.ActiveReports.Export.Pdf.PdfExport()
.Export(myReport.Document, reportStream);
return new BinaryResult(reportStream.GetBuffer(),
"application/pdf", true, "SomeCoolReport.pdf");
}
And that's it. A couple of little tweaks to the normal procedures and we have ActiveReports support within our ASP.NET MVC project.
Billy McCafferty
Posted
05-11-2009 1:33 PM
by
Billy McCafferty