Like many developers, I’ve been working with the Razor View Engine for a while. Prior to MVC 3 and Razor, I was using Spark as my default view engine and I was pretty happy with it. Being a long time Spark user, I sometimes find myself wishing I was using Spark for certain things. The Spark feature I’ve easily missed the most however was the slick way you could uses Spark to mark up iText XML and in turn generate PDFs.
I finally got around to spending a few hours to create this functionality using a Razor view (borrowing heavily from the Spark project). It is very easy to use and makes creating PDFs in your ASP.NET MVC projects a breeze.
- Simply add the RazorPDF nuget package to your project.
- Have your Controller action return a RazorPDF.PdfResult with your model and view name assigned.
- Create the view with itext xml to create the PDF output you want.
Check out this short video showing you RazorPDF in action (2:18) and take a look at the RazorPDFSample project on GitHub to get you going. The sample project has some basic iText XML samples, but there are plenty more on the web.