简体   繁体   中英

"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. I have a class library that houses it all and a self hosted .NET Core 3.1 service that references it. I query the service with the PDF data and it responds with the content. I now want to move this functionality to an azure function (v3) instead, but I am running into the following error:

Could not load file or assembly 'PresentationCore, Version=4.0.0.0, Culture=neutral, 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:

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. 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 . So I don't see why I wouldn't be able to read one too!

Thanks!

EDIT 1:

As per mu88's comment, I have changed the .csproj file to the below and it hasn't helped.

在此处输入图片说明

GemBox.Document has this issue (it might be dependent on .net Framework component), but GemBox.Pdf works fine as below.

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.

在此处输入图片说明

Create 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:

        [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 :

Unfortunately, GemBox.Document uses WPF for reading PDF files. So, even though you can write PDF files on Azure Functions, I'm afraid you cannot read them.

But also, I should point out that PDF reader in GemBox.Document never left the BETA stage, it has limited usage. For more information, please check the Support level for reading PDF format (beta) section.

Instead, I would suggest you try out GemBox.Pdf, see its Reading example. With GemBox.Pdf you can read and write PDF files on Azure Functions.

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. 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 .

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