简体   繁体   English

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

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

I currently use Gembox.Document to read content from PDF documents.我目前使用 Gembox.Document 从 PDF 文档中读取内容。 I have a class library that houses it all and a self hosted .NET Core 3.1 service that references it.我有一个包含所有内容的类库和一个引用它的自托管 .NET Core 3.1 服务。 I query the service with the PDF data and it responds with the content.我用 PDF 数据查询服务,它用内容响应。 I now want to move this functionality to an azure function (v3) instead, but I am running into the following error:我现在想将此功能移至 azure 函数 (v3),但遇到以下错误:

Could not load file or assembly 'PresentationCore, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.无法加载文件或程序集“PresentationCore,版本=4.0.0.0,文化=中性,PublicKeyToken=31bf3856ad364e35”。 The system cannot find the file specified.该系统找不到指定的文件。

To simplify it I have moved only the essential parts into the azure function which you can see below:为了简化它,我只将基本部分移到了 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");
    }
}

Is this a restriction of azure functions?这是天蓝色功能的限制吗? I have read several conflicting things which suggest it can and can't be done as it's using a WPF dll.我已经阅读了一些相互矛盾的内容,这些内容表明它可以和不能完成,因为它使用的是 WPF dll。 For the record, the GemBox website provides an example for creating a PDF document in an azure function: https://www.gemboxsoftware.com/document/examples/create-word-pdf-on-azure-functions-app-service/5901 .作为记录,GemBox 网站提供了一个在 azure 函数中创建 PDF 文档的示例: https ://www.gemboxsoftware.com/document/examples/create-word-pdf-on-azure-functions-app-service/ 5901 So I don't see why I wouldn't be able to read one too!所以我不明白为什么我也不能阅读!

Thanks!谢谢!

EDIT 1:编辑 1:

As per mu88's comment, I have changed the .csproj file to the below and it hasn't helped.根据 mu88 的评论,我已将 .csproj 文件更改为以下内容,但没有帮助。

在此处输入图片说明

GemBox.Document has this issue (it might be dependent on .net Framework component), but GemBox.Pdf works fine as below. GemBox.Document有这个问题(它可能依赖于 .net Framework 组件),但GemBox.Pdf工作正常,如下所示。

I tested it using the GemBox.Pdf nuget with the below Function and it works fine for both creating and loading pdf in deployed Function app.我使用带有以下函数的GemBox.Pdf nuget 对其进行了测试,它适用于在部署的函数应用程序中创建和加载 pdf。

在此处输入图片说明

Create 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 };
                }
            }
        }

在此处输入图片说明

LoadPDF:加载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);
            }
        }

I reached out to GemBox support and they responded with the following :我联系了 GemBox 支持,他们回复如下

Unfortunately, GemBox.Document uses WPF for reading PDF files.不幸的是,GemBox.Document 使用 WPF 来读取 PDF 文件。 So, even though you can write PDF files on Azure Functions, I'm afraid you cannot read them.因此,即使您可以在 Azure Functions 上编写 PDF 文件,恐怕您也无法阅读它们。

But also, I should point out that PDF reader in GemBox.Document never left the BETA stage, it has limited usage.而且,我应该指出 GemBox.Document 中的 PDF 阅读器从未离开 BETA 阶段,它的使用有限。 For more information, please check the Support level for reading PDF format (beta) section.有关更多信息,请查看阅读 PDF 格式(测试版)部分的支持级别

Instead, I would suggest you try out GemBox.Pdf, see its Reading example.相反,我建议您尝试使用 GemBox.Pdf,请参阅它的阅读示例。 With GemBox.Pdf you can read and write PDF files on Azure Functions.使用 GemBox.Pdf,您可以在 Azure Functions 上读写 PDF 文件。

Last, in the long term, we plan to replace the current (internal) implementations of both PDF reader (BETA) and PDF writer in GemBox.Document with a newer implementation that's contained in GemBox.Pdf without changing the public API of GemBox.Document.最后,从长远来看,我们计划将 GemBox.Document 中 PDF 阅读器 (BETA) 和 PDF 编写器的当前(内部)实现替换为包含在 GemBox.Pdf 中的较新实现,而不会更改 GemBox.Document 的公共 API . But that will not be done in the current year and for later I cannot say at this moment.但这不会在今年完成,以后我现在不能说。

Alas, it is not possible with GemBox.Document.. yet .唉,GemBox.Document不可能。

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

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