2014-10-28 31 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

相關問題