HTML to PDF conversion is failing in linux using Aspose.PDF package

We are using Aspose.Total.NET.lic license in dotnet application. When trying to convert html file to pdf using Aspose.PDF package from linux server it is throwing below error:

Error message:

The type initializer for 'Gdip' threw an exception.

StackTrace:

System.Drawing.SafeNativeMethods.Gdip.GdipCreateMatrix2(Single m11, Single m12, Single m21, Single m22, Single dx, Single dy, IntPtr& matrix)\n   at System.Drawing.Drawing2D.Matrix..ctor(Single m11, Single m12, Single m21, Single m22, Single dx, Single dy)\n   at #=zvLLzAUYTltvrLQ_ovTKNc94=..ctor(Single #=z5$6GhCY=, Single #=zoMR81us=, Single #=zKKkxzCA=, Single #=zaZ9kxqo=, Single #=zy6K1mkQ=, Single #=zyBPnB$g=)\n   at #=z92NsO8vQFnhTrMi6MMLNjYIdCLUp21zCUA==.#=zijzACKWy7UwsoNgTL8vTBx4=(SizeF #=z2f3ae8s=)\n   at #=z92NsO8vQFnhTrMi6MMLNjYIdCLUp21zCUA==.#=zCqBXh6k=(SizeF #=z2f3ae8s=)\n   at #=z_mhIgkFH9cYvZjU9s692gcZkKB5VUXnSbg==.Render(#=zTppk8ewd7c$fARK1Ss$UFWUPqNqN #=z4zoGrLI=, CancellationToken #=zeMkaCxs=)\n   at #=zpwj5XGAaoIoEYnk3qgwTvMl5sQoX.#=z1l0wXaKzQL23(#=zpal0xlZm1TysXuavNhGT364= #=zS$4HIgLu4jT4xBCcSA==, #=zZgK7g4kExoiJVjvzkglru1lg3Xi6[] #=zE6AhlBGjZiS5, #=zTppk8ewd7c$fARK1Ss$UFWUPqNqN #=z4zoGrLI=, CancellationToken #=zYCgnwPs=)\n   at #=z83lVzBXnypAA3f0hazlON55hTZEt.Render(#=zTppk8ewd7c$fARK1Ss$UFWUPqNqN #=z4zoGrLI=, CancellationToken #=zYCgnwPs=, #=zqk7PnJWchsgFEFB$8xhqLpY=[] #=zB_MSV6k=, #=z2OY2FUZ3ANy2$cuM9EvrJoA=[] #=zs6Jtchk=)\n   at #=z83lVzBXnypAA3f0hazlON55hTZEt.Render(#=zTppk8ewd7c$fARK1Ss$UFWUPqNqN #=z4zoGrLI=, TimeSpan #=zw7VFzWI=, #=ztjsxeLJtYnHfUHEmVBrAnlY=[] #=zEXhLzS8=)\n   at #=zPAH6K6IYjBtuIwxLsoxYgc$lUnG2.Render(#=zTppk8ewd7c$fARK1Ss$UFWUPqNqN #=z4zoGrLI=, #=zzGGBmwY= #=zONDdwgw=, TimeSpan #=zw7VFzWI=)\n   at #=zPAH6K6IYjBtuIwxLsoxYgc$lUnG2.Render(#=zTppk8ewd7c$fARK1Ss$UFWUPqNqN #=z4zoGrLI=, #=zzGGBmwY= #=zONDdwgw=)\n   at #=zwam3ITW9KZH23SxC_5jBNkkDmPcu.#=zY5iolZxucd8s(Stream #=zMNFFFq$YmvhH, Document #=z2OPF5ZVGYyLC, HtmlLoadOptions #=zZh_vbMhyDANm, String #=ziRJUqanIGp7I)\n   at #=zwam3ITW9KZH23SxC_5jBNkkDmPcu.#=zuCNbsd4=(Stream #=zMNFFFq$YmvhH, Document #=z2OPF5ZVGYyLC, HtmlLoadOptions #=zZh_vbMhyDANm, String #=ziRJUqanIGp7I)\n   at #=zwam3ITW9KZH23SxC_5jBNkkDmPcu.#=zuCNbsd4=(Stream #=zMNFFFq$YmvhH, Document #=z2OPF5ZVGYyLC, HtmlLoadOptions #=zZh_vbMhyDANm)\n   at Aspose.Pdf.Document.#=zj6SKKT4=(Stream #=zaz2v9Wg=, LoadOptions #=zrfBUdC4=, Boolean #=znxZXujUcWi3p)\n   at Aspose.Pdf.Document.#=zj6SKKT4=(Stream #=zaz2v9Wg=, LoadOptions #=zrfBUdC4=)\n   at Aspose.Pdf.Document..ctor(Stream input, LoadOptions options)\n   at KPMG.DocProcessor.HTML.HTMLProcessorAsposeImpl.ConvertToPDF(Stream docStream) in /App/DocProcessor/src/HTML/HTMLProcessorAsposeImpl.cs:line 11\n   at KPMG.DocProcessor.HTML.HTMLController.ConvertToPDF(IFormFile file) in /App/DocProcessor/src/HTML/HTMLController.cs:line 28\n   at lambda_method4(Closure, Object)\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfActionResultExecutor.Execute(ActionContext actionContext, IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()\n--- End of stack trace from previous location ---\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|25_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()\n--- End of stack trace from previous location ---\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope

It is working fine on windows.

@dsom

The error you are encountering, The type initializer for 'Gdip' threw an exception, typically indicates that there is an issue with the GDI+ library, which is used for graphics rendering in .NET applications. This issue often arises when running on a Linux environment, as GDI+ is primarily designed for Windows.

Here are some steps you can take to resolve this issue when converting HTML to PDF using the Aspose.PDF library on a Linux server:

  1. Install Required Libraries: Ensure that you have the necessary libraries installed on your Linux server. You may need to install libgdiplus, which is a cross-platform implementation of the GDI+ API. You can install it using the following command:

    sudo apt-get install libgdiplus
    
  2. Set Up Mono: If you are running a .NET application on Linux, make sure you have Mono installed, as it provides the necessary runtime for .NET applications. You can install Mono with:

    sudo apt-get install mono-complete
    
  3. Check Permissions: Ensure that your application has the necessary permissions to access the required libraries and resources. Sometimes, permission issues can lead to such exceptions.

  4. Use Alternative Rendering Options: If the issue persists, consider using alternative rendering options provided by Aspose.PDF. You can specify custom loading options when converting HTML to PDF, which might help bypass the GDI+ dependency.

Here is a basic example of how to convert HTML to PDF with custom loading options:

using System.IO;
using Aspose.Pdf;

public class HTMLToPDFConverter
{
    public static void Convert(string htmlFilePath, string outputPdfPath)
    {
        HtmlLoadOptions options = new HtmlLoadOptions();
        // Set any specific options if needed
        Document pdfDocument = new Document(htmlFilePath, options);
        pdfDocument.Save(outputPdfPath);
    }
}
  1. Test on a Different Environment: If possible, test your application on a different Linux distribution or a Windows environment to see if the issue is specific to your current setup.

By following these steps, you should be able to resolve the issue and successfully convert HTML to PDF on your Linux server using Aspose.PDF.

If you continue to experience problems, consider reaching out to the Aspose support forum for further assistance.

Sources:
[1]: Provide Credentials During HTML To PDF - Aspose
[2]: HTML To PDF | Aspose.PDF for .NET API Reference

Ok, I am using below code, what loading options should I try?

public Stream ConvertToPDF(Stream docStream)
{
    docStream.Position = 0;
    var pdfDocument = new Document(docStream, new HtmlLoadOptions());
    var outputStream = new MemoryStream();
    pdfDocument.Save(outputStream);
    outputStream.Position = 0;

    return outputStream;
}

At first it was throwing below exception even though libc6-dev, libgdiplus, libx11-dev were installed:

System.Drawing.SafeNativeMethods.Gdip.GdipCreateMatrix2(Single m11, Single m12, Single m21, Single m22, Single dx, Single dy, IntPtr& matrix)\n at System.Drawing.Drawing2D.Matrix

Then after updating environment variable to point to these libraries:

LD_LIBRARY_PATH /usr/lib:/usr/lib64:$LD_LIBRARY_PATH

It started giving below exception:

System.PlatformNotSupportedException: System.Drawing.Common is not supported on non-Windows platforms

Then after changing the library from Aspose.PDF to Aspose.PDF.Drawing as suggested in below post, it started working.

@dsom

Yes, Aspose.Pdf.Drawing for .NET has been launched to work under Linux like environments because it does not use System.Drawing dependency except printing feature. Nevertheless, it is nice to know that you have been able to sort this out. Please keep using the API and feel free to let us know in case you need any further information.