System.AggregateException: One or more errors occurred. (The type initializer for 'Gdip' threw an exception.)

I am trying to convert pdf to image document using .net 8 using Aspose.Pdf and it works fine in my local machine(windows). But i get an issue while running it in alpine linux server.
The exception looks like this
System.AggregateException: One or more errors occurred. (The type initializer for ‘Gdip’ threw an exception.)
—> System.TypeInitializationException: The type initializer for ‘Gdip’ threw an exception.
—> System.PlatformNotSupportedException: System.Drawing.Common is not supported on non-Windows platforms. See Breaking change: System.Drawing.Common only supported on Windows - .NET | Microsoft Learn for more information.
at System.Drawing.SafeNativeMethods.Gdip.<>c.<.cctor>b__2_0(String _, Assembly _, Nullable`1 _)
at System.Runtime.InteropServices.NativeLibrary.LoadLibraryCallbackStub(String libraryName, Assembly assembly, Boolean hasDllImportSearchPathFlags, UInt32 dllImportSearchPathFlags)
at System.Drawing.SafeNativeMethods.Gdip.g____PInvoke|32_0(IntPtr* __token_native, StartupInputEx* __input_native, StartupOutput* __output_native)
at System.Drawing.SafeNativeMethods.Gdip.GdiplusStartup(IntPtr& token, StartupInputEx& input, StartupOutput& output)
at System.Drawing.SafeNativeMethods.Gdip…cctor()
— End of inner exception stack trace —
at System.Drawing.SafeNativeMethods.Gdip.GdipCreateMatrix2(Single m11, Single m12, Single m21, Single m22, Single dx, Single dy, IntPtr& matrix)
at System.Drawing.Drawing2D.Matrix…ctor(Single m11, Single m12, Single m21, Single m22, Single dx, Single dy)
at #=zCHl3SkG5wkA90MugFMD$bwVFbCtr7QnPt7qrsNQ=…ctor(Single #=zhK6P$xU=, Single #=zzslREYc=, Single #=zO1R7TGk=, Single #=zEbdxUF4=, Single #=zKjldTpk=, Single #=zhhd5Bhs=)
at #=zA7aN_PIrEpANf7hWsEOpslLbBIKuTwkJED1KGyJr9YNy.#=zH05g24fc2Oay(#=zJWvPQRDSw1wI1aaRBPcqarHvZI9NiJM7Tw== #=zAioM7v4=, #=zxKAo70E9lo_C8BMN56cAO7J9$i7Pu6rXpwyLFB8= #=zsws19IEG2_Fo, #=zDnPkXf2TZcGDHD9faZlJijvmQSrj4L6jlQ== #=zl235MT0=, Single #=zG5UHabh6t2ce, Single #=zo9f3y22gxEwm, Boolean #=z3kHkMJvRxYtieIc9kg==, Int32 #=zVEX0bGkpHnjw5J59e1Xvplg=, Boolean #=zSnrFkx4lGm8O, Double& #=zt1NxiOs=, Double& #=zbahmwa4=, #=zrqJr8NPFguLZQYihr1tIOKv8rdHZ08bG6w==& #=zFLtYEew=)
at #=zA7aN_PIrEpANf7hWsEOpslLbBIKuTwkJED1KGyJr9YNy…ctor(#=z5kEy0I8nwJ7zA08E53HCmWgD0RpI #=zV9xjgWo=, #=zJWvPQRDSw1wI1aaRBPcqarHvZI9NiJM7Tw== #=zAioM7v4=, #=zxKAo70E9lo_C8BMN56cAO7J9$i7Pu6rXpwyLFB8= #=zsws19IEG2_Fo)
at #=zb0erBJEZd320jOwmmmR4efYU0ESWO5BFoCfxll4=.#=z0uazCWqDYkBe(#=z5kEy0I8nwJ7zA08E53HCmWgD0RpI #=zV9xjgWo=, #=zJWvPQRDSw1wI1aaRBPcqarHvZI9NiJM7Tw== #=zAioM7v4=, #=zxKAo70E9lo_C8BMN56cAO7J9$i7Pu6rXpwyLFB8= #=zsws19IEG2_Fo)
at #=zEVopfVcJ8Xy1bxtjwprhx8eO6Y9l3RwyNN4yjYpGRGuO.#=zQw5nv1g=(#=z5kEy0I8nwJ7zA08E53HCmWgD0RpI #=zV9xjgWo=, #=zJWvPQRDSw1wI1aaRBPcqarHvZI9NiJM7Tw== #=zAioM7v4=, #=zxKAo70E9lo_C8BMN56cAO7J9$i7Pu6rXpwyLFB8= #=zm9W6zPs=, #=zA7aN_PIrEpANf7hWsEOpslLbBIKuTwkJED1KGyJr9YNy& #=zhgbSbLo=)
at #=zvOFLeziqL$npkFFs0vCvX6jDHBlQ.#=zDUKdOdY=(#=zA7aN_PIrEpANf7hWsEOpslLbBIKuTwkJED1KGyJr9YNy& #=zhgbSbLo=)
at #=zvOFLeziqL$npkFFs0vCvX6jDHBlQ.#=zDUKdOdY=()
at Aspose.Pdf.Devices.ImageDevice.#=zDUKdOdY=(Page #=zAioM7v4=)
at Aspose.Pdf.Devices.JpegDevice.Process(Page page, Stream output)
at Aspose.Pdf.Api.Core.Services.Impl.SinglePDFToImageService.SplitMultipleImages(String fileName, String commonTempDirectory, Byte[] pdfBytes, String rootFolder) in D:\Development Tools\bamboo-agent-home\xml-data\build-dir\MOZ-EAPACC0-JOB1\Aspose.Pdf.Api.Core\Services\Impl\SinglePDFToImageService.cs:line 176
at Aspose.Pdf.Api.Core.Services.Impl.SinglePDFToImageService.<>c__DisplayClass9_0.b__1() in D:\Development Tools\bamboo-agent-home\xml-data\build-dir\MOZ-EAPACC0-JOB1\Aspose.Pdf.Api.Core\Services\Impl\SinglePDFToImageService.cs:line 85
at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
— End of stack trace from previous location —
at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
— End of inner exception stack trace —

i have already installed libgdiplus and msttfontconfig in the linux container.
The exception is thrown in this method jpegDevice.Process(pdfDocument.Pages[1], memoryStream);
what can i do to fix the issue ?

Aspose.pdf version 24.9.0

@geekyboy

The error you are encountering, specifically System.PlatformNotSupportedException: System.Drawing.Common is not supported on non-Windows platforms, indicates that the System.Drawing.Common library is not supported on Linux environments, including Alpine Linux. This is a known limitation for .NET 6 and later versions.

To resolve this issue, you should switch to using SkiaSharp, which is a cross-platform graphics library that is supported on non-Windows systems. Here are the steps you can follow to fix the issue:

  1. Update Your Project Dependencies:

    • Ensure that you have the SkiaSharp and SkiaSharp.NativeAssets.Linux packages added to your project. You can do this by running the following command in your project directory:
      dotnet add package SkiaSharp
      dotnet add package SkiaSharp.NativeAssets.Linux
      
  2. Install Required Packages in Your Dockerfile:

    • If you are using Docker, make sure your Dockerfile includes the necessary packages. For Alpine Linux, you need to add fontconfig. Here’s an example of how your Dockerfile might look:
      FROM mcr.microsoft.com/dotnet/runtime:6.0-alpine3.16 AS base
      WORKDIR /app
      RUN apk update && apk add fontconfig
      COPY fonts/ /usr/share/fonts
      FROM mcr.microsoft.com/dotnet/sdk:6.0-alpine3.16 AS build
      WORKDIR /src
      COPY ["YourProject.csproj", "."]
      RUN dotnet restore "./YourProject.csproj"
      COPY . .
      WORKDIR "/src/."
      RUN dotnet build "YourProject.csproj" -c Release -o /app/build
      FROM build AS publish
      RUN dotnet publish "YourProject.csproj" -c Release -o /app/publish
      FROM base AS final
      WORKDIR /app
      COPY --from=publish /app/publish .
      ENTRYPOINT ["dotnet", "YourProject.dll"]
      
  3. Test Your Application:

    • After making these changes, rebuild your application and run it again in your Alpine Linux environment. This should resolve the issue related to the Gdip initialization.

By following these steps, you should be able to successfully convert PDF documents to images on your Alpine Linux server without encountering the System.Drawing.Common related exceptions.

If you continue to face issues, please ensure that all dependencies are correctly installed and that your project is configured to use SkiaSharp for graphics rendering.

Sources:
[1]: How to Run Aspose.Cells for .Net6 - Aspose Documentation

Tried it out and the issue is still present

@geekyboy

Instead of Aspose.PDF for .NET, we recommend that you use Aspose.Pdf.Drawing for .NET package which is designed to work under the Linux like environments. It does not have dependency upon System.Drawing.Common except for functions like printing a PDF. Please try using it and install libgdiplus package as well. In case issue still persists, please let us know.

Thanks @asad.ali , it did work after using aspose.pdf.drawing

@geekyboy

Thanks for the feedback. Its nice to know that your issue has been resolved. Please feel free to create a new topic in case you face any issues.