10

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 });            

} 
Ibrahim Amer
  • 1,147
  • 4
  • 19
  • 46
user3778485
  • 101
  • 1
  • 1
  • 4

4 Answers4

20

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

Community
  • 1
  • 1
Daniel Kelly
  • 201
  • 2
  • 3
11

Another useful answer:

I found the solution here

            var actionPDF = new Rotativa.ActionAsPdf("YOUR_ACTION_Method", new { id = ID, lang = strLang } //some route values)
            {
                //FileName = "TestView.pdf",
                PageSize = Size.A4,
                PageOrientation = Rotativa.Options.Orientation.Landscape,
                PageMargins = { Left = 1, Right = 1 }
            };
            byte[] applicationPDFData = actionPDF.BuildPdf(ControllerContext);

This is the original thread

Community
  • 1
  • 1
Ibrahim Amer
  • 1,147
  • 4
  • 19
  • 46
3

You can achieve this with ViewAsPdf.

[HttpGet]
public ActionResult SaveAsPdf(string refnum, string emid)
{
    try
    {
        var prv = functions.getprvrequest(refnum, emid);
        ViewAsPdf pdf = new Rotativa.ViewAsPdf("PRVRequestPdf", prv)
        {
            FileName = "Test.pdf",
            CustomSwitches = "--page-offset 0 --footer-center [page] --footer-font-size 8"
        };
        byte[] pdfData = pdf.BuildFile(ControllerContext);
        string fullPath = @"\\server\network\path\pdfs\" + pdf.FileName;
        using (var fileStream = new FileStream(fullPath, FileMode.Create, FileAccess.Write))
        {
            fileStream.Write(pdfData, 0, pdfData.Length);
        }
        return Json(new { isSuccessful = true }, JsonRequestBehavior.AllowGet);
    }
    catch (Exception ex)
    {
        //TODO: ADD LOGGING
        return Json(new { isSuccessful = false, error  = "Uh oh!" }, JsonRequestBehavior.AllowGet);
        //throw;
    }
}
Cryptc
  • 2,959
  • 1
  • 18
  • 18
0

You can simply try this:

var fileName = string.Format("my_file_{0}.pdf", id);
var path = Server.MapPath("~/App_Data/" + fileName);
System.IO.File.WriteAllBytes(path, pdfByteArray );
Tasnim Fabiha
  • 1,148
  • 1
  • 17
  • 31