User Id :    Password :      New Member   Forgot Password  
 
Creating PDF with ASP.Net MVC and RazorPDF
Description 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.   No. of Views     2002
  Rating     0
Author Jalpesh Vadgama   Posted On     08 Aug 2014
Tags ASP.NET,ASP.NET-MVC,ASP.NET : How To,ASP.NET MVC 4    

Sample Code   Download Code

About Razor PDF:

 

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

 

Example(Creating PDF with ASP.Net MVC):

 

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<Customer> customers= new List<Customer>();
 
            for (int i = 1; i <= 10; i++)
            {
                Customer customer = new Customer
                {
                    CustomerID = i,
                    FirstName = string.Format("FirstName{0}", i.ToString()),
                    LastName = string.Format("LastName{0}", i.ToString())
                };
                customers.Add(customer);
            }
            return View(customers);
        }
 
    }
}

 

Now lt’s time to create view for listing of customers like following.

 

 

Once you click add it will create a view and now let’s run that application. It will look like following.

 

 

So everything looks good now. Now It’s time to create PDF document for same list. Let’s create a new action result method called PDF in same controller.

 

public ActionResult PDF()
{
    List<Customer> customers = new List<Customer>();
 
    for (int i = 1; i <= 10; i++)
    {
        Customer customer = new Customer
        {
            CustomerID = i,
            FirstName = string.Format("FirstName{0}", i.ToString()),
            LastName = string.Format("LastName{0}", i.ToString())
        };
        customers.Add(customer);
    }
 
    return new RazorPDF.PdfResult(customers, "PDF");
}

 

Here in the above code I have created a list and send it to a PDF Result which will result in PDF Document.Now let’s create a Razor view for that action result like following.

 

@model List<PDFDemo.Models.Customer>
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>
    <h2>Html List in PDF</h2>
    <table width="100%">
        <tr>
            <td>First Name</td>
           
            <td>Last Name</td>
        </tr>
        @foreach (var item in Model)
        {
            <tr>
                <td>@item.FirstName</td>
               
                <td>@item.LastName</td>
            </tr>
        }
    </table>
</body>
</html>

 

Here you can see I have printed a simple table with first name and last name. I have made layout=null as I don’t want any HTML. Now let’s run this application. And my list is converted in PDF as expected.

 

 

That’s it. It’s very easy to create PDF with ASP.Net with Razor PDF. There are more complex examples created by Al Nyveldt at following link.

 

https://github.com/RazorAnt/RazorPDFSample

About Author

About Author I am jalpesh vadgama an Microsoft MVP for Visual C# and BrainBench Certified ASP.NET Developer having more then 5 years of experience in .NET Technology.Please feel free to contact me for any queries via posting comments on my blog I will try to reply as early as possible. Please also visit my blog at http://jalpesh.blogspot.com/ Jalpesh Vadgama
No Photo
 
Country India
Company N/A
Home Page http://jalpesh.blogspot.com/

Rate this article

Rating options from poor, fair, good, very good to excelent.  
 

Comments

   
Write your comment here.
Comment
Verification Code