Rotativa – A tool for PDF Generation in ASP.NET MVC

In this article I have explained a tool that is used to generate PDF with some line of code in ASP.NET MVC. Rotativa is a tool or we can say an ASP.NET MVC library which is available in Nuget Library by which we can generate the PDF for any Documents and Reports to print in a very easy and quick manner.

Before starting with this topic we should have a basic knowledge of ASP .Net MVC. Here, I am just giving the basic overview of ASP .NET MVC. Microsoft provided the ASP.NET MVC (Model-View-Controller) a web development framework and an architectural design pattern which is basically useful for separation of representation and user interaction. It supports the separation of concern in ASP .Net. It is a lightweight, highly testable framework, integrated with all existing ASP.NET features, such as Master Pages, Security, and Authentication.

Model (Business Layer) - Managing the data of the application that handles the logic for the application data. Model represents the real world object and provides data to the View.

View (Display Layer) - Renders the display of the data. Most often the views are created from the model data.

Controller (Input Layer) - Controller takes the input from Model and process the data and give the output to the View to render it. Any request in MVC first hit the controller.


"Rotativa" is an Italian word and was used for rotary printing press.

Rotativa is a tool or we can say an ASP.NET MVC library which is available in Nuget Library by which we can generate the PDF for any Documents and Reports to print in a very easy and quick manner. In ASP.Net MVC, we have views, partial views or URLs from where we can generate the PDF directly with greater flexibility and in a very quick manner with no more hurdle. It gives you the flexibility to create PDFs directly from Views or Partial Views or URLs. Creating PDF docs in Asp.Net MVC is a fairly common functionality requested by Many applications. Usually either for reporting or, more often, for having printable friendly documents (invoices, receipts etc).

Rotativa is derived version of wkhtmltopdf which has the idea to convert HTML to pdf. It is the main concept behind Rotativa.

Demonstration

Now, to understand we will create a sample web application. For this we need to install the ‘Rotativa’ from ‘Package manager console’ to better understand the concept of Rotativa by following the below steps.

  1. Open Visual Studio 2013
  2. File -> New Project
  3. Create the application named “ExampleRotativa1” as shown in screenshot



    Application created.
  4. Under the Visual Studio -> Tools -> Library Package Manger->Package Manager Console as shown in below screen shot.



    Installing Rotativa, Click on ‘Package Manager Console’ as shown in above screen.

    A command window will open and type the below command:

    PM> Install-Package Rotativa and then press Enter key

    After successful installment of Rotativa, a folder named “Rotativa” containing “wkhtmltopdf.exe” will be created in our solution and also a reference named “qt.browser” will be added in auto created folder “App_browsers”.


  5. Create the View/Model/Controller in application as shown in below screenshot.

    Model:



    View:



    • Create a Model class Named Student.

      namespace ExampleRotativa1.Models
      {
          public class Student
          {
              public string Name { get; set; }
              public string Email { get; set; }
              public string Subject { get; set; }
              public int Age { get; set; }
          }
      }
    • Create a View named GetStudentData.

      @model IEnumerable<ExampleRotativa1.Models.Student>
      
      <h1> Student Data </h1>
      
      <table>
          <tr>
              <th>
                  @Html.DisplayNameFor(model => model.Name)
              </th>
              <th>
                  @Html.DisplayNameFor(model => model.Email)
              </th>
              <th>
                  @Html.DisplayNameFor(model => model.Subject)
              </th>
              <th>
                  @Html.DisplayNameFor(model => model.Age)
              </th>
          </tr>
      
          @foreach(var item in Model)
          {
          <tr>
              <td>
                  @Html.DisplayFor(modelItem => item.Name)
              </td>
              <td>
                  @Html.DisplayFor(modelItem => item.Email)
              </td>
              <td>
                  @Html.DisplayFor(modelItem => item.Subject)
              </td>
              <td>
                  @Html.DisplayFor(modelItem => item.Age)
              </td>
          </tr>
          }
      </table>
      <p>
      
          <a href="CreatePDF">Generate Student Data in PDF</a>
    • Create an Action method GetStudent under Controller class named Home.

      using ExampleRotativa1.Models;
      
      namespace ExampleRotativa1.Controllers
      {
          public class HomeController : Controller
          {
              //Create Student Data
              public ActionResult GetStudentData()
              {
                  List<Student> InfyData = new List<Student>();
                  InfyData.Add(new Student() { Name = "Monika", Email = "Monika@gmail.com", Subject = "CS", Age = 27 });
                  InfyData.Add(new Student() { Name = "Neha", Email = "Monika@gmail.com", Subject = "CS", Age = 27 });
                  InfyData.Add(new Student() { Name = "Shweta", Email = "Monika@gmail.com", Subject = "CS", Age = 27 });
      
                  return View(InfyData);
              }
      
              //To generate the PDF 
              public ActionResult GeneratePDF()
              {
                  return new Rotativa.ActionAsPdf("Student");
              }
          }
      }
  6. Save the data and run the application where u will see the output as shown in below screenshot.



    Here clicking on “Generate Student Data in PDF” u can get your PDF as shown in below screenshot



    We can print the PDF as well. :)

  7. We can provide the customization like adding Header/Footer/Comment as per our requirement using the code in”_Layout.cshtml” file in MVC.

Action method present in Rotativa:

There are different Action methods presents in Rotativa library:

  1. ViewAsPdf – It is useful, when we want to generate the PDF of views. It has overload constructor.
  2. ActionAsPdf - It is useful, when we want to generate the PDF using another action method.
  3. PartialViewAsPdf – It is useful, when we want to generate the PDF of partial views
  4. UrlAsPdf – It is useful, when we want to create PDF of any URL content.

Advantages:

  1. Rotativa provides an extremely easy/simple/reliable way to create Pdf files from Asp.net MVC with no more hurdle and also it is very less time consuming.
  2. If we want to see the whole View content in PDF in MVC, directly we can use the action method “ViewAsPdf”.
  3. Using authorized actions (web forms authentication), we can generate the PDF as well.

Latest version:

Rotativa 1.6.4 is available in Nuget package when article was posted.


Reference(s)

  1. www.msdn.microsoft.com
  2. www.codeproject.com