Convert PDF to Image using C# and Aspose.PDF for .NET in Docker - System Out Of Memory Exception

Could not generate PDF: System.OutOfMemoryException: Out of memory.

We are currently experiencing a System.OutOfMemoryException from the following code:

public void Build(ExternalReportPdf externalReportPdf)
{
using (var ms = new MemoryStream(externalReportPdf.SpotfirePdfBytes))
{
var externalPdfReport = new Aspose.Pdf.Document(ms);

            if (externalPdfReport.Pages.Count > 0)
            {
                foreach (var page in externalPdfReport.Pages)
                {
                    var rect = page.GetPageRect(true);

                    if (rect.Width > rect.Height)
                    {
                        _builder.CurrentParagraph.ParentSection.PageSetup.Orientation = Orientation.Landscape;
                    }
                    else
                    {
                        _builder.CurrentParagraph.ParentSection.PageSetup.Orientation = Orientation.Portrait;
                    }

                    using (var imageStream = new MemoryStream())
                    {
                        var resolution = new Aspose.Pdf.Devices.Resolution(300);
                        var jpegDevice = new Aspose.Pdf.Devices.JpegDevice(resolution, 100);
                        jpegDevice.Process(page, imageStream);

                        _builder.InsertImage(imageStream);
                    }
                }
            }
        }
    }

It throws on line 49 (marked in red). This only happens while running inside a Linux container. Running the same code on Windows works perfectly. The exception is preceded by the following warnings:

** (process:1): WARNING **: Path conversion requested 28958208 bytes (3264 x 2218). Maximum size is 8388608 bytes.

** (process:1): WARNING **: Path conversion requested 28958208 bytes (3264 x 2218). Maximum size is 8388608 bytes.

** (process:1): WARNING **: Path conversion requested 28958208 bytes (3264 x 2218). Maximum size is 8388608 bytes.

** (process:1): WARNING **: Path conversion requested 28958208 bytes (3264 x 2218). Maximum size is 8388608 bytes.

If we reduce the resolution from 300 to 150, the code executes fine on both Windows and Linux, but 150 is not enough for us.

The exact same issue has previously been reported by other Aspose customers, but no resolution is provided:

What we are trying to accomplish with the above code, is to convert a PDF page into an image, then insert each image into an Aspose.Words document using the DocumentBuilder class.

Let me also add that the container running the above code, do have enough physical resources. It has been configured with 4GB RAM, and it is only consuming about 600MB when the System.OutOfMemoryException occurs.

@mint007

Could you please also share the sample source PDF document for our reference? We will test the scenario in our environment and address it accordingly. Also, please make sure to use the latest version of the API i.e. 21.4 before sharing the sample file. Please also share the OS information which you have configured in Docker.

ppr_220.pdf (1.2 MB)

I have attached the pdf document. The current aspose version we use is 21.4 however it still doesn’t in the linux (ubuntu) docker container.

@mint007

We have tested the scenario in our environment and were unable to reproduce the issue. Would you kindly confirm if you are executing the code in x64 mode of debugging? Also, please make sure that libgdiplus package is updated in the Linux environment. Please share complete OS Name and Linux Version (it would be great if you can please share the Docker file) with us. We will further log and investigation ticket and share the ID with you.