Introduction
We have used many tools to generate/print the PDF documents from our application or database but this Rotativa will work like a Crystal Report to generate PDF documents and additionality, Rotativa will help us to convert the layout in an image. It is a framework and it provides free APIs to achieve this. Okay! Now, let's move forward to use this.
Prerequisites
- Visual Studio
- Basic knowledge of C#
Article Flow
- Introduction to Rotativa
- Create ASP.NET MVC Empty project
- Create a Model
- Load Employees and design a View
- Flow of Rotativa
- Generate PDF
- Generate Image
Introduction to Rotativa
Rotativa is an open source package. We can easily generate the pdf using their library. Rotativa uses the web kit engine which is used by Chrome browser to render the HTML. Most of the HTML tags and styles are supported by this framework. The Rotativa framework provides Rotativa namespace. This namespace contains the following classes.
ActionAsPdf
It accepts a View named as string parameter so that it can be converted into PDF and also, we are able to pass the second parameter, i.e., the action input params.
Syntax
- public ActionAsPdf(string action);
- public ActionAsPdf(string action, RouteValueDictionary routeValues);
- public ActionAsPdf(string action, object routeValues);
PartialViewAsPdf
It's used to return the partial view as PDF.
Syntax
- public PartialViewAsPdf();
- public PartialViewAsPdf(string partialViewName);
- public PartialViewAsPdf(object model);
- public PartialViewAsPdf(string partialViewName, object model);
It enables us to return any URL as PDF.
Syntax
- public UrlAsPdf(string url);
ViewAsPdf
It returns the result as PDF instead of HTML Response.
Like this, Rotativa has more than 10 classes to generate pdf and images. We will see one by one.
Create ASP.NET MVC Empty project
- To create an ASP.NET MVC empty project, follow the below steps one by one. Here, I have used Visual Studio 2017.
- Select New Project -> Visual C# -> Web -> ASP.NET Web Application and enter your application name. Here, I named it "GeneratePDF".
- Now, click OK.
- Then, select Empty ASP.NET MVC template and click OK to create the project.
- Once you click OK, the project will be created with the basic architecture of MVC. If you are not aware of how to create an Empty ASP.NET Web Application, please visit Step 1 and Step 2 to learn.
Once you complete these steps, you will get the screen as below.
Create a Model
Now we are going to create a model to hold the values of the employee. Here, I have created one with the name of "EmployeeModel".
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace GeneratePDF.Models {
- public class EmployeeModel {
- public int ID {
- get;
- set;
- }
- public string Name {
- get;
- set;
- }
- public string Designation {
- get;
- set;
- }
- public decimal Salary {
- get;
- set;
- }
- }
- }
Load Employees and design a view
I have created the list of an employee under "GeneratePDF" controller as below
- /// <summary>
- /// Load all Employees
- /// </summary>
- /// <returns></returns>
- private List < EmployeeModel > LoadEmployees() {
- List < EmployeeModel > employees = new List < EmployeeModel > () {
- new EmployeeModel() {
- ID = 1, Name = "Gnanavel Sekar", Designation = "Sr.Software Engineer", Salary = 1500000
- },
- new EmployeeModel() {
- ID = 2, Name = "Subash S", Designation = "Sr.Software Engineer", Salary = 1700000
- },
- new EmployeeModel() {
- ID = 3, Name = "Robert A", Designation = "Sr.Application Developer", Salary = 1800000
- },
- new EmployeeModel() {
- ID = 4, Name = "Ammaiyappan", Designation = "Sr.Software Developer", Salary = 1200000
- }
- };
- return employees;
- }
Create Action and View
Now let's create an action to return the list of an employee detail to the respective view
- public ActionResult Employees()
- {
- return View(LoadEmployees());
- }
And now create a strongly typed view as the list for this "Employees" action to view the list of an employee in table format using employee model.
- @model IEnumerable<GeneratePDF.Models.EmployeeModel>
- @{
- ViewBag.Title = "Employees";
- Layout = "~/Views/Shared/_Layout.cshtml";
- }
- <br />
- <table class="table table-bordered">
- <tr>
- <th> @Html.DisplayNameFor(model => model.Name) </th>
- <th> @Html.DisplayNameFor(model => model.Designation) </th>
- <th> @Html.DisplayNameFor(model => model.Salary) </th>
- <th></th>
- </tr> @foreach (var item in Model) { <tr>
- <td> @Html.DisplayFor(modelItem => item.Name) </td>
- <td> @Html.DisplayFor(modelItem => item.Designation) </td>
- <td> @Html.DisplayFor(modelItem => item.Salary) </td>
- <td> @Html.ActionLink("Details", "Employee", new { id=item.ID }) </td>
- </tr> }
- </table>
Run your application,

Flow of Rotativa
Step 1 - Install Rotativa From Nuget Package
Step 2 - Rotativa Folder Creation
After Installing Rotativa package from NuGet, the new folder of the name Rotativa will be created in the project containing wkhtmltopdf.exe. This exe is a command line tool to render HTML into PDF. You will able to see of two features available in the below screenshot such as Html to Image and Html to PDF
Rotativa Lifecycle as below for developers
Generate PDF
Now let's try to convert the employee List view to PDF. In the below code, I have created a "PrintAllEmployee" action and called the Rotativa Action method "actionasPdf" which is used to convert the action result as pdf. The Action method PrintAllEmployee() creates an instance of ActionAdPdf() class. The constructor of this class accepts a View Name as a string parameter. In this case, the Index view is passed to it. This class prints the HTML rendered output of the "Employees" view as a PDF document.
- /// <summary>
- /// Print Employees details
- /// </summary>
- /// <returns></returns>
- public ActionResult PrintAllEmployee()
- {
- var report = new Rotativa.ActionAsPdf("Employees");
- return report;
- }
Now call this "PrintAllEmployee" from our view
- <div style="text-align:right">@Html.ActionLink("Print Employees", "PrintAllEmployee")</div>
Now run your application,
In the above result, we can see that page has been converted as pdf. Now let's try to do this for a particular employee.
Now, write logic in the controller to get respective employee details. The Action method Employee() accepts an id parameter. This method searches an Employee based on its id and returns a view object showing an Employee information
- public ActionResult Employee(int id)
- {
- var employee = LoadEmployees().Where(a => a.ID == id).FirstOrDefault();
- return View(employee);
- }
Create a view for this and load the same.
- @model GeneratePDF.Models.EmployeeModel
- @{
- ViewBag.Title = "Employee";
- Layout = "~/Views/Shared/_Layout.cshtml";
- }
- <h2>Employee Details</h2>
- <br />
- <table class="table table-bordered">
- <tr>
- <th> @Html.DisplayNameFor(model => model.Name) </th>
- <th> @Html.DisplayNameFor(model => model.Designation) </th>
- <th> @Html.DisplayNameFor(model => model.Salary) </th>
- <th></th>
- </tr>
- <tr>
- <td> @Html.DisplayFor(modelItem => Model.Name) </td>
- <td> @Html.DisplayFor(modelItem => Model.Designation) </td>
- <td> @Html.DisplayFor(modelItem => Model.Salary) </td>
- <td> @Html.ActionLink("Back To List", "Employees") </td>
- </table>
Now let's write Rotativa logic to get selected employee details,
- /// <summary>
- /// Print employee details
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public ActionResult PrintEmployee(int id)
- {
- var report = new Rotativa.ActionAsPdf("Employee", new { id=id });
- return report;
- }
Now call this PrintEmployee from employee view and pass the employee id as input param
- <div style="text-align:right">@Html.ActionLink("Print Employee", "PrintEmployee", new { id=Model.ID })</div>
Now Run your application. In the below image, you can see that the employee detail is converted as a pdf document.
Generate Image
As of now, we have seen how to generate pdf. Now let's convert the layout as an image using Rotativa. In the below code, you can see that I have used the "ActionAsImage" class to convert the respective employee details to the image.
- /// <summary>
- /// employee details as image
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public ActionResult EmployeeImage(int id)
- {
- var report = new Rotativa.ActionAsImage("Employee", new { id=id });
- return report;
- }
Now call this action by passing input parameter from view
- <div style="text-align:right">@Html.ActionLink("Convet to Image", "EmployeeImage", new { id=Model.ID })</div>
Run your application
Complete Controller
- using GeneratePDF.Models;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web.Mvc;
- namespace GeneratePDF.Controllers {
- public class GeneratePDFController: Controller {
- #region Grid Portion
- public ActionResult Employees() {
- return View(LoadEmployees());
- }
- public ActionResult Employee(int id) {
- var employee = LoadEmployees().Where(a => a.ID == id).FirstOrDefault();
- return View(employee);
- }
- #endregion
- #region Rotativa
- /// <summary>
- /// Print Employees details
- /// </summary>
- /// <returns></returns>
- public ActionResult PrintAllEmployee() {
- var report = new Rotativa.ActionAsPdf("Employees");
- return report;
- }
- /// <summary>
- /// Print employee details
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public ActionResult PrintEmployee(int id) {
- var report = new Rotativa.ActionAsPdf("Employee", new {
- id = id
- });
- return report;
- }
- /// <summary>
- /// employee details as image
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public ActionResult EmployeeImage(int id) {
- var report = new Rotativa.ActionAsImage("Employee", new {
- id = id
- });
- return report;
- }#endregion
- /// <summary>
- /// Load all Employees
- /// </summary>
- /// <returns></returns>
- private List < EmployeeModel > LoadEmployees() {
- List < EmployeeModel > employees = new List < EmployeeModel > () {
- new EmployeeModel() {
- ID = 1, Name = "Gnanavel Sekar", Designation = "Sr.Software Engineer", Salary = 1500000
- },
- new EmployeeModel() {
- ID = 2, Name = "Subash S", Designation = "Sr.Software Engineer", Salary = 1700000
- },
- new EmployeeModel() {
- ID = 3, Name = "Robert A", Designation = "Sr.Application Developer", Salary = 1800000
- },
- new EmployeeModel() {
- ID = 4, Name = "Ammaiyappan", Designation = "Sr.Software Developer", Salary = 1200000
- }
- };
- return employees;
- }
- }
- }
Employees.cshtml
- @model IEnumerable<GeneratePDF.Models.EmployeeModel>
- @{
- ViewBag.Title = "Employees";
- Layout = "~/Views/Shared/_Layout.cshtml";
- }
- <br />
- <div style="text-align:right">@Html.ActionLink("Print Employees", "PrintAllEmployee")</div>
- <br />
- <table class="table table-bordered">
- <tr>
- <th> @Html.DisplayNameFor(model => model.Name) </th>
- <th> @Html.DisplayNameFor(model => model.Designation) </th>
- <th> @Html.DisplayNameFor(model => model.Salary) </th>
- <th></th>
- </tr> @foreach (var item in Model) { <tr>
- <td> @Html.DisplayFor(modelItem => item.Name) </td>
- <td> @Html.DisplayFor(modelItem => item.Designation) </td>
- <td> @Html.DisplayFor(modelItem => item.Salary) </td>
- <td> @Html.ActionLink("Details", "Employee", new { id=item.ID }) </td>
- </tr> }
- </table>
Employee.cshtml
- @model GeneratePDF.Models.EmployeeModel
- @{
- ViewBag.Title = "Employee";
- Layout = "~/Views/Shared/_Layout.cshtml";
- }
- <h2>Employee Details</h2>
- <div style="text-align:right">@Html.ActionLink("Print Employee", "PrintEmployee", new { id=Model.ID })</div>
- <br />
- <div style="text-align:right">@Html.ActionLink("Convet to Image", "EmployeeImage", new { id=Model.ID })</div>
- <br />
- <table class="table table-bordered">
- <tr>
- <th> @Html.DisplayNameFor(model => model.Name) </th>
- <th> @Html.DisplayNameFor(model => model.Designation) </th>
- <th> @Html.DisplayNameFor(model => model.Salary) </th>
- <th></th>
- </tr>
- <tr>
- <td> @Html.DisplayFor(modelItem => Model.Name) </td>
- <td> @Html.DisplayFor(modelItem => Model.Designation) </td>
- <td> @Html.DisplayFor(modelItem => Model.Salary) </td>
- <td> @Html.ActionLink("Back To List", "Employees") </td>
- </table>
Refer to the attached project for reference and I did attach the demonstrated project without a package and Rotativa exe due to the size limit.
The Rotativa package provides an extremely easy way to convert an HTML response directly into a PDF document, print the PDF document and generate the image in an ASP.NET MVC.
I hope you enjoyed this article. Your valuable feedback and comments about this article are always welcome.

Secret AkoPosted Feb 24, 2022, 6:33 AM
How can I remove the header and footer of my website when exporting to pdf?
PDF HelpPosted Dec 30, 2021, 9:23 PM
In my Query Handler I have a Position="✓" but in the PDF it shows as a square. How can I make the PDF recognize my checkmark? Is it possible?
Ela GrophyPosted Dec 9, 2020, 7:58 PM
Can I include an another view as a part of pdf with CustomSwitches? such as CustomSwitches =" \" https://localhost:44367/home/header\"" , add it as the page's header?
Alejandro HernandezPosted Jun 13, 2020, 4:38 PM
Hey, When I pass an Url to a view that is in another controller I get this: Se detect? un posible valor Request.Path peligroso en el cliente (?). any idea?
Ahmed AdelPosted Nov 25, 2019, 5:27 AM
Nice article, but i want to ask about something , does Rotativa takes our data to their Sever?
Amir NavedPosted Nov 13, 2019, 3:12 AM
I don't need header and footer of the web page
Amir NavedPosted Nov 12, 2019, 1:44 AM
Thanks dear
Sazal PradhanPosted Sep 27, 2019, 12:24 PM
Can Rotativa generate WCAG 2.0 compliant PDF?
Sundaram SubramanianPosted Sep 26, 2019, 2:34 AM
Does Rotativa Nuget Package sends out data to them (Like, Rotativa takes our data to their Sever) ?. My technical team says not to use rotativa nuget !!!.
vishnupriya ravichandranPosted Apr 10, 2019, 11:42 PM
Nice article , but i want only the details of that particular employee to be downloaded as pdf format when i click on print tab
Sven AndreasPosted Mar 26, 2019, 7:49 AM
This is rly a nice manual, thank you very much indeed!
yornn sopratnaPosted Feb 15, 2019, 7:14 PM
Hi I tried serveral of time to download the GeneratePdf.zip but still failed to download any help please ?
Talaviya BhavdipPosted Jan 8, 2019, 9:47 PM
Nice article
Arvind SinghPosted Jan 8, 2019, 9:14 PM
Nice article...
Alice NguyenPosted Jan 8, 2019, 8:50 PM
Nice great article, thank you so much
Satyaprakash SamantarayPosted Jan 8, 2019, 10:27 AM
Nice article with description. Keep sharing.