Print View as PDF using Rotativa – Very Simple C# MVC Example

In this article you will learn
  1. How to Install Rotativa in MVC Project?
  2. How to Convert View as PDF using Rotativa?
  3. 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.

  1. Create New MVC Project
  2. 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.
Nuget Package Manager Console Rotativa
Step 2: Write or Paste following code and press Enter. Wait for the process finish installation.

Install-Package Rotativa -Version 1.7.4-rc

Installing Rotativa from Console

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.cshtml

Step 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>

4. Run the project.

Simple PDF Example Output 1 Simple PDF Example Output 2

Summary

In this article, I tried to tell you how to download any page as PDF using Rotativa. In the next article, I will explain how to convert partial view page as PDF using Rotativa.

More Articles

 

Share your thought