Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save Rotativa PDF on server

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

} 
like image 332
user3778485 Avatar asked Oct 28 '14 12:10

user3778485


3 Answers

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;
    }
}
like image 68
Cryptc Avatar answered Nov 16 '22 01:11

Cryptc


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

like image 24
Daniel Kelly Avatar answered Nov 16 '22 02:11

Daniel Kelly


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

like image 11
Ibrahim Amer Avatar answered Nov 16 '22 02:11

Ibrahim Amer