Convert PDF to Image in C# using Aspose.PDF - weird output on Azure function (.net core 3)

I’m running an azure function to produce images of PDFs and recently had an issue which I don’t fully understand yet. The code is running in .net core 3 on azure functions (windows, function 3 runtime)

The part that produces a weird output is pretty straightforward and looks like this:

using var document = new Document(pdfStream);
var jpegDevice = new JpegDevice(new Resolution(150), 90);
jpegDevice.Process(document.Pages[1], imageStream);

I will attach the input PDF and the output image. The output image seems mangled (like a font issue) but we can’t get a good reproduction scenario (yet), it seems to happen on certain azure functions hosts only.

Do you know what might cause these issues?

I’m using Aspose PDF 20.10.0

Edit: Not sure how to attach files, here’s a wetransfer link with both the pdf as well as the jpg image: https://we.tl/t-f7ZIDyewFL

Edit 2:

I managed to reproduce it on my own machine, could it be related to this topic: Operations that change non-concurrent collections must have exclusive access. error in Convert(Document.CallBackGetHocr) - #2 by asad.ali

This code can be used to reproduce it (make a lot of copies of the pdf file attached to simulate multiple pages in parallel:

var allFiles = Directory.GetFiles(@"C:\tmp\pages");
Parallel.ForEach(allFiles, inputfile =>
{
    using var document = new Document(inputfile);
    var jpegDevice = new JpegDevice(new Resolution(150), 90);
    jpegDevice.Process(document.Pages[1], $"{Guid.NewGuid()}.jpg");
});

When I use Aspose.PDF 20.9 it works fine, with Aspose.PDF 20.10 every image is mangled.

One last edit: We found another workaround (for now we downgraded back to Aspose PDF 20.9) is to set RenderingOptions.MaxFontsCacheSize = 0 on the jpegDevice, so it’s probably related to the font cache.

@Azure

The issue seems related to both font and parallel manner of accessing the files. There are few issue logged before where unwanted results were being produced due to usage of Parallel.ForEach. Nevertheless, we have logged an investigation ticket as PDFNET-48963 in our issue tracking system for further analysis. We will further look into its details and keep you posted with the status of its rectification. Please be patient and spare us some time.

We are sorry for the inconvenience.

@Azure

We have introduced enhancement to grant thread-safe access to some static data structures. We really hope that using Aspose.PDF for .NET 21.5, the issue will be resolved. Please try using the latest version and let us know in case you still notice any issue.