Friday, October 2, 2015

A Better Solution to create PDF with Rotativa and ASP.NET MVC

In most of the line of business application we need some kind of reports. Now days we need those reports as PDF format as it is the most convenient way of storing documents. I have written a post about creating a PDF in ASP.NET MVC with Razor PDF and it is one of the top visited page on my blog.

Creating PDF with ASP.NET MVC and RazorPDF

But now I have found a better way to create a PDF with ASP.NET MVC so I thought it will be a good idea to write a blog post about it. So in this blog post we are going to learn about creating PDF with ASP.NET MVC with the use of Rotativa as open source framework for creating PDF with ASP.NET MVC.

What is Rotativa:

It is a framework created by Giorgio Bozio with the purpose of creating PDF in ASP.NET MVC extremely easy way. It is based on wkhtmltopdf tool to create PDF from html content rendered on browser. It uses the web kit engine which is used by Safari and Chrome browser to render html. And support most of HTML tags and styles. It is a open source command line tools so Giogio has created a nuget package to use this tool as PDF creator. You can find more information about Rotativa from the following links.

You can find source code of rotativa-

Now let's create example to demonstrate the power of Rotativa with ASP.NET MVC

Creating PDF with ASP.NET MVC and Rotativa:

So to create example, I have created a ASP.NET MVC application.


The first thing we need to do is to add Rotativa Nuget Package in our application.

Here is the model class I have used to demonstrate power or Rotativa.
namespace MvcPdf.Models
    public class Customer
        public int CustomerId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string ProfileImage { get; set; }
In this example, we are not going to use any database so we are going to hardcode data in customer. so We have done is hardcode list of customer in controller and returning it to a view.
using MvcPdf.Models;
using Rotativa;
using System.Collections.Generic;
using System.Web.Mvc;

namespace MvcPdf.Controllers
    public class CustomerController : Controller
        private List<Customer> _customers;

        public CustomerController()
            var imagePath = "profile.jpg";
            _customers = new List<Customer>()
                new Customer {CustomerId = 1, FirstName = "Jalpesh",
                               LastName = "Vadgama", ProfileImage = imagePath},
                new Customer {CustomerId = 1, FirstName = "Vishal", 
                               LastName = "Vadgama", ProfileImage = imagePath}

        // GET: Customer
        public ActionResult Index()
            return View(_customers);

And in view we has displayed this list as table like following.
@model List<MvcPdf.Models.Customer>

    ViewBag.Title = "PDF Title";

<h2>Convert to  PDF</h2>

<table class="table">
        <td class="alert-success">First Name</td>
        <td class="alert-info">Last name</td>
        <td style="background-color: yellow;">Profile Image</td>
    @foreach (var item in Model)

            <td><img src="@Url.Content("~/content/" + Path.GetFileName(item.ProfileImage))" alt="" width="100" height="100" /></td>

@Html.ActionLink("Print", "Print", "Customer")
If you see in above code, I have used a image to display profile pic. I have also used different styles for td element in table to display power of Rotativa. Even I have used hardcoded style directly.  At bottom I have created  Html Action link to print this view as PDF and following is a code the printing this view as PDF in Action Result.
public ActionResult Print()
    return new ActionAsPdf("Index", _customers);
And when we run this application it looks like following.


Now when you click on Print it will create a PDF like following.


This was pretty basic demo but there are lots of options also available with Rotativa you can find that options in documentation on Github as well. I have shared same link above.
You can find source code of this sample application on github at following location -
Hope you like it. Stay tuned for more!!


Post a Comment

Your feedback is very important to me. Please provide your feedback via putting comments.

Search This Blog
About Me
My Photo

Full Stack Developer,Blogger,Microsoft Mvp, Mentor, Life long learner, 10+ Years of Experience for JavaScript, web and Microsoft technologies. ASP.NET MVC,Web Forms,C#,AngularJs,Golang
Mvp profile
Subscribe to my blog

Enter your email address:

Follow us on facebook
Blog Archive
Total Pageviews