我正在使用DinkToPDF库将html字符串转换为等效的PDF。 要使用此库,我们必须导入提供的本地库libwkhtmltox.dll。当我在本地运行.net核心项目时,此方法工作正常,但是当我尝试在Azure中将Web项目发布为应用程序服务时,出现以下错误,
未处理的异常:System.DllNotFoundException:无法加载共享库'/home/site/wwwroot/libwkhtmltox.dll'或其依赖项之一。为了帮助诊断加载问题,请考虑设置LD_DEBUG环境变量:/home/site/wwwroot/libwkhtmltox.dll:无法打开共享对象文件:没有这样的文件或目录
我在startup.cs文件中引用了库的用法,如下所示。
// Renaming not supported if cgo files are affected.
var generatedFileNames []string
for _, info := range r.packages {
for _, f := range info.Files {
tokenFile := r.iprog.Fset.File(f.Pos())
if filesToUpdate[tokenFile] && generated(f, tokenFile) {
generatedFileNames = append(generatedFileNames, tokenFile.Name())
}
}
}
if !Force && len(generatedFileNames) > 0 {
return fmt.Errorf("refusing to modify generated file%s containing DO NOT EDIT marker: %v", plural(len(generatedFileNames)), generatedFileNames)
}
请帮助我找出此错误的解决方案。
答案 0 :(得分:1)
您必须将dll扩展名复制到项目的根目录
之后,您必须创建一个内部类来加载dll
将此添加到您的班级
using System.Runtime.Loader;
using System.Reflection;
要包括的方法:
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();
}
}
答案 1 :(得分:1)
在 dockerfile 中插入以下命令到 componente 引擎 html to pdf:
FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
RUN apt-get update
RUN apt-get install -y apt-utils
RUN apt-get install -y libgdiplus
RUN apt-get install -y libc6-dev
RUN ln -s /usr/lib/libgdiplus.so/usr/lib/gdiplus.dll
答案 2 :(得分:0)
您需要设置库的绝对路径,而对于Windows,则不必使用“ .dll”,在我的情况下,它位于[project_root]/wkhtmltox/v0.12.4/[x_64|x_86]
var architectureFolder = (IntPtr.Size == 8) ? "x_64" : "x_86";
var wkHtmlToPdfFileName = "libwkhtmltox";
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
wkHtmlToPdfFileName += ".so";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
wkHtmlToPdfFileName += ".dylib";
}
var wkHtmlToPdfPath = Path.Combine(
new string[] {
_hostingEnvironment.ContentRootPath,
"wkhtmltox",
"v0.12.4",
architectureFolder,
wkHtmlToPdfFileName
});
CustomAssemblyLoadContext context = new CustomAssemblyLoadContext();
context.LoadUnmanagedLibrary(wkHtmlToPdfPath);
services.AddSingleton(typeof(IConverter), new SynchronizedConverter(new PdfTools()));