Click here to Skip to main content
13,045,564 members (73,248 online)
Click here to Skip to main content
Add your own
alternative version

Stats

95.2K views
30 bookmarked
Posted 24 Feb 2012

Rotativa, How to Print PDF in ASP.NET MVC

, 9 Feb 2017
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 design part was OK but what really bothered me were 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:

Install-Package Rotativa
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) than the other views of the web app (or you’ll define a separate 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.

public ActionResult PrintInvoice(int invoiceId)
{
  return new ActionAsPdf(
                 "Invoice", 
                 new { invoiceId= invoiceId }) 
                 { FileName = "Invoice.pdf" };
}
  1. Install using the Nuget package manager (search for Rotativa) or, more conveniently, with the package manager console, typing:
  2. 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:
  3. When the HTML is OK, you can just setup a special action returning a custom ActionResult: ActionAsPdf. The code will look like this:

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 at 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.

You may also be interested in...

Comments and Discussions

 
Questionin my case ViewAsPdf gettiing error Pin
thirupathiCSt18-Apr-17 1:39
memberthirupathiCSt18-Apr-17 1:39 
QuestionAzure basic web app using Rotativa? Pin
Member 933782211-Mar-17 8:41
memberMember 933782211-Mar-17 8:41 
AnswerRe: Azure basic web app using Rotativa? Pin
Giorgio Bozio14-Mar-17 5:07
memberGiorgio Bozio14-Mar-17 5:07 
GeneralRe: Azure basic web app using Rotativa? Pin
Member 933782214-Mar-17 6:25
memberMember 933782214-Mar-17 6:25 
GeneralRe: Azure basic web app using Rotativa? Pin
Giorgio Bozio14-Mar-17 6:32
memberGiorgio Bozio14-Mar-17 6:32 
GeneralMy vote of 5 Pin
Member 787034513-Feb-17 23:25
professionalMember 787034513-Feb-17 23:25 
QuestionRotativa with Azure gives issues Pin
Cheyne Lothering13-Feb-17 23:21
memberCheyne Lothering13-Feb-17 23:21 
AnswerRe: Rotativa with Azure gives issues Pin
Giorgio Bozio14-Mar-17 5:10
memberGiorgio Bozio14-Mar-17 5:10 
QuestionList to PDF Pin
Member 1272281723-Dec-16 21:12
memberMember 1272281723-Dec-16 21:12 
QuestionADFS authenticated app causes the login screen to be printed Pin
planetregin4-May-15 7:45
memberplanetregin4-May-15 7:45 
QuestionBuildPDF without ControllerContext Pin
jstring20-Jan-15 9:01
memberjstring20-Jan-15 9:01 
QuestionAll my pdf are empty Pin
Member 1081627117-Sep-14 3:11
memberMember 1081627117-Sep-14 3:11 
QuestionGreat work Pin
Jon D24-Jul-14 23:38
memberJon D24-Jul-14 23:38 
AnswerRe: Great work Pin
Giorgio Bozio25-Jul-14 0:07
memberGiorgio Bozio25-Jul-14 0:07 
GeneralRe: Great work Pin
Jon D25-Jul-14 5:41
memberJon D25-Jul-14 5:41 
QuestionDifferent sizes on different PC Pin
Member 1051563624-Jun-14 4:19
memberMember 1051563624-Jun-14 4:19 
AnswerRe: Different sizes on different PC Pin
Giorgio Bozio24-Jun-14 4:24
memberGiorgio Bozio24-Jun-14 4:24 
GeneralRe: Different sizes on different PC Pin
KrakTehPlanet2-Mar-16 8:36
memberKrakTehPlanet2-Mar-16 8:36 
GeneralGreat Work Pin
kingve9-Jun-14 12:54
memberkingve9-Jun-14 12:54 
GeneralMy vote of 5 Pin
nagash6669-Apr-14 11:52
membernagash6669-Apr-14 11:52 
QuestionGiorgio - HELP our PDFs are long multi - page PDFs and the bottoms/tops between pages are cutting off text Pin
Bryan Taylor3-Mar-14 5:22
memberBryan Taylor3-Mar-14 5:22 
QuestionCan we print all pages of the MVC application? Pin
codeprojectsaf12-Nov-13 13:45
membercodeprojectsaf12-Nov-13 13:45 
GeneralMy vote of 5 Pin
Ritesh Man12-Aug-13 18:46
memberRitesh Man12-Aug-13 18:46 
GeneralBravo! Pin
peter gabris21-Apr-13 18:03
memberpeter gabris21-Apr-13 18:03 
GeneralRe: Bravo! Pin
Giorgio Bozio26-Apr-13 4:12
memberGiorgio Bozio26-Apr-13 4:12 
QuestionDefault type problem Pin
federicolaggiard19-Apr-13 0:07
memberfedericolaggiard19-Apr-13 0:07 
AnswerRe: Default type problem Pin
Giorgio Bozio26-Apr-13 4:11
memberGiorgio Bozio26-Apr-13 4:11 
QuestionReturn PDF only? Pin
CodeDog Billy11-Apr-13 12:25
memberCodeDog Billy11-Apr-13 12:25 
AnswerRe: Return PDF only? Pin
Giorgio Bozio11-Apr-13 23:11
memberGiorgio Bozio11-Apr-13 23:11 
GeneralRe: Return PDF only? Pin
CodeDog Billy12-Apr-13 5:45
memberCodeDog Billy12-Apr-13 5:45 
GeneralRe: Return PDF only? Pin
CodeDog Billy12-Apr-13 6:12
memberCodeDog Billy12-Apr-13 6:12 
Questiongr8 solution to something i think the framework should have Pin
Usha Pini3-Apr-13 0:56
memberUsha Pini3-Apr-13 0:56 
AnswerRe: gr8 solution to something i think the framework should have Pin
Giorgio Bozio11-Apr-13 23:08
memberGiorgio Bozio11-Apr-13 23:08 
GeneralMy vote of 5 Pin
Ankhara9-Jan-13 10:33
memberAnkhara9-Jan-13 10:33 
QuestionGreat Work! Pin
zyck24-Feb-12 15:51
memberzyck24-Feb-12 15:51 
GeneralMy vote of 5 Pin
William John Adam Trindade24-Feb-12 7:21
memberWilliam John Adam Trindade24-Feb-12 7:21 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 9 Feb 2017
Article Copyright 2012 by Giorgio Bozio
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid