Failed to convert Image type format to PDF: The type initializer for 'Gdip' threw an exception

Hi,

I am trying to convert images format to the pdf please find the below code for that:
using (var imageStream = new MemoryStream(inputBytes))
{
using (var outputStream = new MemoryStream())
{
var pdf = new Aspose.Pdf.Document();
var image = new Aspose.Pdf.Image() { ImageStream = imageStream };
pdf.Pages.Add().Paragraphs.Add(image);
pdf.Save(outputStream);
return outputStream.ToArray();
}
}

It is works fine in my local which is in windows but when I am trying to get the same result with the deployed version of azure linux. I am not able to get success respose in throws me an below error:

Failed to convert document to PDF: The type initializer for ‘Gdip’ threw an exception. —> System.TypeInitializationException: The type initializer for ‘Gdip’ threw an exception. —> System.PlatformNotSupportedException

Thanks

@cdayalal

The error you are encountering, “The type initializer for ‘Gdip’ threw an exception,” typically occurs when using the System.Drawing.Common library on non-Windows platforms, such as Linux. This library is not fully supported on Linux, which leads to the System.PlatformNotSupportedException you are seeing.

To resolve this issue, you can switch to using the Aspose.Pdf.Drawing library instead of Aspose.Pdf. The Aspose.Pdf.Drawing library does not have a dependency on System.Drawing, making it more suitable for Linux environments. Additionally, you may need to install the libgdiplus package on your Linux server to ensure that the API functions correctly.

Here’s a modified version of your code that uses Aspose.Pdf.Drawing:

using (var imageStream = new MemoryStream(inputBytes))
{
    using (var outputStream = new MemoryStream())
    {
        var pdf = new Aspose.Pdf.Document();
        var image = new Aspose.Pdf.Drawing.Image() { ImageStream = imageStream };
        pdf.Pages.Add().Paragraphs.Add(image);
        pdf.Save(outputStream);
        return outputStream.ToArray();
    }
}

Make sure to reference the correct namespaces and ensure that you have the latest version of Aspose.Pdf.Drawing installed. This should help you avoid the issues related to System.Drawing.Common on your Azure Linux deployment.

If you continue to experience issues, please let us know, and we can assist you further.

Sources:
[1]: PlatformNotSupportedException for PDF to Image and Word to Image Conversion
[2]: Convert various Images formats to PDF in .NET - Aspose Documentation

Getting below error with some large image type
Unable to convert Image document to pdf : : Cannot allocate so many bytes. Use LoadPartialPixels instead

@cdayalal

If possible, can you please share your sample image with us as well so that we can test the scenario in our environment and address it accordingly.

I am able to get the pdf version with small image file but when I am goes above 40Mb it throws error.

Find the below attachment:

Instead of updating package can we installed the required library in docker container.

Can you please suggest me which library should i need to install ?

@cdayalal

You should install Aspose.Pdf.Drawing instead of Aspose.PDF for .NET from NuGet and it should work without any issues in docker. Please try it and let us know if you still notice any issues. Also, please make sure to use a valid license while using the API.

I already tried with Aspose.Pdf.Drawing package for conversion but it will not work with large image file it seems it can not allocate so many bytes and suggest to LoadPartialPixels

And regarding the licence currently I am using trial for test

@cdayalal

Looks like there is an issue that needs to be investigated in detail. Could you please share below information with us so that we can log an investigation ticket and share the ID with you?

  • your docker file
  • complete error message and stack trace information

Find the below attachment for the Error Logs and Docker file.
And also I am using below aspose packages:

Dockerfile.docx (6.4 KB)

Logs.docx (73.4 KB)

@cdayalal

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-58312

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

Thanks… I hope this will be resolved soon and you can give me an estimate

@cdayalal

Sure, we will investigate and prioritize the ticket on first come first serve basis and as soon as we make some progress in this regard, we will share with you.