Fail to convert PDF to JPEG Due to Image Size

Encountered this error while trying to convert PDFs to JPEGs with dotnetcore in Ubuntu:

** (process:24425): WARNING **: Path conversion requested 8582688 bytes (1224 x 1753). Maximum size is 8388608 bytes.

What should I do to overcome this limitation? Thanks.

@kityeo

Thanks for contacting support.

There is no such limitations in Aspose.PDF for .NET related to size of file Path. Maximum characters in Path is allowed by the Operating System itself and any API being used in that OS, should have to rely on that limitation. You may please try placing the file(s) at some shorter path and try your scenario.

In case you still face issue, please share your input PDF document along with code snippet and complete path information. Also, please share the environment details like OS version, Application Type, Tool in which you are developing application, etc. We will test the scenario in our environment and address it accordingly.

Hi Asad,

The error doesn’t look like maximum characters in path. It looks like some space constraint imposed by the OS (Ubuntu) as you can interpret from the error.

I’m seeking advise from Aspose not because of the limitation of Aspose.PDF but rather how do I overcome the limitation imposed by the OS.

Besides, I think the error looks very much like an error thrown from Aspose.PDF as there is even mention about resolution (1224 x 1753) of the output image. It is unlikely the OS would know anything about throwing such an error with regards to resolution. In addition, when the resolution and quality of the output is reduced, no error is thrown.

With regards to your question, the complete path was: /var/aspnetcore/asposetest/output/c3b4b252-aa4f-4825-85d2-febc938f483e

Here’s the code snippet:

string imageOutputDirectory = "/var/aspnetcore/asposetest/output/c3b4b252-aa4f-4825-85d2-febc938f483e";

    using (MemoryStream ms = new MemoryStream(uploadedFile))
    {
    	Aspose.Pdf.Document pdf = new Aspose.Pdf.Document(ms);

    	for (int pageCount = 1; pageCount <= pdf.Pages.Count; pageCount++)
    	{
    		string imageFilename = Path.Combine(imageOutputDirectory, $"image{pageCount}.jpg");

    		// Convert PDF pages into images
    		using (FileStream imageStream = new FileStream(imageFilename, FileMode.Create))
    		{
    			Resolution resolution = new Resolution(150);

    			JpegDevice jpegDevice = new JpegDevice(resolution, 100);

//Error happens after this line
    			jpegDevice.Process(pdf.Pages[pageCount], imageStream);

    			// Close stream
    			imageStream.Close();
    		}
    		
    	}
    }

@kityeo

Thanks for sharing these details.

Would you please share your sample PDF document with us, so that we can test the scenario accordingly.

Hi Im facing this same thing in linux container. Im converting PDF to image. It will work fine in Windows but sorry for linux. im using aspose Aspose.PDF 20.8.0

 System.OutOfMemoryException: Out of memory.
    at System.Drawing.Graphics.get_ClipBounds()
    at #=zSK246xR0STLCCf_RpAEVwH0NEAig$Scziu7SfiM=.#=zZ2ut6Haa31O5(#=zVUQqnLiJKanv_2TqYJoDVxjRKtZhUwDFhA== #=zQVvbOr0=)
    at #=z8JzRwMDnRYyPoUkGXOTktM9trhIXgYkw427agUEuJHAgR0sVFw==.#=zjh146I1ljGZI(#=zJ7mqFOeX8tyl7GEYBXHhtqrARUJU7FUlNWcI_rNzPOUq #=zC6IqXOI=)
    at #=zw34YIMWWW$i4N0k7rtWKKRUws3dYC_xU0nPAue8LRQtK.#=zHIU4q42Gxrmp(#=zJ7mqFOeX8tyl7GEYBXHhtqrARUJU7FUlNWcI_rNzPOUq #=zC6IqXOI=)
    at #=zw34YIMWWW$i4N0k7rtWKKRUws3dYC_xU0nPAue8LRQtK.#=z$Yg4egs=(#=zJ7mqFOeX8tyl7GEYBXHhtqrARUJU7FUlNWcI_rNzPOUq #=zC6IqXOI=)
    at #=zJRoqtKfEX723e63CZPklJ_$$uDoryYUCCnC57eCjxMuB.#=zdOb_qo3nnEeT(#=zJ7mqFOeX8tyl7GEYBXHhtqrARUJU7FUlNWcI_rNzPOUq #=zC6IqXOI=, List`1 #=zu3zJp7EPvQVm)
    at #=zJRoqtKfEX723e63CZPklJ_$$uDoryYUCCnC57eCjxMuB.#=zOVYCjv4=(#=zCu8FNLfeUBNURJFTmzd1xghAdCdS #=zzftdz3o=, #=zAoiaTHyEQCAStHFalUSLEr1YFcm5oC49Fw== #=zkNijddA=, #=zU9Vh7gAU4ZMIdpayEFMv6gxhqTuqYBtuz8Oe2ME= #=zxurZdh8=, #=zJ7mqFOeX8tyl7GEYBXHhtqrARUJU7FUlNWcI_rNzPOUq& #=zC6IqXOI=)
    at #=zYywSfhaJEevNXSPkTYIhcpTfdq2M.#=zRovHUxA=(#=zJ7mqFOeX8tyl7GEYBXHhtqrARUJU7FUlNWcI_rNzPOUq& #=zC6IqXOI=)
    at #=zYywSfhaJEevNXSPkTYIhcpTfdq2M.#=zRovHUxA=()
    at Aspose.Pdf.Devices.ImageDevice.#=zRovHUxA=(Page #=zkNijddA=)
    at Aspose.Pdf.Devices.PngDevice.Process(Page page, Stream output)

Here is my code:

        public ImageData GenerateImage1(byte[] pdfData, int pageNo)
        {

            using (var imageStream = new MemoryStream())
            {
                using (var pdfDocument = new Document(new MemoryStream(pdfData), isManagedStream: true))
                {
                    
                    Resolution resolutionLow = new Resolution(300);
                    PngDevice pngDeviceTempTest = new PngDevice(resolutionLow);

                    //following will casing error
                    pngDeviceTempTest.Process(pdfDocument.Pages[pageNo], imageStream);


                    pdfDocument.Dispose();
                    var data = new ImageData()
                    {
                        MimeType = "image/png",
                        Size = imageStream.Length,
                        Data = imageStream.ToArray()
                    };
                    return data;
                }
            }
        }

@AnushkaDSC

Would you please share your sample PDF document along with complete environment details i.e OS Name and Version, Application Type, etc. We will test the scenario in our environment and address it accordingly.

Hi Asad,
Do you have any update for this? we are facing same issue in Ubuntu but same working fine in windows.

@cyginfo

Would you kindly make sure to test the case using 21.11 version of the API. In case issue still persists, please share your sample PDF document along with sample code snippet so that we can test the scenario in our environment and address it accordingly.

Hi @asad.ali, we are facing the same issue with aspnet core 5 container (Debian 11). it works in Windows.

this is the line of code its failing at (GetNextImage):

 PdfConverter converter = new PdfConverter();
                stream.Position = 0;
                converter.BindPdf(stream);
                converter.DoConvert();
                int pageNumber = 1;
                //Check if pages exist and then convert to image one by one
                while (converter.HasNextImage())
                {
                    MemoryStream imageStream = new MemoryStream();
                    converter.GetNextImage(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg);

and below is the error log

System.OutOfMemoryException:
at System.Drawing.SafeNativeMethods+Gdip.CheckStatus (System.Drawing.Common, Version=5.0.0.2, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51)
at System.Drawing.Graphics.get_ClipBounds (System.Drawing.Common, Version=5.0.0.2, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51)
at #=zciTlGLc_3S$We7ZsQlA8VEoRdQbrX6qyLMIrYMU=.#=ziYYr_mEsPD4m (Aspose.PDF, Version=22.2.0.0, Culture=neutral, PublicKeyToken=716fcc553a201e56)
at #=zZ6lUGLojU6wvByyTys0oP4GopVPlipbb8YCYXMRXwK9RnCnF7g==.#=z652174FUtw1L (Aspose.PDF, Version=22.2.0.0, Culture=neutral, PublicKeyToken=716fcc553a201e56)
at #=zoZbZ78XusXM_nzRHEWgXZaskeNtsMSidg8nKj2utxoBq.#=z9od4YRF6F0Qo (Aspose.PDF, Version=22.2.0.0, Culture=neutral, PublicKeyToken=716fcc553a201e56)
at #=zoZbZ78XusXM_nzRHEWgXZaskeNtsMSidg8nKj2utxoBq.#=zRUUa0H4= (Aspose.PDF, Version=22.2.0.0, Culture=neutral, PublicKeyToken=716fcc553a201e56)
at #=zwDke0qLP6_TZqOtxwhERJYRuDw8nso2p4r3UuiUpxfk3.#=z6c9BBlVBU5lC (Aspose.PDF, Version=22.2.0.0, Culture=neutral, PublicKeyToken=716fcc553a201e56)
at #=zwDke0qLP6_TZqOtxwhERJYRuDw8nso2p4r3UuiUpxfk3.#=zRxX32zo= (Aspose.PDF, Version=22.2.0.0, Culture=neutral, PublicKeyToken=716fcc553a201e56)
at #=zzeBqYrHwMR7Jfo5xz5yCEN1V9fyY.#=zQ200CKM= (Aspose.PDF, Version=22.2.0.0, Culture=neutral, PublicKeyToken=716fcc553a201e56)
at #=zzeBqYrHwMR7Jfo5xz5yCEN1V9fyY.#=zQ200CKM= (Aspose.PDF, Version=22.2.0.0, Culture=neutral, PublicKeyToken=716fcc553a201e56)
at Aspose.Pdf.Devices.ImageDevice.#=zQ200CKM= (Aspose.PDF, Version=22.2.0.0, Culture=neutral, PublicKeyToken=716fcc553a201e56)
at Aspose.Pdf.Devices.JpegDevice.Process (Aspose.PDF, Version=22.2.0.0, Culture=neutral, PublicKeyToken=716fcc553a201e56)
at Aspose.Pdf.Facades.PdfConverter.GetNextImage (Aspose.PDF, Version=22.2.0.0, Culture=neutral, PublicKeyToken=716fcc553a201e56)
at Aspose.Pdf.Facades.PdfConverter.GetNextImage (Aspose.PDF, Version=22.2.0.0, Culture=neutral, PublicKeyToken=716fcc553a201e56)
at Bupa.Vision.Infrastructure.Services.FileConversionService+d__7.MoveNext (Bupa.Vision.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: /src/src/Infrastructure/Services/FileConversionService.cs:128)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)

Any help would be appreciate.

Thanks!

@isharasp

Would you please make sure that the libgdiplus package is installed and up-to-date? Also, please make sure to use 22.3 version of the API. In case issue still persists, please share complete environment details along with sample PDF file. We will further proceed accordingly.

Thanks for the tip @asad.ali. Yes updating to the latest version of libgdiplus fixed my issue. ( though it was bit harder to update it in Dabian :slight_smile: ) . I created a quick blog post for anyone facing this issue and struggling to update the libgdiplus.

@isharasp

Thanks for the feedback and sharing the helpful information. Please feel free to create a new topic in case you face any issues.