Unable to download PDF file with Rotativa in MVC4

2019-07-23 04:04发布

I am using Rotativa to turn a Razor view into a PDF.

The PDF file is not downloading.

I can see it in Fiddler but the browser is not prompting for download - I have tried this with both IE and Chrome.

I also tried to download the file to a physical path using the solution in this question here. but that didn't work either because of the system couldn't access the folder (Access Denied).

Here is my code:

public ActionResult Index()
{
    var model = new CustomerDashboardVM();
    return View("Index", model);
}

public ActionResult print(int voucherID)
{
    var pdf =  new ActionAsPdf("Index", new { voucherID}) { FileName = "testInvoice.pdf", PageSize = Rotativa.Options.Size.A4};

  //  RotativaHelper.SaveHttpResponseAsFile("http://localhost:65425/BlankDashboard",  Server.MapPath("~\\PdfDownloads"));

    return pdf;

}

I wonder why is this happening - I click the button, it calls the print ActionResult method - no error messages (I wrapped this in a try and catch block). I have tried this on a colleague PC and it was the same issue!

Many thanks.

2条回答
戒情不戒烟
2楼-- · 2019-07-23 04:14

Okay, i got your mistake. You have not passed parameter name voucherID in Index method.

So now your code look like this:

public ActionResult Index(int voucherID)  // Here you have to pass parameter
{
    var model = new CustomerDashboardVM(voucherID);
    return View("Index", model);
}

and Print() method look like this -

public ActionResult Print(int voucherID)
{
  return new ActionAsPdf(
                 "Index", 
                 new { voucherID = voucherID }) 
                 { 
                    FileName = "testInvoice.pdf",
                    PageSize = Rotativa.Options.Size.A4
                 };
}
查看更多
Melony?
3楼-- · 2019-07-23 04:34

The issue was that I was calling the ActionResult print() method on a button click event through an Ajax post. This apparently doesn't work.

It should work if you replace the button with a link, which has the print() mehtod in its URL; i.e., just the way mvc links work..

查看更多
登录 后发表回答