

The PdfResult class is the heart of this approach. This is what the process looks like in a nutshell:
#Use memory stream with pdfwriter pdf#
cshtml view, rendered and interpreted as any Razor view would be, passed to a PDF generation library (I use the excellent Aspose.Pdf, but this approach can also work with iTextSharp or any other PDF generation library that takes markup such as XML), and then returned to the client as PDF content. In a nutshell, the commands necessary to create a given PDF file (typically as XML) are placed in a standard. It’s also based on general concepts used in two open source projects, MvcRazorToPdf and RazorPDF. The code here is based primarily on the code in MVC itself, specifically the ActionResult and ViewResult classes. This allows you to make use of view models, HTML helpers, etc. It turns out there is a pretty simple way to enable the generation of PDF files in an ASP.NET MVC application using the same Razor view engine that you’re probably already using. While these kinds of tools work well enough for generating reports straight from the database, it’s been surprising how few resources exist to aid in generating PDF files from arbitrary data. In the past, I’ve used a variety of tools to achieve this such as SQL Server Reporting Services or Telerik Reporting. From reports to scan sheets, the need to generate PDF files has been present in every line-of-business application I’ve ever worked on.
