繁体   English   中英

使用 Gembox.Document 的 azure 函数中出现“无法加载文件或程序集‘PresentationCore’”错误

[英]"Could not load file or assembly 'PresentationCore'" error in azure function using Gembox.Document

我目前使用 Gembox.Document 从 PDF 文档中读取内容。 我有一个包含所有内容的类库和一个引用它的自托管 .NET Core 3.1 服务。 我用 PDF 数据查询服务,它用内容响应。 我现在想将此功能移至 azure 函数 (v3),但遇到以下错误:

无法加载文件或程序集“PresentationCore,版本=4.0.0.0,文化=中性,PublicKeyToken=31bf3856ad364e35”。 该系统找不到指定的文件。

为了简化它,我只将基本部分移到了 azure 函数中,您可以在下面看到:

public static async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
    ILogger log)
{
    ComponentInfo.SetLicense("FREE-LIMITED-KEY");
    ComponentInfo.FreeLimitReached += (sender, args) => args.FreeLimitReachedAction = FreeLimitReachedAction.ContinueAsTrial;

    try
    {
        string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
        ParseRequest data = JsonConvert.DeserializeObject<ParseRequest>(requestBody);

        StringBuilder sb = new StringBuilder();

        using (var ms = new MemoryStream(data.Data))
        {
            // Load document from file's path.
            var document = DocumentModel.Load(ms, LoadOptions.PdfDefault);

            foreach (var childElement in document.GetChildElements(true, ElementType.Paragraph))
            {
                sb.AppendLine(childElement.Content.ToString());
            }
        }

        return new OkObjectResult(sb.ToString());
    }
    catch (Exception e)
    {
        return new OkObjectResult("Unable to read document");
    }
}

这是天蓝色功能的限制吗? 我已经阅读了一些相互矛盾的内容,这些内容表明它可以和不能完成,因为它使用的是 WPF dll。 作为记录,GemBox 网站提供了一个在 azure 函数中创建 PDF 文档的示例: https ://www.gemboxsoftware.com/document/examples/create-word-pdf-on-azure-functions-app-service/ 5901 所以我不明白为什么我也不能阅读!

谢谢!

编辑 1:

根据 mu88 的评论,我已将 .csproj 文件更改为以下内容,但没有帮助。

在此处输入图片说明

GemBox.Document有这个问题(它可能依赖于 .net Framework 组件),但GemBox.Pdf工作正常,如下所示。

我使用带有以下函数的GemBox.Pdf nuget 对其进行了测试,它适用于在部署的函数应用程序中创建和加载 pdf。

在此处输入图片说明

创建PDF:

        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            ComponentInfo.SetLicense("FREE-LIMITED-KEY");

            using (var document = new PdfDocument())
            {
                // Add a page.
                var page = document.Pages.Add();

                // Write a text.
                using (var formattedText = new PdfFormattedText())
                {
                    formattedText.Append("Hello World!");

                    page.Content.DrawText(formattedText, new PdfPoint(100, 700));
                }

                var fileName = "Output.pdf";
                var options = SaveOptions.Pdf;
                using (var stream = new MemoryStream())
                {
                    document.Save(stream, options);
                    return new FileContentResult(stream.ToArray(), "application/pdf") { FileDownloadName = fileName };
                }
            }
        }

在此处输入图片说明

加载PDF:

        [FunctionName("Function3")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            ComponentInfo.SetLicense("FREE-LIMITED-KEY");

            try
            {
                StringBuilder sb = new StringBuilder();
                using (var document = PdfDocument.Load(req.Body))
                {
                    foreach (var page in document.Pages)
                    {
                        sb.AppendLine(page.Content.ToString());
                    }
                }

                return new OkObjectResult(sb.ToString());
            }
            catch (Exception e)
            {
                return new ExceptionResult(e, true);
            }
        }

我联系了 GemBox 支持,他们回复如下

不幸的是,GemBox.Document 使用 WPF 来读取 PDF 文件。 因此,即使您可以在 Azure Functions 上编写 PDF 文件,恐怕您也无法阅读它们。

而且,我应该指出 GemBox.Document 中的 PDF 阅读器从未离开 BETA 阶段,它的使用有限。 有关更多信息,请查看阅读 PDF 格式(测试版)部分的支持级别

相反,我建议您尝试使用 GemBox.Pdf,请参阅它的阅读示例。 使用 GemBox.Pdf,您可以在 Azure Functions 上读写 PDF 文件。

最后,从长远来看,我们计划将 GemBox.Document 中 PDF 阅读器 (BETA) 和 PDF 编写器的当前(内部)实现替换为包含在 GemBox.Pdf 中的较新实现,而不会更改 GemBox.Document 的公共 API . 但这不会在今年完成,以后我现在不能说。

唉,GemBox.Document不可能。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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