Xceedas

Xceedas
xceedas

Thursday 29 January 2015

Creating PDF with ASP.Net MVC and RazorPDF

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.


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.

ASPNETPDFDemoApplication

Once you click on OK. It will ask for type of project. We are going to create ASP.Net MVC internet application.

MVCTypeApplicationRazorPDF

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.

NuGetPackageforASPNETMVCPDF

Like following.

NugetPacketManagerConsoleForCreatingPDFinaspnetmvc

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.

CreatingControllerToCreatePDFinaspnetmvc

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.

CreatingaViewforPDfListing

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

image

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.

image

That’s it. It’s very easy to create PDF with ASP.Net with Razor PDF.

No comments :