在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应用程序中运行一个单独的可执行文件是一个非常非常糟糕的主意:
最后,忘记跨平台部署.可执行文件名称为硬编码即使 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:
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错误
发布评论