Click here to Skip to main content
15,901,205 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 207.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

 
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 
QuestionIt is indeed fast and very good! Pin
PiyushVarma3-Oct-14 7:02
PiyushVarma3-Oct-14 7:02 
AnswerRe: It is indeed fast and very good! Pin
khem thapa3-Oct-14 17:16
khem thapa3-Oct-14 17:16 
QuestionSpelling Mistake? Pin
PiyushVarma3-Oct-14 6:29
PiyushVarma3-Oct-14 6:29 
You might first want to rename the project from DonwloadPDF to DownloadPDF to start with please.
AnswerRe: Spelling Mistake? Pin
khem thapa3-Oct-14 17:03
khem thapa3-Oct-14 17:03 
QuestionNuGet Package Manager Pin
PiyushVarma3-Oct-14 6:19
PiyushVarma3-Oct-14 6:19 
AnswerRe: NuGet Package Manager Pin
khem thapa3-Oct-14 17:13
khem thapa3-Oct-14 17:13 
QuestionThis was a tip... Pin
OriginalGriff12-Sep-14 8:04
mveOriginalGriff12-Sep-14 8:04 
AnswerRe: This was a tip... Pin
khem thapa12-Sep-14 8:27
khem thapa12-Sep-14 8:27 

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.