我有一个使用 .net Core 2.2 的 Azure functionapp,它是为将 html 文本转换为 pdf 而编写的。我正在使用 DinkToPdf。当我运行该函数时,我得到“无法加载 libwkhtmltox.dll。我已经尝试了一些帖子中提到的替代解决方案,但它仍然抛出相同的错误。
我尝试使用 Directory.GetCurrentDirectory 并使用 path.combine
代码如下:
[FunctionName("Function1")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
CustomAssemblyLoadContext context = new CustomAssemblyLoadContext();
var architectureFolder = (IntPtr.Size == 8) ? "64 bit" : "32 bit";
context.LoadUnmanagedLibrary($@"{Directory.GetCurrentDirectory()}\Dependencies\libwkhtmltox.dll");
var IocContainer = new SynchronizedConverter(new PdfTools());
string html = await new StreamReader(req.Body).ReadToEndAsync();
var globalSettings = new GlobalSettings
{
ColorMode = ColorMode.Color,
Orientation = Orientation.Portrait,
PaperSize = PaperKind.A4,
Margins = new MarginSettings { Top = 10 },
};
var objectSettings = new ObjectSettings
{
PagesCount = true,
WebSettings = { DefaultEncoding = "utf-8" },
HtmlContent = html
};
var pdf = new HtmlToPdfDocument()
{
GlobalSettings = globalSettings,
Objects = { objectSettings }
};
byte[] pdfBytes = null;// IocContainer.Convert(pdf);
return new FileContentResult(pdfBytes, "application/pdf");
}
}