Rotativa.aspnetcore中的BuildFile错误

编程入门 行业动态 更新时间:2024-10-20 15:53:12
本文介绍了Rotativa.aspnetcore中的BuildFile错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在asp core 2.1应用程序中以旋转方式将视图转换为pdf时会出现错误

值不能为null.参数名称:path1

下面是我的代码

var rpt = new ViewAsPdf(); //rptLandscape.Model = Model; rpt.PageOrientation = Rotativa.AspNetCore.Options.Orientation.Landscape; rpt.PageSize = Rotativa.AspNetCore.Options.Size.A4; rpt.ViewName = "Test"; byte[] arr = await rpt.BuildFile(actionContextAccessor.ActionContext); System.IO.File.WriteAllBytes(Path.Combine(env.WebRootPath, "PDFStorage", "File.pdf"), arr);

尽管它成功地将网页返回为pdf,但我想将其存储在文件夹中. 此错误的可能原因是什么? ,我已经检查了全部,它甚至不包含名称为name1的属性

更新1:错误不在Path.Combine()中,该错误位于该行之前.

byte[] arr = await rpt.BuildFile(actionContextAccessor.ActionContext);

解决方案

简短版本

您需要在Startup.cs 中调用RotativaConfiguration.Setup(env);,然后下载并部署另一个工具来进行实际的转换工作.您可能应该找到其他库.

长版

没有实际的异常及其调用堆栈,只能猜测或检查源代码,然后尝试猜测可能出了什么问题.

BuildFile的源代码为:

public async Task<byte[]> BuildFile(ActionContext context) { if (context == null) throw new ArgumentNullException("context"); //if (this.WkhtmlPath == string.Empty) // this.WkhtmlPath = context.HttpContext.Server.MapPath("~/Rotativa"); this.WkhtmlPath = RotativaConfiguration.RotativaPath; var fileContent = await CallTheDriver(context); if (string.IsNullOrEmpty(this.SaveOnServerPath) == false) { File.WriteAllBytes(this.SaveOnServerPath, fileContent); } return fileContent; }

WriteAllBytes不能成为罪魁祸首.但是,它是确实从RotativaConfiguration.RotativaPath设置中设置了WkhtmlPath属性.在CallTheDriver()中的调用之后,表明该库仅通过一些开关调用可执行文件即可转换PDF文件.

实际调用执行exe,从ViewAsPdf.cs追溯到WkhtmlDriver.cs的是:

var proc = new Process { StartInfo = new ProcessStartInfo { FileName = Path.Combine(wkhtmlPath, wkhtmlExe), Arguments = switches, UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true, RedirectStandardInput = true, WorkingDirectory = wkhtmlPath, CreateNoWindow = true } }; proc.Start();

如果wkhtmlPath为null,则将得到null参数异常.所有这些调用将出现在异常的调用堆栈中.

解决方案是确保正确设置RotativaConfiguration.RotativaPath属性.回购本身对此进行了解释:

在Startup.cs中完成的基本配置:

RotativaConfiguration.Setup(env);

确保您有一个包含wkhtmltopdf.exe文件的文件夹,该文件可由运行Web应用程序的进程访问.默认情况下,它在Web应用程序的根目录中搜索名为"Rotativa"的文件夹.如果您需要更改此参数,请在安装程序中使用可选参数调用RotativaConfiguration.Setup(env,"path/relative/to/root")

顺便说一句,该库的作用是,在 Web应用程序中运行一个单独的可执行文件是一个非常非常糟糕的主意:

  • 可伸缩性丢失.为每个请求运行单独的可执行文件非常昂贵,并且很容易淹没繁忙的服务器.这就是为什么生产服务器不能以这种方式工作.如果该进程挂起,则请求移交.您可能会遇到孤立的进程.
  • 第二,它需要提升的权限-Web应用程序的帐户必须能够执行任意可执行文件,这应该不允许执行.
  • 最后,忘记跨平台部署.可执行文件名称为硬编码即使 wkhtmltopdf/网站提供了适用于所有操作系统的版本./p>

    BTW工具本身提供了一个C库,可在其他应用程序中使用

    While converting a view as pdf in asp core 2.1 app with rotative it gives an error

    Value cannot be null. Parameter name: path1

    Below is my Code

    var rpt = new ViewAsPdf(); //rptLandscape.Model = Model; rpt.PageOrientation = Rotativa.AspNetCore.Options.Orientation.Landscape; rpt.PageSize = Rotativa.AspNetCore.Options.Size.A4; rpt.ViewName = "Test"; byte[] arr = await rpt.BuildFile(actionContextAccessor.ActionContext); System.IO.File.WriteAllBytes(Path.Combine(env.WebRootPath, "PDFStorage", "File.pdf"), arr);

    Although it returns webpage as pdf successfully, but I want to store it inside a folder. What are the possible reasons for this Error? , I have checked all, it does not even contain property by the name name1

    Update 1: Error is not in Path.Combine(), the error is in line before it.

    byte[] arr = await rpt.BuildFile(actionContextAccessor.ActionContext);

    解决方案

    Short Version

    You need to call RotativaConfiguration.Setup(env); in Startup.cs and download and deploy another tool to do the actual conversion work. You should probably find a different library.

    Long version

    Without the actual exception and its call stack one can only guess, or check the source code and try to guess what could go wrong.

    The source code for BuildFile is :

    public async Task<byte[]> BuildFile(ActionContext context) { if (context == null) throw new ArgumentNullException("context"); //if (this.WkhtmlPath == string.Empty) // this.WkhtmlPath = context.HttpContext.Server.MapPath("~/Rotativa"); this.WkhtmlPath = RotativaConfiguration.RotativaPath; var fileContent = await CallTheDriver(context); if (string.IsNullOrEmpty(this.SaveOnServerPath) == false) { File.WriteAllBytes(this.SaveOnServerPath, fileContent); } return fileContent; }

    WriteAllBytes can't be the culprit. It does set the WkhtmlPath property from the RotativaConfiguration.RotativaPath setting though. Following the calls inside CallTheDriver() shows that this library just calls an executable with some switches to convert the PDF file.

    The actual call that executes the exe, traced from ViewAsPdf.cs to WkhtmlDriver.cs is :

    var proc = new Process { StartInfo = new ProcessStartInfo { FileName = Path.Combine(wkhtmlPath, wkhtmlExe), Arguments = switches, UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true, RedirectStandardInput = true, WorkingDirectory = wkhtmlPath, CreateNoWindow = true } }; proc.Start();

    If wkhtmlPath is null, you'll get a null argument exception. All those calls would appear in the exception's call stack.

    The solution is to ensure that the RotativaConfiguration.RotativaPath property is set correctly. The repo itself explains that :

    Basic configuration done in Startup.cs:

    RotativaConfiguration.Setup(env);

    Make sure you have a folder with the wkhtmltopdf.exe file accessible by the process running the web app. By default it searches in a folder named "Rotativa" in the root of the web app. If you need to change that use the optional parameter to the Setup call RotativaConfiguration.Setup(env, "path/relative/to/root")

    BTW what that library does, run a separate executable in a web application is a very, very bad idea:

  • Scalability is lost. Running a separate executable for each request is very expensive and can easily flood a busy server. That's why production servers don't work this way. If the process hangs, the request hands. You can end up with orphaned processes.
  • Second, it requires elevated permissions - the web app's account has to be able to execute arbitrary executables, something it should not be allowed to do.
  • Finally, forget about cross-platform deployment. The executable name is hard-coded to "wkhtmltopdf.exe", even though the wkhtmltopdf/ site provides versions for all OSs.

    BTW the tool itself provides a C library for use in other applications

    更多推荐

    Rotativa.aspnetcore中的BuildFile错误

    本文发布于:2023-11-17 04:48:36,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1608885.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:错误   Rotativa   aspnetcore   BuildFile

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!