Rotativa ASP MVC HTML to PDF Library

Category: .NET Programming Tags: ,
June 12, 2014

In the past when I needed to create a PDF report in .NET I used to create a SSRS (SQLServer Reporting Services) Report or a Crystal Report. That method is involved and it was only when I coded some PHP work and discovered that components that converted HTML directly to PDF really did simplify things.

For ASP MVC projects (and using NuGet with Visual Studio) you can download Rotativa, which gives you the ability to create PDFs directly from Views or Partial Views. It’s easy to use and works well.

There are probably a few restrictions in the HTML that you may use, and my advice is to keep it simple if you want to keep the PDF layout consistent with the HTML layout. You may need to consider the old style tables layouts rather than complex CSS, but give it a go, you may be surprised with the results. If your page has any dynamic drawing, like graphs, you may want to do a few small tests before committing yourself too much as you may need to create actual file images and import them in.

Examples of using Rotativa

Once installed via NuGet, just include the reference at the top of you Controller:

using Rotativa;

You’re ready to roll already. You can call Views, PartialView, even Actions and instead of displaying them in your browser you will get the standard modal to open as a PDF or save to a file.

You can also select some options for page size, orientation, margins, etc.

public ActionResult HotelPdf(int hid)
{
  return new ActionAsPdf( "Hotel", new { hotelID = hid } )
  {
    FileName = "MyDoc.pdf",
    PageSize = Size.A4,
    PageOrientation = Orientation.Landscape,
    PageMargins = { Left = 10, Right = 10 }
  };
}

Using a Partial View is almost identical and provides a good method of reusing the code for both browser and PDF:

return new PartialViewAsPdf( "_ShortListWithNotes", GetShortListView() )
{
  FileName = "MyDoc.pdf"
  PageSize = Size.A4,
  PageOrientation = Orientation.Landscape,
  PageMargins = { Left = 10, Right = 10 }
};

You can see already how easy this is.

Drawbacks

When you use SSRS reports you get pampered with the automatic features which you may think of as basic, such as paging, headers, footers, numbering, etc. HTML to PDF convertors often lack these features; it’s the tradeoff for simplicity.

An example is when I searched Google how to insert a simple page break and I was surprised to find how many where asking the same question and having difficulty. It’s one of this things you need to do yourself in the HTML, such as this solution:

In your CSS:

.page-breaker {
 display: block;
 clear: both;
 page-break-after: always;
 }

The add this at the end of each page:

<div class="page-breaker"> </div>

It’s no real problem but I just wanted to highlight the fact you may need to get involved in some of this fiddly low-level stuff. If I’ve got to do this manually I would have prefered short-code directives.

Given all that, I think I will be using Rotativa often because I like the simplicity and convenience of the drop-in solution it gives. There are limits in its use but I probably don’t need a full blown Report Generator for simple PDFs.