简体   繁体   English

NetCore HTML 转 PDF 库在 linux 环境中失败

[英]NetCore HTML to PDF library fails in linux environment

Currently I'm using SelectPdf to generate based on some html a pdf file.目前我正在使用SelectPdf基于一些 html 生成一个 pdf 文件。 The library works just fine in a windows environment, but it fails when I'm running it inside a linux container.该库在 Windows 环境中工作得很好,但是当我在 linux 容器中运行它时它会失败。

This is my dockerfile:这是我的 dockerfile:

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /source_code
COPY . .    
RUN dotnet publish --configuration Release --output dist    

FROM mcr.microsoft.com/dotnet/aspnet:6.0     
WORKDIR /app    
COPY --from=build /source_code/dist .    
EXPOSE 80
EXPOSE 8080
EXPOSE 433    
ENTRYPOINT ["dotnet", "Api.dll"]

Which builds just fine, and it works just fine, that is untill I'm using some the SelectPdf time at which I'm getting all kind of exceptions哪个构建得很好,而且工作得很好,直到我使用了一些 SelectPdf 时间,在这个时间我得到了所有类型的异常

invoice-api |发票-api |

{"EventId":13,"LogLevel":"Error","Category":"Microsoft.AspNetCore.Server.Kestrel","Message":"Connection id "0HMEDQ3A2F2L8", Request id "0HMEDQ3A2F2L8:00000002": An unhandled exception was thrown by the application.","Exception":"System.TypeInitializationException: The type initializer for \'Gdip\' threw an exception. ---\> System.DllNotFoundException: Unable to load shared library \'libgdiplus\' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: liblibgdiplus: cannot open shared object file: No such file or directory at System.Drawing.SafeNativeMethods.Gdip.GdiplusStartup(IntPtr\& token, StartupInput\& input, StartupOutput\& output) at System.Drawing.SafeNativeMethods.Gdip..cctor() --- End of inner exception stack trace --- at System.Drawing.SafeNativeMethods.Gdip.GdipNewPrivateFontCollection(IntPtr\& fontCollection) at System.Drawing.Text.PrivateFontCollecti {"EventId":13,"LogLevel":"Error","Category":"Microsoft.AspNetCore.Server.Kestrel","Message":"Connection id "0HMEDQ3A2F2L8", Request id "0HMEDQ3A2F2L8:00220000应用程序抛出未处理的异常。","Exception":"System.TypeInitializationException: \'Gdip\' 的类型初始值设定项抛出异常。---\> System.DllNotFoundException: 无法加载共享库 \'libgdiplus\'或其依赖项之一。为了帮助诊断加载问题,请考虑设置 LD_DEBUG 环境变量:liblibgdiplus:无法打开共享对象文件:System.Drawing.SafeNativeMethods.Gdip.GdiplusStartup(IntPtr\& token, StartupInput\& 输入,StartupOutput\& 输出)在 System.Drawing.SafeNativeMethods.Gdip..cctor() --- 内部异常堆栈跟踪结束 --- 在 System.Drawing.SafeNativeMethods.Gdip.GdipNewPrivateFontCollection(IntPtr\& fontCollection ) 在 System.Drawing.Text.PrivateFontCollecti on..ctor() at SelectPdf.Lib.\ᡟ..ctor() at SelectPdf.Lib.\១..ctor() at SelectPdf.Lib.\១..ctor(\ᡒ A_0, \ᠠ A_1) at SelectPdf.HtmlToPdf.\ᜂ(String A_0, String A_1, String A_2, String A_3, Boolean A_4) at SelectPdf.HtmlToPdf.ConvertHtmlString(String htmlString) at Api.Controllers.InvoicesController.Post(Int64 id, InvoiceRequestModel model) in /source_code/Api/Controllers/InvoicesController.cs:line 80 at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.\ g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask\`1 actionResultValueTask) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.\ g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Sco on..ctor() 在 SelectPdf.Lib.\ᡟ..ctor() 在 SelectPdf.Lib.\១..ctor() 在 SelectPdf.Lib.\១..ctor(\ᡒ A_0, \ᠠ A_1) 在SelectPdf.HtmlToPdf.\ᜂ(String A_0, String A_1, String A_2, String A_3, Boolean A_4) at SelectPdf.HtmlToPdf.ConvertHtmlString(String htmlString) at Api.Controllers.InvoicesController.Post(Int64 id, InvoicesourceRequestModel model) /Api/Controllers/InvoicesController.cs:Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) 的第 80 行,Microsoft.AspNetCore.Mvc.Invokerstructure.ControllerInaction .\ g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask\`1 actionResultValueTask) 在 Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.\ pe scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State\& next, Scope\& scope, Object\& state, Boolean\& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.\ g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.\ 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\& isComplete pe scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State\& next, Scope\& scope, Object\\ u0026 state, Boolean\& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.\ g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object State, at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker, .Infrastructure.ResourceInvoker.\ g__Awaited|25_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) 在 Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrowed(ResourceNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrowedcontext)atMicrosoft. .Mvc.Infrastructure.ResourceInvoker.Next(State\& next, Scope\& scope, Object\& state, Boolean\& isComplete d) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.\ g__Awaited|20_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.\ g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.\ g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope) at Microsoft.AspNetCore.Routing.EndpointMiddleware.\ g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger) at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication\`1 application)","State":{"Message":"Connection id "0HMEDQ3A2F2L8", Request id "0HMEDQ3A2F2L8:00000002": d) 在 Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.\ g__Awaited|20_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Invoker.Infrastructure u003CInvokeAsync\>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.\ g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.\ g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.\ g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope) .\ g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger) at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol[ProcessRequests] TContext](IHttpApplication\`1 application)","State":{"Message":"Connection id "0HMEDQ3A2F2L8", Request id "0HMEDQ3A2F2L8:00000002": An unhandled exception was thrown by the application.","ConnectionId":"0HMEDQ3A2F2L8","TraceIdentifier":"0HMEDQ3A2F2L8:00000002","{OriginalFormat}":"Connection id "{ConnectionId}", Request id "{TraceIdentifier}": An unhandled exception was thrown by the application."}} 应用程序抛出未处理的异常。","ConnectionId":"0HMEDQ3A2F2L8","TraceIdentifier":"0HMEDQ3A2F2L8:00000002","{OriginalFormat}":"Connection id "{ConnectionId}", Request 2 id \ {TraceIdentifier}":应用程序抛出了一个未处理的异常。"}}

I would appreciate some help with this.我将不胜感激。 Or any other suggestions of a library that can be run inside a linux container without having to do any aditional setup in the dockerfile or the host running the container.或者可以在 linux 容器内运行的库的任何其他建议,而无需在 dockerfile 或运行容器的主机中进行任何额外的设置。

I don't care about the performance, but only about just being able to plug in the library and just work.我不关心性能,而只关心能够插入库并正常工作。

From the SelectPdf website:SelectPdf网站:

SelectPdf only works on Windows SelectPdf 仅适用于 Windows

You can try using C# wrappers for wkhtmltopdf (at least some of them can require you to install the lib itself in container manually), iTextSharp or it's LGPL port .您可以尝试将 C# 包装器用于wkhtmltopdf (至少其中一些可能需要您手动在容器中安装 lib 本身)、 iTextSharp或其 LGPL 端口

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM