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 File â†’ New â†’ Project. 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.
Go to Models folder and add new class file Employee.cs. Now replace all code with below code.
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.
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.
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.