![]() Recall that I said I’m using Aspose.Pdf, so the XML in my view corresponds to the XML that Aspose.Pdf expects. The final step is the view, where the actual PDF content is specified. ![]() It will be given an int array as it’s model and then rendered by the Razor view engine. The code about will cause a PdfResult class to be instantiated which will attempt to find a view named “PdfTest.cshtml” in the conventional location. The result of all this is that you can write you PDF generating actions in a very similar way to how you write your normal web actions: public virtual ActionResult PdfTest() ViewEngineCollection = ViewEngineCollection Based on View() code in Controller base class from MVC Protected ActionResult Pdf(string fileDownloadName, string viewName, object model) Return Pdf(fileDownloadName, null, model) ![]() ![]() Protected ActionResult Pdf(string fileDownloadName, object model) Return Pdf(fileDownloadName, viewName, null) Protected ActionResult Pdf(string fileDownloadName, string viewName) Return Pdf(fileDownloadName, null, null) Protected ActionResult Pdf(string fileDownloadName) To that end, the following are overloads for a Pdf() method that can be used as the return value for an action: protected ActionResult Pdf() And I’ve found over time that it’s nice having a base Controller class into which I can add all sorts of other helper methods and additional logic. Even if it’s just to hold the next bit of code, it’s worthwhile. Public string FileDownloadName If you don’t already have a custom base Controller in your MVC application, I suggest adding one. Setting a FileDownloadName downloads the PDF instead of viewing it Though it does all the heavy lifting, there actually isn’t that much code involved: public class PdfResult : PartialViewResult It contains all of the logic necessary to locate the view, set up the view context, render the view, and generate the PDF file. The PdfResult class is the heart of this approach. This is what the process looks like in a nutshell: 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.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |