Tips for creating PDF files with RotativaHQ

date Apr 3, 2016

Using Rotativa.io in the same way you use Asp.Net code should be enough to get reasonable performance in creating your PDF files. There are cases, though, perhaps complex or large pages, in which this is not enough. In this blog post we will cover some aspects that, with some attention, can make your PDF creation faster.

Web page elements

Most issues about PDF creation slowness can be routed down to web page slowness. In the case of remote PDF creation this issues can stand out greatly. It all boils down to total page size, and I mean total by adding up all elements needed for a page render.

Using templates and layouts provided by Visual Studio Asp.net MVC tooling is really great and saves lots of time and headaches. Sometimes though this can make our pages unnecessarily large and bloated with unused elements. Think of javascript references, perhaps you don’t need them for you PDF. Perhaps a simpler css file would be enough.

For those reasons consider making a dedicated layout page, maybe taking the _Layout.cshtml as a source and stripping out unused elements. For css files consider inspecting your PDF HTML source for unused styles. You can do this by visualizing your HTML as a page instead of a PDF, turning ViewAsPdf to View and using Chrome Developer Tools using a technique explained in this blog post: Remove Unused CSS to Reduce the Size of your Stylesheets.

Compression

By default Rotativa.io compresses the content of the API call to the service. This is really useful in the development stage and shouldn’t effect substantially once deployed to the server. But in some scenario this can affect performance if you use the standard Bundles Asp.Net MVC feature. Bundling means that multiple js or css files are bundled together and minified. Compressing and uncompressing minified content can result in unnecessary computing activity.

The good news is that you can in fact control whether compression is applied or not very easily. There is Web.config appsetting you can use, it’s the RotativaGZip setting. You can easily setup a Web.config transform to disable compression only for the published web app. Simply add this to the Web.Release.config file:

<appsettings>
  <add key="RotativaGZip" xdt:Transform="Remove" 
    xdt:Locator="Match(key)"/>
  <add key="RotativaGZip" value="0" xdt:Transform="Insert" />
</appsettings>

This way you will benefit from compression when developing locally and, at the same time, avoid unnecessary work on the server thus having faster PDF creation.