Update: I have written a new blog post about better approach to create a PDF with asp.net mvc- You can find that following location.- A Better Solution to create PDF with Rotativa and ASP.NET MVC
In this post we are going to learn how we can easily create PDF from ASP.Net application with the help of Razor PDF NuGet package. This NuGet package is created by Al Nyveldt It internally uses ITextSharp an open source PDF convertor library. RazorPDF uses Razor View engine to create iTextXML which is in tern used to produce PDF file. You can get more information about that at below link.
https://www.nuget.org/packages/RazorPDF
So what we are we waiting for ? Let’s create a simple example. To create example first thing we need to a create and ASP.Net MVC application.
Once you click on OK. It will ask for type of project. We are going to create ASP.Net MVC internet application.
Once you click on it will create an application. The next thing you need to install a NuGet package. You need to type following command on your NuGet Package manager console.
Like following.
Now our application is ready to create PDF files. Now to create an example Let’s create a model class ‘Customer’ to create a listing of customers in the application.
namespace PDFDemor.Models { public class Customer { public int CustomerID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } } }
Now Custom class is ready. So let’s create an CustomerController with listing of customer ActionResult like following.
Now once you click Add It will create CustomerController. In index ActionResult I have created following code. Where I have created an list and pass it to view.
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using PDFDemo.Models; namespace PDFDemo.Controllers { public class CustomerController : Controller { // // GET: /Customer/ public ActionResult Index() { List customers= new List(); for (int i = 1; i