Cannot open shared object file gdiplus.dll, libgdiplus.dll

In Azure Linux Container getting runtime error. Working fine in Window 11 system

Error at: using (var document = new Aspose.Pdf.Document(stream, options))

// Method to generate PDF from HTML string and return byte array
private static byte[] GeneratePdfFromHtml(string htmlString)
{
    var options = new Aspose.Pdf.HtmlLoadOptions()
    {
        PageInfo = new Aspose.Pdf.PageInfo()
        {
            Width = Aspose.Pdf.PageSize.PageLetter.Width,
            Height = Aspose.Pdf.PageSize.PageLetter.Height,
            IsLandscape = false,
            Margin = new Aspose.Pdf.MarginInfo(0, 0, 0, 0) // Set margins
        },
        HtmlMediaType = Aspose.Pdf.HtmlMediaType.Screen, // Set HTML media type as screen
        IsPriorityCssPageRule = true, // Prioritize CSS page rules
        PageLayoutOption = Aspose.Pdf.HtmlPageLayoutOption.None
    };
    //Aspose.Pdf.Drawing.doc doc = new Aspose.Pdf.Drawing(options);
    using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(htmlString)))
    {
        using (var document = new Aspose.Pdf.Document(stream, options))
        {
            document.Flatten();// Flatten the PDF to make it non-editable
            using (MemoryStream memoryStream = new())
            {
                document.Save(memoryStream);
                return memoryStream.ToArray();
            }
        }
    }
}
8/libgdiplus.dll.so: cannot open shared object file: No such file or directory\\n/usr/share/dotnet/shared/Microsoft.NETCore.App/9.0.8/gdiplus.dll: cannot open shared object file: No such file or directory\\n/usr/share/dotnet/shared/Microsoft.NETCore.App/9.0.8/libgdiplus.dll: cannot open shared object file: No such file or directory\\n\",\"StackTrace\":\"   at Windows.Win32.PInvokeCore.GdiplusStartup(UIntPtr* token, GdiplusStartupInput* input, GdiplusStartupOutput* output)\\n   at Windows.Win32.PInvokeCore.GdiplusStartup(UIntPtr* token, GdiplusStartupInput* input, GdiplusStartupOutput* output)\\n   at Windows.Win32.Graphics.GdiPlus.GdiPlusInitialization.Init()\\n   at Windows.Win32.Graphics.GdiPlus.GdiPlusInitialization..cctor()\",\"InnerFailure\":null,\"IsNonRetriable\":false},\"IsNonRetriable\":false},\"IsNonRetriable\":false},\"IsNonRetriable\":false}","@tr":"7a8f8254187543b3405691d12c71c5d7","@sp":"0a14e0d260ea329d","functionName":"DFunction","invocationId":"a759e3ec30-fd0ce8e870ec","EventId":{"Id":2,"Name":"InvocationError"},"SourceContext":"Microsoft.Azure.Functions.Worker.FunctionsApplication","AzureFunctions_InvocationId":"a759e3ee-5e34-4ec","AzureFunctions_FunctionName":"DFunction"}

Thanks

@innovammir

It looks like you’re encountering a runtime error related to the gdiplus.dll or libgdiplus.dll when running your application in an Azure Linux container. This issue typically arises because the required libraries for GDI+ are not installed in the Linux environment, which is different from your Windows setup where these libraries are available by default.

Cause:

The absence of the libgdiplus library in your Azure Linux container is causing the runtime error.

Solution:

To resolve this issue, you need to install the libgdiplus library in your Azure Linux container. You can do this by adding the following command to your Dockerfile or executing it in your container:

apt-get update && apt-get install -y libgdiplus

Next Steps:

  1. Verify Installation: After installation, verify that libgdiplus is correctly installed by running:

    ldconfig -p | grep libgdiplus
    
  2. Rebuild and Deploy: Once you have installed the library, rebuild your container and redeploy your application to Azure.

  3. Test the Application: After redeployment, test your application again to see if the issue is resolved.

Closing Line:

Your existing code for generating a PDF from HTML should work fine once the library is installed. If you have any further questions or run into other issues, please feel free to ask.

Hi,

The guidance above thinks that we’re using an azure linux container, but it’s dotnet/aspnet and azure-functions/dotnet-isolated, which are debian containers (bookworm-slim).
No matter what library we use, it’s still trying to use Windows components. That’s not a viable option for containers
Maybe you could provide some help beyond the AI response.

Thanks

@innovammir

Could you try installing and using Aspose.Pdf.Drawing for .NET instead of Aspose.PDF for .NET? Please ensure you uninstall the previous Aspose.PDF for .NET package before installing Aspose.Pdf.Drawing. This package is specifically designed to work in non-Windows environments and does not rely on the System.Drawing.Common dependency. If you encounter any further issues, feel free to let us know.

Hi Asad,
Thanks for the reply. Tried with a new project with just Aspose.Pdf.Drawing, however it is still giving same error.

We did included libdgiplus package in container, though it is giving error
iled with an unhandled exception: System.Drawing.Common is not supported on non-Windows platforms. See https://aka.ms/systemdrawingnonwindows for more information.

While installing Aspose.Pdf.Drawing I could see that it is referring to System.Drawing .Common

image.png (132.8 KB)

Kind regards
M Mir

@innovammir

Aspose.Pdf.Drawing was released specifically for non-Windows environments. It uses System.Drawing.Common only for printing like features in the API. Other than that it does not rely on it. Nevertheless, we will surely investigate why you are facing this kind of error despite using Aspose.Pdf.Drawing. Can you please share a sample console application for our reference in .zip format? We will use it to replicate the issue in our environment and address it accordingly.

Hi Asad,
Thanks for getting back, please see attached .Net Application for generating Pdf. It works fine locally, but if you try to run it in Container then it gives the error.
To run locally please add /swagger/index.html e.g. http://localhost:5219/swagger/index.html, you could view the endpoints.

image.png (12.7 KB)

MVPAsposeDrawingExternal.zip (717.2 KB)

Kind regards,
M Mir

@innovammir

Thanks for providing requested details and sample project. We are generating an investigation ticket in our issue management system to address the issue and will be sharing the ticket ID with you shortly.

@innovammir

We have logged below investigation ticket in our issue management system to further analyze the scenario.

  • PDFNET-60619

We will look into its details and keep you posted with the status of its correction. Please be patient and spare us some time. We are sorry for the inconvenience.

Hi Asad,

Thanks for your help. We have managed to get this issue resolved.

We have taken following steps

  1. Removed all Aspose packages from the solution
  2. Added only Aspose.PDF.Drawing (version 25.8.0)
  3. Create a master container image with command to install libgdiplus
  4. Build the final container image that included master container image and our .NET Core solution (version 9) build

Kind regards,
M Mir

@innovammir

It is nice to know that your issue has been resolved. It could be happening because of some traces from Aspose.PDF for .NET that you were using before. It must have been resolved when you cleaned the solution and installed everything from scratch. Nevertheless, please feel free to create a new topic in case you need any kind of assistance.