Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

ASP.NET MVC Generate and Print PDF using Rotativa MVC

In this tutorial, I am going to explain you how to print PDF of webpage in ASP.NET MVC using Rotativa MVC. Rotativa MVC is framework to convert your view into pdf file. Rotativa can be install using nuget package manager. I will be using Visual Studio 2013. Below is step by step tutorial.

Creating ASP.NET MVC Empty Application

Now the next step is to create ASP.NET MVC empty application as shown below.
Go to FileNewProject. A new window will be open as shown below.
Now go to Web and select .NET Framework 4.5 and give project name and click on OK .

Now new window will open as shown below.
Now Select Empty Template, check on MVC checkbox and click on OK.

Now, a new project will be created as shown below.

Installing Rotativa in Mvc

Next step is to install Rotativa. Right click on References and go to Manage NuGet Packages as shown below.

After that a new window will open as shown below. Search for Rotativa and you can see different framework available. I tried first option Rotativa, but it was not installed properly for some reason then I installed Rotativa.MVC which worked. So install Rotativa.MVC in ASP.Net MVC projects.

Adding Models

Go to Models folder and add new class file Employee.cs. Now replace all code with below code.

using System;
using System.Linq;
using System.Web;

    public class Employee
        public int EmployeeId { get; set; }
        public string Name { get; set; }
        public string Gender { get; set; }
        public int Salary { get; set; }

Adding Controller

Next step is to add controller to application. Go to controller folder and add new empty controller named as Home controller. Replace all code with below code. Here first method is Index which returns list of dummy Employee. PrintPage method is used to print your Index view as pdf file. Here we have different print methods like ActionAsPdf, ViewAsPdf, UrlAsPdf, PartialViewAsPdf with different overloads.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication1.Models;
using Rotativa.MVC;

    public class HomeController : Controller
        ListEmployee> list = null;
        public ActionResult Index()
            list = new ListEmployee>() {
            new Employee{EmployeeId=1,Name="Yogesh",Gender="Male",Salary=25000},
            new Employee{EmployeeId=2,Name="Mary",Gender="Female",Salary=30000},
            new Employee{EmployeeId=3,Name="Mike",Gender="Male",Salary=35000},
            new Employee{EmployeeId=4,Name="Rahul",Gender="Male",Salary=40000},
            return View(list);
        public ActionResultPrintPage()
            // for viewing pdf
            return new ActionAsPdf("Index", list);

            // for viewing pdf with file name
            //return new ActionAsPdf("Index", list) { FileName = "File_Name.pdf" };

            // return new ViewAsPdf("Index", list) { FileName = "File_Name.pdf" };

            // url in pdf
            //return new UrlAsPdf("") { FileName = "File_Name.pdf" };

            // partialview in pdf
            //return new PartialViewAsPdf("_PartialViewName", list) { FileName = "File_Name.pdf" };

Adding View

Now, right click on home controller index action method, add new view and name Index. Select Empty template and uncheck Use layout page and click on Add as shown below.

Now go to Views / Home folder, open Index.cshtml file and replace all code with below code.
Here Scripts and CSS files are used for styling the View. And one ActionLink helper method to call PrintPage action method.

Index.cshtml Code:
@model ListEmployee>

h2>Print PDF in ASP.NET MVC Using Rotativa.MVCh2>

script src="~/Scripts/jquery-3.0.0.js">script>
script src="~/Scripts/bootstrap.js">script>
link href="~/Content/bootstrap.css" rel="stylesheet" />

table class="table table-striped table-inverse">
    thead class="thead-inverse">
            th>Employee Idth>
        @foreach (var item in Model)

@Html.ActionLink("Print PDF", "PrintPage", "Home")

When application runs then view can be seen as below.

Now click on Print PDF link, Index view will be rendered as PDF or will be downloaded as PDF file depends on method you have used.

This post first appeared on ASPArticles, please read the originial post: here

Share the post

ASP.NET MVC Generate and Print PDF using Rotativa MVC


Subscribe to Asparticles

Get updates delivered right to your inbox!

Thank you for your subscription