【问题标题】:Unable to load the dll libwkhtmltox using dinktopdf for a .net core2.2 azure functionapp无法使用 dinktopdf 为 .net core2.2 azure functionapp 加载 dll libwkhtmltox
【发布时间】:2019-11-07 19:06:52
【问题描述】:

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

【问题讨论】:

  • 你能在 azure 门户 -> azure function kudu 站点中看到 libwkhtmltox.dll 吗?
  • 我还在本地尝试。它仍未部署到 Azure 门户。我还尝试手动将库添加到函数中并将属性设置为始终复制。还是不行。
  • 这个问题有什么更新吗?

标签: c# html pdf azure-functions dinktopdf


【解决方案1】:

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

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

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

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

Docs访问函数宿主目录

【讨论】:

    猜你喜欢
    • 2021-06-23
    • 2019-08-21
    • 1970-01-01
    • 2016-09-17
    • 2017-04-29
    • 1970-01-01
    • 1970-01-01
    • 2012-04-12
    • 2023-01-20
    相关资源
    最近更新 更多