简体   繁体   中英

Unable to load the dll libwkhtmltox using dinktopdf for a .net core2.2 azure functionapp

I have an Azure functionapp using .net Core 2.2 that is written for the converting the html text to pdf. I am using the DinkToPdf. When I run the function, I get "Unable to load the libwkhtmltox.dll. I have tried the alternate solutions as mentioned in some of the posts, but it still throws the same error.

I tried using Directory.GetCurrentDirectory and using path.combine

The code is below:

        [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");
        }
}

I´m also experiencing a lot of troubles with this solution but i think one problem might be your directory. Try use

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

instead. You can access the execution context by overload of your function.

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

Docs for accessing directory of function host

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM