1

I'm using Rotativa to generate a PDF file from a view, which works well, but now on the browser I get the raw file thrown at the console, no download dialog box, no warning, nothing. Here's my code:

Controller

public ActionResult DescargarPDF (int itemId) {
        var presupuesto = ReglasNegocio.Fachada.Consultas.ObtenerPresupuesto(itemId);     
        return new Rotativa.PartialViewAsPdf("_PresupuestoFinal", presupuesto) {
            FileName = "Presupuesto_" + itemId + ".pdf",
            PageSize = Rotativa.Options.Size.A4
        };
    }

JQuery script:

$(".convertirPDF").on("click", function (id) {
    var itemId = $(this).data('itemid');
    Pdf(itemId);
});

function Pdf(itemid) {
    var id = itemid;

    $.ajax({
        method: "POST",
        url: 'DescargarPDF',
        data: { itemId: id },
        cache: false,
        async: true,
    });
};

Button on the HTML

<button class="convertirPDF btn btn-secondary btn-info" data-itemid="@item.Id">PDF</button>

I've tried several codes on the controller (with same result) since the script and view seems to work fine. However, I'm suspecting, maybe the html or the script need some tuning to inform the browser it has to download the file?

Thanks everyone in advance.

dCarMal
  • 151
  • 7
  • 1
    Return a `FileResult` from the action? Or use [`ActionAsPdf`](https://stackoverflow.com/questions/29268449/mvc-action-not-being-called-from-rotativa-partialviewaspdf#29270794)? – stuartd Aug 31 '18 at 10:43
  • @stuartd Thanks for your suggestion, that's one of the things I've tried on the controller, `public FileResult DescargarPDF (int itemId) { var presupuesto = ReglasNegocio.Fachada.Consultas.ObtenerPresupuesto(itemId); var archivo = new Rotativa.PartialViewAsPdf("_PresupuestoFinal", presupuesto) { FileName = "Presupuesto_" + itemId + ".pdf" }; var binario = archivo.BuildFile(this.ControllerContext); return File(binario.ToArray(), "application/pdf");` also tried applying a filter to the controller method, both give the same result I got now. Thanks again. – dCarMal Aug 31 '18 at 10:46
  • 1
    In your controller, have you added a `System.Net.Mine.ContentDisposition` with `Inline = false`? – freedomn-m Aug 31 '18 at 10:55
  • @freedomn-m No, I haven't tried that. Going to search how and try. Thank you. – dCarMal Aug 31 '18 at 11:00
  • 1
    Ah, sorry, just noticed that your getting the PDF data via ajax. Easiest is to open it via an anchor `` with `target="_blank"` – freedomn-m Aug 31 '18 at 11:00
  • This *might* help: https://stackoverflow.com/a/50408867/2181514 – freedomn-m Aug 31 '18 at 11:00
  • 1
    Try this: `var contentDisposition = new System.Net.Mine.ContentDisposition { Inline = false }; response.AppendHeader("Content-Disposition", contentDisposition.ToString());` - not sure if it will help with ajax. It *suggests* to the browser that the download should not be displayed in a browser window. So probably not what you want here. – freedomn-m Aug 31 '18 at 11:02
  • @freedomn-m Thank you very much, right now I'm trying this [link] (https://stackoverflow.com/questions/23239557/unable-to-download-pdf-file-with-rotativa-in-mvc4?rq=1) since it's very similar to my case, but if it doesn't work, I'll be looking into that. Thank you again. – dCarMal Aug 31 '18 at 11:04
  • @freedomn-m I'm trying your code, but this `System.Net.Mine` doesn't exist in my context :-/ – dCarMal Aug 31 '18 at 11:11
  • 1
    Typo: System.Net.Mime – freedomn-m Aug 31 '18 at 11:15
  • @freedomn-m Just tried it, same result. I think it has something to do with the fact that I'm making the request through ajax. Thanks. – dCarMal Aug 31 '18 at 11:26

1 Answers1

0

I found a solution. It's not elegant, but it works. So I didn't need to use ajax necessarily to make the request, neither to give function to the button. I'm kind of sure that the issue has something to do with JS and/or jQuery. Nevertheless, there's a simpler way to do this.

I changed my html button to:

<a href="DescargarPDF/?itemId=@item.Id" target="_blank" class="btn btn-secondary btn-info">PDF</a>

so it looks like a button but it's really a link to my controller¡s method. I also removed the script for that button and now it downloads the file. Not with the name intended, but still.

Thanks to everyone. Happy coding.

UPDATE

I've been working on the same project, and I think I found out why my PDF file was being thrown into console.

The thing is, jQuery makes the request, so jQuery manages the response. Is that simple. If you check official docs for .post(), you'll see the following:

The success callback function is passed the returned data, which will be an XML root element or a text string depending on the MIME type of the response. It is also passed the text status of the response.

As of jQuery 1.5, the success callback function is also passed a "jqXHR" object (in jQuery 1.4, it was passed the XMLHttpRequest object).

Most implementations will specify a success handler.

And I wasn't, so, by default, it just drop it to console. I hope this throws some light into the issue and helps. Happy coding.

Community
  • 1
  • 1
dCarMal
  • 151
  • 7