简体   繁体   中英

System.DllNotFoundException: 'Unable to load DLL 'libwkhtmltox': The specified module could not be found. (Exception from HRESULT: 0x8007007E)'

Getting error when trying to generate PDF from HTML template. I have a different class library for generating PDF and I'm calling that from another controller.

System.DllNotFoundException: 'Unable to load DLL 'libwkhtmltox': The specified module could not be found. (Exception from HRESULT: 0x8007007E)'

您的 csproj 文件强制将 NuGet 程序集复制到构建的输出:

<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>

The files should at the root of the project and another thing that you know you should installed Microsoft Visual C++ Redistributable. follow this link

If you are running application in docker,then do an update to install libwkhtmltox dependencies for .netcore

# Install libwkhtmltox dependencies
RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        zlib1g \
        fontconfig \
        libfreetype6 \
        libx11-6 \
        libxext6 \
        libxrender1 \
        libjpeg62-turbo

Download package from https://github.com/rdvojmoc/DinkToPdf/tree/master/v0.12.4

Add it to your project reference by custom Assembly reference & register in your startup project. This is custom assembly load context which can load library from absolute path.

 internal class CustomAssemblyLoadContext : AssemblyLoadContext { public IntPtr LoadUnmanagedLibrary(string absolutePath) { return LoadUnmanagedDll(absolutePath); } protected override IntPtr LoadUnmanagedDll(String unmanagedDllName) { return LoadUnmanagedDllFromPath(unmanagedDllName); } protected override Assembly Load(AssemblyName assemblyName) { throw new NotImplementedException(); } }

In Statup.cs add below code. Call CustomAssemblyLoadContext before you create your converter:

CustomAssemblyLoadContext context = new CustomAssemblyLoadContext(); context.LoadUnmanagedLibrary(path);

var converter = new SynchronizedConverter(new PdfTools()); services.AddSingleton(converter); services.AddSingleton(typeof(IConverter), new SynchronizedConverter(new PdfTools()));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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