In this article you will learn
- How to Install Rotativa in MVC Project?
- How to Convert View as PDF using Rotativa?
- Simple Programming Example
Here, in this article, I will explain how can you convert any view page into PDF using Rotativa in ASP.NET MVC5. Rotativa is an awesome PDF converter tool that allows you to convert any View Page into PDF using very easy process.
Here, in this example, I am going to Convert default Index page as PDF.
- Create New MVC Project
- Installing Rotativa
1. Installing Rotativa
Step 1: Click on Tools menu > NeGet Package Manager > Package Manager Console.
Note: You never install Rotativa v1.7.3 because it is not stable and gives you error. That's why I am suggesting you to install rotativa v1.7.4 using the following method.
Step 2: Write or Paste following code and press Enter. Wait for the process finish installation.
Install-Package Rotativa -Version 1.7.4-rc
2. Add Method to Controller
Step 1: Open
HomeController.cs
Step 2: Add Following Method in it. You must include using Rotativa namespace.
using System.Web.Mvc; using Rotativa; namespace Rotativa_Simple_Example.Controllers { public class HomeController : Controller { //Other Action Method public ActionResult ConvertToPDF() { var printpdf = new ActionAsPdf("Index"); return printpdf; } } }
3. Add Download PDF button to View Page
Step 1: Open
Index.cshtml
. Go to Solution Explorer > Views > Home > Index.cshtmlStep 2: Add following line of code any where in the page; where you want to put Download as PDF button.
<button>@Html.ActionLink("Downlad as PDF", "ConvertToPDF")</button>