1

我有一个使用 .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");
        }
}
4

1 回答 1

0

这个解决方案我也遇到了很多麻烦,但我认为一个问题可能是您的目录。尝试使用

$@"{context.FunctionDirectory}\Dependencies\libwkhtmltox.dll");

反而。您可以通过函数重载来访问执行上下文。

  [HttpTrigger(AuthorizationLevel.Function, "get", Route = "iamgroot")] HttpRequest req, ExecutionContext context)

访问函数宿主目录的文档

于 2020-10-16T14:01:57.163 回答