Click here to Skip to main content
15,881,882 members
Articles / Web Development / HTML
Tip/Trick

Generate PDF in ASP.NET MVC Using Rotativa

Rate me:
Please Sign up or sign in to vote.
4.70/5 (16 votes)
12 Sep 2014CPOL3 min read 206.8K   8.8K   25   33
Generate PDF in ASP.NET MVC using Rotativa library

Introduction

Generating PDF for report or any document purpose that can be printable in .NET is a bit cumbersome. But this can be achieved in ASP.NET MVC very easily and quickly using Rotativa tools which is available in Nuget package. It gives you the flexibility to create PDFs directly from Views or Partial Views or URLs too.

Rotativa is an ASP.NET MVC library, which helps to generate PDF from MVC controller.

Using the Code

How to get or install Rotativa in MVC application?

We can directly install rotativa using package manager console (i.e View-> Package Manager Console) and then type Install-Package Rotativa

Or

We can do by searching it from Nuget package manager (i.e Project-> Manage Nuget Packages-> search nuget-> Install).

A folder with a name Rotativa in your project gets created, along with a Rotativa package folder in project root directory and also gets the reference to your solution.

Now we are ready to move on and create a new demo for PDF generation:

Create a demo MVC application named as "DonwloadPDF", where we will generate a PDF with some content and logo.

The solution is implemented with four classes of Rotativa in HomeController.cs to generate PDF.

  • ViewAsPdf – This class will generate the PDF based on views. It has overload constructor, like

    Implemented only one

    C#
    public ActionResult DownloadViewPDF()
    {
    var model = new GeneratePDFModel();
    //Code to get content
    return new Rotativa.ViewAsPdf("GeneratePDF", model){FileName = "TestViewAsPdf.pdf"}
    }

    Here "GeneratePDF" is the view name i.e., GeneratePDF.cshtml(Razor view)

  • ActionAsPdf - will take other action method to generate PDF using view
    C#
    public ActionResult DownloadActionAsPDF()
    {
      var model = new GeneratePDFModel();
      //Code to get content
      return new Rotativa.ActionAsPdf("GeneratePDF", model){FileName = "TestActionAsPdf.pdf"};
    }

    Here "GeneratePDF" is the name of other action which will return a view to generate PDF.

    C#
    public ActionResult GeneratePDF()
    {
      var model = new GeneratePDFModel();
      //get content
      return View(model);
    }
  • PartialViewAsPdf – This class is used to generate PDF of partial view.
    C#
    public ActionResult DownloadPartialViewPDF()
    {
     var model = new GeneratePDFModel();
     //Code to get content
     return new Rotativa.PartialViewAsPdf("_PartialViewTest", model){FileName = "TestPartialViewAsPdf.pdf" };
    }

    Here "_PartialViewTest" is the name of the partial view i.e., "_PartialViewTest.cshtml"(Razor view).

  • UrlAsPdf – Using this class, we can create PDF of any URL content.
    C#
    public ActionResult UrlAsPDF()
    {
       return new Rotativa.UrlAsPdf("http://www.Google.com"){FileName = "UrlTest.pdf"};
    }

    UrlAsPdf will return the content of the site in PDF. For example, www.google.com site content will display in PDF.

    Action link to call method in home controller from index.cshtml:

    C#
    <div><a href="@Url.Action("DownloadViewPDF", "Home")">Download ViewAsPdf</a></div>
    <div><a href="@Url.Action("DownloadActionAsPDF", "Home")">Download ActionAsPdf</a></div>
    <div><a href="@Url.Action("DownloadPartialViewPDF", "Home")">Download PartialViewAsPDF</a></div>
    <div><a href="@Url.Action("UrlAsPDF", "Home")">Download UrlAsPDF</a></div>

Follow the below steps to create a solution and run:

  1. Open Visual Studio, then create a new project
    File ? new Project ? MVC Web Application ? Empty Template
  2. Add New Controller named as “HomeController.cs
  3. Add “Home” as sub folder in a view folder and then Add view Index.cshtml, GeneratePDF.cshtml and _PartialTestView.cshtml (razor view)
  4. Add new folder to store images as “Images” and add a logo images.

And now your project structure is like:

Image-1 (final structure)

Now implement the above code and run. Yes, now click on each link, you can see the standard model popup to open as a PDF or save.

Get the attached demo project to check and run the demo project.

Note: Install Rotativa before run the demo as per descriptions.

Update Section

                   We can add  footer and page number very easily in pdf. Just need to add one more parameter named as "CustomSwiches".

Check below code:

      string footer= "--footer-right \"Date: [date] [time]\" " + "--footer-center \"Page: [page] of [toPage]\" --footer-line --footer-font-size \"9\" --footer-spacing 5 --footer-font-name \"calibri light\"";

Now implementation will be like:-

return new Rotativa.ViewAsPdf("GeneratePDF", model)
{
    FileName = "firstPdf.pdf",
    CustomSwitches = footer
};

Points of Interest:

Hmm, now we can easily download or generate a PDF in MVC application. And also can add page number and footer and all.

Reference: GitHub https://github.com/webgio/Rotativa

Reference: http://madalgo.au.dk/~jakobt/wkhtmltoxdoc/wkhtmltopdf-0.9.9-doc.html

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
QuestionPrint section by section on page Pin
sagitt241114-May-20 23:26
sagitt241114-May-20 23:26 
QuestionFont-awesome icons are not coming Pin
Nilam Savani1-Mar-19 10:36
Nilam Savani1-Mar-19 10:36 
QuestionRotativa asp net core web API Pin
RB Maksim14-Oct-18 22:29
RB Maksim14-Oct-18 22:29 
QuestionMerge Attachments with PDF Created by Rotativa? Pin
Frank Benício3-Sep-18 4:18
Frank Benício3-Sep-18 4:18 
QuestionThanks Pin
Member 1359299325-Dec-17 3:14
Member 1359299325-Dec-17 3:14 
Questiondoes rotativa support loading more images.? Pin
Soumya Rajesh11-Dec-17 1:13
Soumya Rajesh11-Dec-17 1:13 
QuestionError running the application Pin
Member 113987136-Feb-17 5:21
Member 113987136-Feb-17 5:21 
QuestionFor Clarification Pin
Member 1268732324-Sep-16 21:19
Member 1268732324-Sep-16 21:19 
QuestionDIV using Rotativa Pin
zetaperu19-May-16 8:37
zetaperu19-May-16 8:37 
PraiseBrilliant Work Pin
UsmanZeb22-Apr-16 22:15
UsmanZeb22-Apr-16 22:15 
QuestionRotative does not work in MVC 6! Pin
marcosph27-Jul-15 10:40
marcosph27-Jul-15 10:40 
QuestionWhy Rotaiva Not Working When i hosted in Server...?????? Pin
Member 118068278-Jul-15 20:30
Member 118068278-Jul-15 20:30 
AnswerRe: Why Rotaiva Not Working When i hosted in Server...?????? Pin
khem thapa9-Jul-15 5:31
khem thapa9-Jul-15 5:31 
AnswerRe: Why Rotaiva Not Working When i hosted in Server...?????? Pin
ransems27-Jun-19 15:00
ransems27-Jun-19 15:00 
GeneralMy vote of 5 Pin
rosdi23-May-15 8:12
rosdi23-May-15 8:12 
Questionthe demo is not working Pin
vishalrathaure12-Apr-15 21:55
vishalrathaure12-Apr-15 21:55 
AnswerRe: the demo is not working Pin
Hewbert Gabon14-Apr-15 2:27
Hewbert Gabon14-Apr-15 2:27 
GeneralRe: the demo is not working Pin
khem thapa14-Apr-15 2:57
khem thapa14-Apr-15 2:57 
GeneralRe: the demo is not working Pin
Hewbert Gabon14-Apr-15 4:05
Hewbert Gabon14-Apr-15 4:05 
QuestionAnother Wow! This can utilize Bootstrap 3!!! Pin
Hewbert Gabon12-Apr-15 8:59
Hewbert Gabon12-Apr-15 8:59 
QuestionThe cleanest, easiest, simplest .NET pdf generator ever!!! Pin
Hewbert Gabon12-Apr-15 7:48
Hewbert Gabon12-Apr-15 7:48 
QuestionRotativa Pin
Member 104122218-Mar-15 13:08
Member 104122218-Mar-15 13:08 
Questionis there a way you attach the PDF in an email Pin
soorma9-Feb-15 5:29
soorma9-Feb-15 5:29 
QuestionNice Tutorial. Pin
bhairab.dutt16-Dec-14 22:49
bhairab.dutt16-Dec-14 22:49 
QuestionHow to Compose several htmls into one pdf Pin
Kumar Ajay from India2-Nov-14 18:54
Kumar Ajay from India2-Nov-14 18:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.