How to render an ASP.NET MVC View in PDF format

I packaged my solution in a Nuget package: Rotativa http://nuget.org/packages/Rotativa. It’s based on wkhtmltopdf.

Usage is really simple.

Having an action you would like to serve as Pdf, instead of Html page. You can define an action that returns an ActionResult of the type ActionAsPdf (RouteAsPdf is also available).
So the code is just:

public ActionResult PrintIndex()
{
    return new ActionAsPdf("Index", new { name = "Giorgio" }) { FileName = "Test.pdf" };
}

With name = “Giorgio” being a route parameter.

It works even if the action to print is protected by web forms authentication ([Authorize] attribute)

Leave a Comment