Rotativa.io WebApi support

date Mar 8, 2016

Rotativa.io supports WebApi usage and in general usage outside of Asp.net MVC web app. As we will see later this feature can be used to build the PDF from Asp.net MVC too, in case you need to access directly either the pdf or the URL pointing to the PDF (stored on the cloud and accessible for 2 minutes).

Returning PDF from WebApi is as simple as just doing:

public HttpResponseMessage Get()
{
    var mymodel = Db.SomeCallToGetModel();
    return Request.CreatePdfResponse(
        "~/Views/Home/Simple.cshtml", 
        model: mymodel, filename: "simple.pdf");
}

You need to also add the using directive using RotativaHQ.MVC5;.

Instead if you need to access the pdf binary directly you can simply call:

var pdf = PdfHelper.GetPdf(
    "~/Views/Home/Simple.cshtml", model: mymodel);

Finally you can just get the URL of the temporary file in the Rotativa.io cloud storage, certainly the most efficient option.

var pdf = PdfHelper.GetPdfUrl(
    "~/Views/Home/Simple.cshtml", 
    model: mymodel, filename: "simple.pdf");