Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Rotativa, how to print PDF in ASP.NET MVC

, 24 Feb 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Print PDF in ASP.NET MVC.

Creating PDF docs in Asp.Net MVC is a fairly common functionality requested by LOB applications. Usually either for reporting or, more often, for having printable friendly documents (invoices, receipts etc).

I used to design a Crystal Report document and then evaluate it and convert it to PDF in the web application. The desing part was OK but what really bothered me was the problems that often came up with deploying it. Version mismatches could really turn it into a nightmare.

Lately I came up using the excellent wkhtmltopdf tool to convert html content to PDF. It uses the WebKit engine (used by Chrome and Safari) to render HTML. The nice thing about it is that I can leverage my knowledge of html and css to obtain a good looking PDF and also it's quite fast.

It's an exe file not a DLL library (a DLL actually exists but it's not managed code and it has some problems related to usage in a multi-threaded app). It has to be executed from the Asp.net app spawning a process, so it requires some unusual coding from a web development perspective. Another downside is that it requires some work to set it up: copying exe and dll files in the web app solution, make sure they get copied over when building and publishing and some config stuff needed to make it access authenticated actions.

It seemed to me a perfect candidate to become a Nuget package. It was fairly easy to build it, except some quirks when trying to modify package properties.

I've named it Rotativa /rota'tiva/, which is Italian for rotary printing press. With Rotativa, all it takes to print a PDF is:

  1. Install using the Nuget package manager (search for Rotativa) or, more conveniently, with the package manager console, typing:
  2. Install-Package Rotativa
  3. Writing a controller action to return a view representing the desired PDF output. This means just coding as usual to serve the data as regular MVC action. Such as:
  4. public ActionResult Invoice(int invoiceId)
    {
        var invoiceViewModel;
        // code to retrieve data from a database
        return View(invoiceViewModel);
    }

    Nothing special here, just an action. Perhaps the view used will have a different master page (or layout page) then the other views of the web app (or you’ll define a separete css for the print media, see http://www.w3.org/TR/CSS2/media.html). You can test and “preview” the results just using the browser, since we are working with a regular action, returning a html result.

  5. When the html is ok you can just setup a special action returning a custom ActionResult: ActionAsPdf. The code will look like this:
  6. public ActionResult PrintInvoice(int invoiceId)
    {
      return new ActionAsPdf(
                     "Invoice", 
                     new { invoiceId= invoiceId }) 
                     { FileName = "Invoice.pdf" };
    }

ActionAsPdf requires a string parameter with the name of the action to be converted to PDF. It can accept a parameter with other route data, in this case we are defining the invoiceId parameter. You can specify the name of the file being returned using the FIleName property.

To link to the PDF file in other views you just have to insert a regular link to the PrintInvoice action. Something like:

@Html.ActionLink(“PrintInvoice”)

if you’re working with razor views.

And voila, that’s all and it just works, even if the printed action is protected with forms authentication.

Source code is on GitHub https://github.com/webgio/Rotativa.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Software architect. At present working on C# development, with mainly Asp.net Ajax and MVC user inteface. Particularly interested in OOP, test driven, agile development.

Comments and Discussions

 
QuestionAll my pdf are empty PinmemberMember 1081627117-Sep-14 4:11 
QuestionGreat work PinmemberJon D25-Jul-14 0:38 
AnswerRe: Great work PinmemberGiorgio Bozio25-Jul-14 1:07 
GeneralRe: Great work PinmemberJon D25-Jul-14 6:41 
QuestionDifferent sizes on different PC PinmemberMember 1051563624-Jun-14 5:19 
AnswerRe: Different sizes on different PC PinmemberGiorgio Bozio24-Jun-14 5:24 
GeneralGreat Work Pinmemberkingve9-Jun-14 13:54 
GeneralMy vote of 5 Pinmembernagash6669-Apr-14 12:52 
QuestionGiorgio - HELP our PDFs are long multi - page PDFs and the bottoms/tops between pages are cutting off text PinmemberBryan Taylor3-Mar-14 6:22 
QuestionCan we print all pages of the MVC application? Pinmembercodeprojectsaf12-Nov-13 14:45 
GeneralMy vote of 5 PinmemberRitesh Man12-Aug-13 19:46 
GeneralBravo! Pinmemberpeter gabris21-Apr-13 19:03 
GeneralRe: Bravo! PinmemberGiorgio Bozio26-Apr-13 5:12 
QuestionDefault type problem Pinmemberfedericolaggiard19-Apr-13 1:07 
AnswerRe: Default type problem PinmemberGiorgio Bozio26-Apr-13 5:11 
QuestionReturn PDF only? PinmemberCodeDog Billy11-Apr-13 13:25 
AnswerRe: Return PDF only? PinmemberGiorgio Bozio12-Apr-13 0:11 
GeneralRe: Return PDF only? PinmemberCodeDog Billy12-Apr-13 6:45 
GeneralRe: Return PDF only? PinmemberCodeDog Billy12-Apr-13 7:12 
Questiongr8 solution to something i think the framework should have PinmemberUsha Pini3-Apr-13 1:56 
AnswerRe: gr8 solution to something i think the framework should have PinmemberGiorgio Bozio12-Apr-13 0:08 
GeneralMy vote of 5 PinmemberAnkhara9-Jan-13 11:33 
QuestionGreat Work! Pinmemberzyck24-Feb-12 16:51 
GeneralMy vote of 5 PinmemberWilliam John Adam Trindade24-Feb-12 8:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 24 Feb 2012
Article Copyright 2012 by Giorgio Bozio
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid