Aspose.Words - The type initializer for 'Windows.Win32.PInvoke' threw an exception on linux container app

We are using the latest Aspose.Words Version=“25.5.0” to generate documents in a Linux container app, and we encountered the following error::

2025-05-13T09:35:50.6140702Z stdout F [2025-05-13 09:35:50 ERR] The type initializer for 'Windows.Win32.PInvoke' threw an exception.
2025-05-13T09:35:50.6141124Z stdout F System.TypeInitializationException: The type initializer for 'Windows.Win32.PInvoke' threw an exception.
2025-05-13T09:35:50.6141419Z stdout F  ---> System.TypeInitializationException: The type initializer for 'System.Drawing.Gdip' threw an exception.
2025-05-13T09:35:50.6141441Z stdout F  ---> System.TypeInitializationException: The type initializer for 'Windows.Win32.Graphics.GdiPlus.GdiPlusInitialization' threw an exception.
2025-05-13T09:35:50.6141457Z stdout F  ---> System.DllNotFoundException: Unable to load shared library 'gdiplus.dll' or one of its dependencies. In order to help diagnose loading problems, consider using a tool like strace. If you're using glibc, consider setting the LD_DEBUG environment variable:

This works fine on Windows, but fails in Linux.

We had previously faced a similar issue using an older version (11.0) and read that recent versions now use Aspose.Drawing instead of System.Drawing, which is supposed to be cross-platform supported.

Is there any additional configuration required to ensure this works properly on Linux? Are we missing any dependencies or environment setup?

@kishor338

The error you are encountering, specifically the System.DllNotFoundException for gdiplus.dll, indicates that your Linux container is missing the necessary graphics library that Aspose.Words relies on for rendering. While Aspose.Words has transitioned to using Aspose.Drawing for cross-platform support, it still requires certain native dependencies to function correctly on Linux.

To resolve this issue, you need to ensure that the libgdiplus library is installed in your Docker container. This library is essential for graphics operations in .NET applications that utilize System.Drawing or its alternatives.

Here are the steps you can follow to set up your Docker container correctly:

  1. Install Docker: Ensure that Docker is installed on your system. You can find installation instructions for various operating systems in the official Docker documentation.

  2. Create a Dockerfile: You need to create a Dockerfile that includes the installation of libgdiplus. Here’s a simple example of what your Dockerfile might look like:

    FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
    WORKDIR /app
    
    # Install libgdiplus
    RUN apt-get update && apt-get install -y libgdiplus
    
    # Copy your project files
    COPY . ./
    
    # Restore and build your project
    RUN dotnet restore
    RUN dotnet publish -c Release -o out
    
    # Set the entry point for the application
    ENTRYPOINT ["dotnet", "out/YourApp.dll"]
    
  3. Build the Docker Image: Navigate to the directory containing your Dockerfile and run the following command to build your Docker image:

    docker build -t your-app-name .
    
  4. Run the Docker Container: After building the image, you can run your container with:

    docker run your-app-name
    

By ensuring that libgdiplus is installed in your Docker container, you should be able to resolve the DllNotFoundException and run your Aspose.Words application successfully on Linux.

If you continue to experience issues, consider checking for any additional dependencies that may be required for your specific use case or reviewing the official Aspose documentation for further guidance on running Aspose.Words in Docker environments.

Sources:
[1]: How to Run Aspose.Words in Docker - Aspose Documentation

I read that " If you are working in an environment where libgdiplus is necessary (for example, on Linux), you may encounter limitations in rendering metafiles due to the differences in how graphics are processed compared to Windows." so I’m looking for a solution other than adding this library to docker.

In the case of using Aspose.Words, the library implements its own Windows Metafile player to handle metafile formats across all platforms, including those that may not support GDI+. If you are working in an environment where libgdiplus is necessary (for example, on Linux), you may encounter limitations in rendering metafiles due to the differences in how graphics are processed compared to Windows.

@kishor338 Are you use the exception is thrown by Aspose.Words? That is target framework of your application? If possible, could you please create a simple console application that will allow us to reproduce the problem and provide Dockerfile that will allow us to emulate the environment? We will check the issue and provide you more information.

Aspose.Words for .NET Standard and .NET Core does not use System.Drawing to deal with graphics, so it is doubtful that the exception is thrown by Aspose.Words.

You are right. It was while generating the PDF. so its with Aspose.PDF. We are using Aspose.PDF.Drawing latest version and Aspose.Word latest version and the issue has been resolved now

@kishor338 It is perfect that you managed to resolve the problem. Please feel free to ask in case of any issues. We are always glad to help you.

1 Like