首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在服务器上保存Rotativa PDF

如何在服务器上保存Rotativa PDF
EN

Stack Overflow用户
提问于 2014-10-28 20:45:28
回答 4查看 20.6K关注 0票数 10

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

代码如下:

代码语言:javascript
复制
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 });            

} 
EN

回答 4

Stack Overflow用户

发布于 2015-02-05 23:22:09

你可以试一试

代码语言:javascript
复制
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视图,而不是一个正常的视图,就像你上面(只提到自己设法陷入冲突,造成了很多乐趣)。

票数 19
EN

Stack Overflow用户

发布于 2015-08-25 22:08:38

另一个有用的答案是:

我找到了解决方案here

代码语言:javascript
复制
            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

票数 11
EN

Stack Overflow用户

发布于 2020-01-31 02:58:03

您可以使用ViewAsPdf来实现这一点。

代码语言:javascript
复制
[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;
    }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26608729

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档