无法使用dinktopdf加载dll libwkhtmltox以获得.net core2.2天蓝色functionapp

时间:2019-06-25 22:00:08

标签: c# html pdf azure-functions dinktopdf

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

1 个答案:

答案 0 :(得分:0)

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

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

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

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

Docs用于访问功能主机的目录