-
-
-
-
- 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.
-
-
-
-
- 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,
-
-
-
-
-
- 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.
-
-
-
-
-
- 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
-
-
-
-
- public ActionResult PrintAllEmployee() {
- var report = new Rotativa.ActionAsPdf("Employees");
- return report;
- }
-
-
-
-
-
- public ActionResult PrintEmployee(int id) {
- var report = new Rotativa.ActionAsPdf("Employee", new {
- id = id
- });
- return report;
- }
-
-
-
-
-
- public ActionResult EmployeeImage(int id) {
- var report = new Rotativa.ActionAsImage("Employee", new {
- id = id
- });
- return report;
- }#endregion
-
-
-
-
- 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.
Summary
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.