2014-10-28 57 views
5

我正在使用Rotativa在我的“MVC”应用程序中生成PDF。我怎样才能保存Rotativa PDF?在完成所有过程后,我需要将文档保存在服务器上。下面如何在服务器上保存Rotativa PDF

代码:

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

} 

回答

12

你可以试试这个

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

如果不这样做的伎俩的话,你可以按照答案here

只要确保如果你这样做没有PRVRequestPdf作为一个PDF视图返回,而是一个正常的视图,就像你上面(只提及为设法让我自己造成的大量犯规的乐趣)。

2

另一个有用的答案:

我找到了解决办法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); 

这是原来的thread

+0

该解决方案采用了丹尼尔的回答了。 – meJustAndrew 2016-08-08 18:58:25

相关问题