Using RotativaHQ 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 RotativaHQ account preferences:
Now you can create PDF docs from a razor view in basically two ways:
- Using the
ViewAsPdfaction result, or
- Using the
The PDF helper method was explained in a previous post, RotativaHQ 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
This means that Rotativa will create a PDF document using the View with the same action name. So it will look for a
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.
Of course you can use all of this together:
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
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:
There are also other available, you can check them in the wkhtmltopdf documentation. To use them with Rotativa you pass them in the
30 march 2016 Update
You can manage page PDF header and footers with views very easily: PDF Headers and Footers as Views.