1

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.

Community
  • 1
  • 1
t_plusplus
  • 4,079
  • 5
  • 45
  • 60
  • Instead of **ActionAsPdf**,you can use **ViewAsPdf**. So your code look like this - return new ViewAsPdf("Index.cshtml", new { voucherID }) { FileName = "testInvoice.pdf", CustomSwitches = "--print-media-type --header-center \"text\"" };. Sorry for the formatting issue. – Krishnraj Rana Apr 23 '14 at 09:55
  • @KrishnrajRana thanks mate. This didn't work; it asked for the viewmodel of the view (Index.cshtml). I actually want it to display the result of an action so I wonder why this wont work with ActionAsPdf - it works on other projects! – t_plusplus Apr 23 '14 at 13:57
  • Okay, tell me your **CustomerDashboardVM()** returns what ?? Is it return generic list ??? I m talking abt this line - **var model = new CustomerDashboardVM();** in Index() method – Krishnraj Rana Apr 23 '14 at 15:03
  • @KrishnrajRana this is actually the viewModel of the page. but the problem is solve now as shown below. many thanks. – t_plusplus Apr 23 '14 at 15:12

2 Answers2

1

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

t_plusplus
  • 4,079
  • 5
  • 45
  • 60
1

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
                 };
}
Krishnraj Rana
  • 6,516
  • 2
  • 29
  • 36