How to create PDF from Asp.net MVC with rotativa.io

date Mar 12, 2016

Using Rotativa.io is extremely easy and it will come natural to those familiar with Asp.Net MVC.

First of all install the Nuget package:

PM> Install-Package RotativaHQ

Then add two entries in your web.config file, with data from your Rotativa.io account preferences:

<appsettings>
  <add key="RotativaKey" value="YOUR_KEY" />
  <add key="RotativaUrl" value="YOUR_ENDPOINT" />
</appsettings>

Now you can create PDF docs from a razor view in basically two ways:

  • Using the ViewAsPdf action result, or
  • Using the PdfHelper class.

The PDF helper method was explained in a previous post, Rotativa.io WebApi support. It enables to create a PDF or a link to a PDF stored in Azure cloud storage. It is useful in case you need to do something with the PDF or the URL, for example to store it on you server, or to return it in a JSON result.

If you need to just show the file to the user or make him download it, you’ll use the first method, ViewAsPdf. With it you simply return a new instance in your MVC action, instead of returning the built-in View:

public ActionResult MyAction()
{
    // return View();
    return new ViewAsPdf();
}

This means that Rotativa will create a PDF document using the View with the same action name. So it will look for a MyAction view.

If you want to use a different view, you simply pass it to the constructor: return new ViewAsPdf("AnotherView").

You can use a model or ViewBag variables exactly the same as you would do with a normal return View(myModel); View result.

public ActionResult MyAction(int id)
{
    var myModel = DB.GetMyData(id);
    // return View(myModel);
    return new ViewAsPdf(myModel);
}

Of course you can use all of this together:

public ActionResult MyAction(int id)
{
    var myModel = DB.GetMyData(id);
    ViewBag.SomeData = "Ciao";
    return new ViewAsPdf("AnotherView", myModel);
}

Until now we’ve seen how to create a PDF and show it in the user’s browser. What if you want to make the user automatically download it, with a filename you will set. In this case you can use one of the optional properties of the ViewAsPdf class, the FileName property.

public ActionResult MyAction()
{
    return new ViewAsPdf()
    {
        FileName = "MyPdfFile.pdf"  
    };
}

Other properties available are syntactic sugar for parameters available with the engine Rotativa uses to create PDF docs. This tool is wkhtmltopdf and you can set a number of parameters to customize its execution. Those directly exposed by Rotativa are:

  • PageMargins
  • PageSize
  • PageWidth
  • PageHeight
  • PageOrientation
  • IsJavaScriptDisabled
  • IsLowQuality
  • IsBackgroundDisabled
  • MinimumFontSize
  • Copies

There are also other available, you can check them in the wkhtmltopdf documentation. To use them with Rotativa you pass them in the CustomSwitches property.

30 march 2016 Update

You can manage page PDF header and footers with views very easily: PDF Headers and Footers as Views.