DinkToPdf在Net Core中无法加载DLL文件

8
我将尝试使用DinkToPdf库从HTML SQL服务器数据库生成PDF文件。
在启动文件中,我已经添加了以下内容:
var context = new CustomAssemblyLoadContext();
context.LoadUnmanagedLibrary(Path.Combine(Directory.GetCurrentDirectory(), "libwkhtmltox.dll"));

在启动Web应用程序时,这行代码会出现以下错误:

DllNotFoundException:无法加载DLL'C:\Program Files\IIS Express\libwkhtmltox.dll'或其依赖项之一:指定的模块无法找到。(HRESULT异常:0x8007007E)

System.Runtime.Loader.AssemblyLoadContext.InternalLoadUnmanagedDllFromPath(string unmanagedDllPath)

DllNotFoundException:无法加载DLL'C:\Program Files\IIS Express\libwkhtmltox.dll'或其依赖项之一:指定的模块无法找到。(HRESULT异常:0x8007007E)


你是否可以直接使用NuGet导入dinktopdf,而不是直接作为未管理的加载? - keysl
你好,我已经导入了相关内容并且安装成功。在使用说明中,我必须从运行中导入dll文件。但是在Asp.Net Core 3.0上,这种方法Directory.GetCurrentDirectory()似乎无法正常工作。 - Joseph Wambura
Asp.Net Core 的 Directory.GetCurrentDirectory() 在这里可能是罪魁祸首。它将 C:\Program Files\IIS Express\ 作为项目的默认文件夹返回。 - Joseph Wambura
Farzaneh Talebi在Stack Overflow上发布了一个答案,表示"我遇到过同样的问题,这篇教程帮了我大忙:https://medium.com/volosoft/convert-html-and-export-to-pdf-using-dinktopdf-on-asp-net-boilerplate-e2354676b357"。 - Scratte
5个回答

12

如果有其他人遇到与我相同的问题,我通过安装Microsoft Visual C++ 2015 Redistributable解决了它。


1
在UAT环境中加载旧的32位dll时,我遇到了类似的问题。对于这个特定的dll,C++ 2010可再发行组件起了作用。 - nf313743
非常感谢...本地可以,但是生产环境 - 其他服务器不行 - 你救了我的一天。 - blogprogramisty.net
哇,安装 Microsoft Visual C++ 2015 Redistributable 对我有用,这是下载链接 https://www.microsoft.com/en-us/download/details.aspx?id=52685 - Luqman Cheema
@LuqmanCheema 很抱歉,此下载已不再可用。- 现在怎么办? - Cloud

10

在库的git存储库中提到,您应该下载二进制文件并将其包含在源代码中:

将原生库复制到项目的根文件夹。从那里.NET Core在使用P / Invoke调用本机方法时加载本机库。您可以在此处找到最新版本的本地库这里。为您的操作系统和平台(64位或32位)选择适当的库。

出错的原因是我去了这个url然后右键单击每个文件并选择save link as(chrome)。这导致下载一个破损的文件: enter image description here

不要这样做 您必须在github中打开每个文件,然后使用Download按钮。 正确的文件比您走错路得到的文件大得多!

enter image description here

荒谬可笑但问题可能是由此引起的...


1
我正在使用这个解决方案,现在它已经为我工作了。所以我只是手动下载了dll文件,即使我有libwkhtmltox.dylib和libwkhtmltox.so文件。 - Mr Alexander Ws

2
在Asp.Net Core应用程序中,我会像这样使用它来在运行时获取当前目录。
#if DEBUG
            //windows
            string filePath = $@"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}\libwkhtmltox.dll";
#else
            //linux
            string filePath = @$"{(($@"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}/libwkhtmltox.so").Replace(@"\", @"/"))}";
#endif
            CustomAssemblyLoadContext context = new CustomAssemblyLoadContext();
            context.LoadUnmanagedLibrary(filePath);
            serviceCollection.AddSingleton(typeof(IConverter), new SynchronizedConverter(new PdfTools()));
#endregion

2
我建议使用System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(),检查平台是否为Windows或Linux并设置适当的路径。这样调试就不会与Linux绑定了。 - M.Boukhlouf
还推荐这种方法 https://dev59.com/aVkT5IYBdhLWcg3wStzQ#38795621。适用于所有平台,效果很好。 - Tropin Alexey

1
如果您在Linux上使用asp.net core,则需要使用以下命令安装所需的软件包。
apt-get update \
&& apt-get install -y --no-install-recommends \
    zlib1g \
    fontconfig \
    libfreetype6 \
    libx11-6 \
    libxext6 \
    libxrender1 \
&& curl -o /usr/lib/libwkhtmltox.so \
    --location \
    https://github.com/rdvojmoc/DinkToPdf/raw/v1.0.8/v0.12.4/64%20bit/libwkhtmltox.so

这个命令需要使用 root 权限,我们需要在使用它之前调用 'sudo su' 命令。 - Zeshan
以下命令有助于解决DinkToPdf无法正确显示图像的问题:sudo apt-get install libgdiplus sudo apt-get install xvfb libfontconfig wkhtmltopdf sudo apt-get install libc6-dev sudo apt-get install openssl sudo apt-get install libssl1.0-dev - Zeshan

1
我找到了一些解决方法。它们不完美,但值得一试,它们确实有所帮助,我能够从SQL Server生成PDF文件。我将.dll文件放在以下文件夹中,然后它就可以工作了。

C:\Program Files\IIS Express

然后使用以下命令加载.dll文件:

Path.Combine(Directory.GetCurrentDirectory(), "libwkhtmltox.dll");

我选择了整个路径的另一种方式

context.LoadUnmanagedLibrary(Path.GetFullPath(@"C:\Users\User\source\repos\WebSolution\WebApp\libwkhtmltox.dll"));

两者都可以工作。然而,我建议 .Net Core 开发人员认真研究 GetCurrentDir,或者提供一种从项目或解决方案文件夹中加载的方法。

Path.Combine(Directory.GetCurrentDirectory(), "libwkhtmltox.dll");

2
它在本地工作,但是当我发布到Azure时出现了错误。 - Swaroop
1
Mac 上怎么样? - Idris Stack

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接