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