在 ASP.NET Core MVC 7 中使用 Rotativa 将 HTML 导出为 PDF 文件

问题描述 投票:0回答:1

我正在使用 ASP.NET Core MVC 7,我想使用 Rotativa 包将 HTML(即 Razor View)导出到 PDF 文件中。

我使用包管理器控制台安装所需的包:

Install-Package Rotativa.AspNetCore -Version 1.2.0

然后我使用以下操作代码:

public IActionResult SavePDF()
{
            return new ViewAsPdf("PDF")
            {
                FileName = "MyPdfFile.pdf"
            };
}

其中 PDF 是 Razor 视图名称,MyPdfFile.pdf 是 PDF 文件名 但是,我在运行时出现以下错误

System.ArgumentNullException: Value cannot be null. (Parameter 'path1')
   at System.ArgumentNullException.Throw(String paramName)
   at System.IO.Path.Combine(String path1, String path2)
   at Rotativa.AspNetCore.WkhtmlDriver.Convert(String wkhtmlPath, String switches, String html, String wkhtmlExe)
   at Rotativa.AspNetCore.WkhtmltopdfDriver.ConvertHtml(String wkhtmltopdfPath, String switches, String html)
   at Rotativa.AspNetCore.ViewAsPdf.CallTheDriver(ActionContext context)
   at Rotativa.AspNetCore.AsResultBase.BuildFile(ActionContext context)
   at Rotativa.AspNetCore.AsResultBase.ExecuteResultAsync(ActionContext context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResultFilterAsync>g__Awaited|30_0[TFilter,TFilterAsync](ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|25_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

有什么建议请多多指教,谢谢

在使用 ASP.NET Core MVC 时,我尝试使用 Rotativa 包 (Rotativa.AspNetCore) 从 Razor 视图 (HTML) 生成 PDF 文件,但它不适用于处理请求时发生未处理的异常。

asp.net-core pdf-generation rotativa
1个回答
0
投票

1.请务必在Program.cs中添加以下代码:

RotativaConfiguration.Setup(builder.Environment.WebRootPath, "Rotativa");

2.确保您的项目包含位于以下位置的

wkhtmltoimage.exe
wkhtmltopdf.exe
文件:

如果没有,您可以从github下载:https://github.com/webgio/Rotativa.AspNetCore。他们位于

Rotativa.AspNetCore/Rotativa.AspNetCore.Demo/wwwroot/Rotativa/
。然后将这两个文件放入您的项目中。

© www.soinside.com 2019 - 2024. All rights reserved.