簡體   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