【问题标题】:System.DllNotFoundException: 'Unable to load DLL 'libwkhtmltox': The specified module could not be found. (Exception from HRESULT: 0x8007007E)'System.DllNotFoundException:'无法加载 DLL'libwkhtmltox':找不到指定的模块。 (来自 HRESULT 的异常:0x8007007E)'
【发布时间】:2019-03-08 08:00:33
【问题描述】:

尝试从 HTML 模板生成 PDF 时出错。我有一个不同的类库来生成 PDF,我从另一个控制器调用它。

System.DllNotFoundException: '无法加载 DLL 'libwkhtmltox': 找不到指定的模块。 (HRESULT 的例外情况: 0x8007007E)'

【问题讨论】:

标签: c# api asp.net-core pdf-generation


【解决方案1】:

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

<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>

【讨论】:

    【解决方案2】:

    这些文件应该位于项目的根目录中,并且您知道应该安装 Microsoft Visual C++ Redistributable。 follow this link

    【讨论】:

      【解决方案3】:

      如果您在 docker 中运行应用程序,则进行更新以安装 .netcore 的 libwkhtmltox 依赖项

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

      【讨论】:

        【解决方案4】:

        https://github.com/rdvojmoc/DinkToPdf/tree/master/v0.12.4下载包

        通过自定义程序集引用将其添加到您的项目引用中并在您的启动项目中注册。 这是自定义程序集加载上下文,可以从绝对路径加载库。

        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();
            }
        }

        在 Statup.cs 中添加以下代码。 在创建转换器之前调用 CustomAssemblyLoadContext:

        CustomAssemblyLoadContext 上下文 = new CustomAssemblyLoadContext(); context.LoadUnmanagedLibrary(path);

        var 转换器 = new SynchronizedConverter(new PdfTools()); services.AddSingleton(转换器); services.AddSingleton(typeof(IConverter), new SynchronizedConverter(new PdfTools()));

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-02-18
          • 1970-01-01
          相关资源
          最近更新 更多