How to save Rotativa PDF on server

user3778485 picture user3778485 · Oct 28, 2014 · Viewed 19.6k times · Source

I am using Rotativa to generate PDF in my "MVC" application. How can I save Rotativa PDF? I need to save the document on a server after all the process is completed.

Code below:

public ActionResult PRVRequestPdf(string refnum,string emid)
{
    var prv = functions.getprvrequest(refnum, emid);            
    return View(prv);

}
public ActionResult PDFPRVRequest()
{
    var prv = Session["PRV"] as PRVRequestModel;
    byte[] pdfByteArray = Rotativa.WkhtmltopdfDriver.ConvertHtml("Rotativa", "Approver", "PRVRequestPdf");
    return new Rotativa.ViewAsPdf("PRVRequestPdf", new { refnum = prv.rheader.request.Referenceno });            

} 

Answer

Daniel Kelly picture Daniel Kelly · Feb 5, 2015

You can give this a try

var actionResult = new ActionAsPdf("PRVRequestPdf", new { refnum = prv.rheader.request.Referenceno, emid = "Whatever this is" });
var byteArray = actionResult.BuildPdf(ControllerContext);
var fileStream = new FileStream(fullPath, FileMode.Create, FileAccess.Write);
fileStream.Write(byteArray, 0, byteArray.Length);
fileStream.Close();

If that doesn't do the trick then, you can follow the answers here

Just make sure if you do it this way not to have PRVRequestPdf return as a PDF View, rather a normal View like you have above (only mention as managed to fall foul of that myself causing lots of fun).