Getting TypeInitializationException while converting an HTML to PDF

Hi Team,

I’m trying to generate a PDF by passing an HTML stream to constructor. But it seems that it fails to do so. It works fine on local but fails on server(Linux).

Following other threads which asks to get rid off Systems.Drawing.Common I already installed follwing:

  1. Aspose.PDF.Drawing
  2. SkiaSharp
  3. SkiaSharp.NativeAssets.Linux

Code which causing the issue:

public static void ConvertHtmlDocToPDFDoc(byte[] doc, out Document pdfDocument)
{
    //TODO: Added it to check build issue
 
    RegisterFonts();
 
    using var htmlStream = new MemoryStream(doc);
    var loadOptions = PDFDocumentHelper.GetHtmlLoadOptions();
    pdfDocument = new Document(htmlStream, loadOptions);
}

public static void RegisterFonts()
{
     var fontPath = System.IO.Path.Combine(AppContext.BaseDirectory, "Resources", "Fonts");
     if (Directory.Exists(fontPath))
     {
         FontRepository.Sources.Add(new FolderFontSource(fontPath));
     }
     else
     {
         throw new DirectoryNotFoundException($"Font folder not found: {fontPath}");
     }
}

public static HtmlLoadOptions GetHtmlLoadOptions()
{
     return new HtmlLoadOptions
     {
         InputEncoding = "UTF-8",
         PageInfo =
         {
             Margin = new MarginInfo(45,45,45,45),
             Width = PageSize.A4.Width,
             Height = PageSize.A4.Height,
         },
         IsEmbedFonts = true
     };
}

Error message is:

System.TypeInitializationException:
at System.Drawing.SafeNativeMethods+Gdip.GdipCreateMatrix2 (System.Drawing.Common, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51)
at System.Drawing.Drawing2D.Matrix…ctor (System.Drawing.Common, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51)
at #=zkPxZAbpo_XojO$zB1bq57VA=…ctor (Aspose.PDF, Version=25.8.0.0, Culture=neutral, PublicKeyToken=716fcc553a201e56)
at #=zPqRjsWokP$$ZAZmvsF33Gi6LBp2Jruui9g==.#=z6$ONzYzl9FqDdsXna3CQWpI= (Aspose.PDF, Version=25.8.0.0, Culture=neutral, PublicKeyToken=716fcc553a201e56)
at #=zPqRjsWokP$$ZAZmvsF33Gi6LBp2Jruui9g==.#=zjdaNgqo= (Aspose.PDF, Version=25.8.0.0, Culture=neutral, PublicKeyToken=716fcc553a201e56)
at #=z5$Srdeyr06Qc95XCXHeahHpwXBJjsXQrUCKNha8=.Render (Aspose.PDF, Version=25.8.0.0, Culture=neutral, PublicKeyToken=716fcc553a201e56)
at #=zNP9TNAY4vUJQDQx3ivlFeK_yRwsh.#=z6wvWxUb107bB (Aspose.PDF, Version=25.8.0.0, Culture=neutral, PublicKeyToken=716fcc553a201e56)
at #=zTwZXpP282jvPo2MGCxGnKhbZTabI.Render (Aspose.PDF, Version=25.8.0.0, Culture=neutral, PublicKeyToken=716fcc553a201e56)
at #=zTwZXpP282jvPo2MGCxGnKhbZTabI.Render (Aspose.PDF, Version=25.8.0.0, Culture=neutral, PublicKeyToken=716fcc553a201e56)
at #=zPAH6K6IYjBtuIwxLsoxYgc$lUnG2.Render (Aspose.PDF, Version=25.8.0.0, Culture=neutral, PublicKeyToken=716fcc553a201e56)
at #=zPAH6K6IYjBtuIwxLsoxYgc$lUnG2.Render (Aspose.PDF, Version=25.8.0.0, Culture=neutral, PublicKeyToken=716fcc553a201e56)
at #=zuu4MA6QdJMlYCxyZJDKSb2vdH2WZ.#=z0vF5BzEBoo5i (Aspose.PDF, Version=25.8.0.0, Culture=neutral, PublicKeyToken=716fcc553a201e56)
at #=zuu4MA6QdJMlYCxyZJDKSb2vdH2WZ.#=z$OFy7Ag= (Aspose.PDF, Version=25.8.0.0, Culture=neutral, PublicKeyToken=716fcc553a201e56)
at #=zuu4MA6QdJMlYCxyZJDKSb2vdH2WZ.#=z$OFy7Ag= (Aspose.PDF, Version=25.8.0.0, Culture=neutral, PublicKeyToken=716fcc553a201e56)
at Aspose.Pdf.Document.#=zUO6kUDs= (Aspose.PDF, Version=25.8.0.0, Culture=neutral, PublicKeyToken=716fcc553a201e56)
at Aspose.Pdf.Document.#=zUO6kUDs= (Aspose.PDF, Version=25.8.0.0, Culture=neutral, PublicKeyToken=716fcc553a201e56)
at Aspose.Pdf.Document…ctor (Aspose.PDF, Version=25.8.0.0, Culture=neutral, PublicKeyToken=716fcc553a201e56)
at Deloitte.APT.Infrastructure.Common.PDFHelpers.PDFDocumentHelper.ConvertHtmlDocToPDFDoc (Deloitte.APT.Infrastructure.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null:

@tupol

It looks like you’re encountering a TypeInitializationException when trying to convert HTML to PDF on a Linux server, while it works fine locally. This issue often arises due to missing dependencies or configuration differences between environments.

Cause: The error may be due to missing libraries, font availability, or permission issues on the Linux server.

Solution: Here are some steps you can take to troubleshoot and resolve the issue:

  1. Ensure All Dependencies Are Installed: Since you’re using Aspose.PDF, make sure that all required libraries are installed on your Linux server. You mentioned installing Aspose.PDF.Drawing, SkiaSharp, and SkiaSharp.NativeAssets.Linux, which is great. However, you also need to ensure that libgdiplus is installed, as it is crucial for graphics operations in .NET applications on Linux.

    You can install it using:

    sudo apt-get install libgdiplus
    
  2. Check Font Availability: The error may also be related to font issues. Ensure that the fonts you are trying to use are available on the server. You can add a fallback mechanism in your code to specify a default font if the desired one is not found.

  3. Verify Permissions: Ensure that the application has the necessary permissions to access the directories where fonts and resources are stored. This includes the path you specified in your RegisterFonts method.

  4. Test with Minimal Code: Simplify your code to isolate the issue. Try creating a basic PDF document without any HTML conversion to see if the library works correctly in that context.

  5. Check for Environment Differences: Since it works locally, compare the .NET runtime versions and configurations between your local environment and the server. Ensure they match.

Here’s a minimal code snippet to test PDF creation without HTML conversion:

using Aspose.Pdf;

public static void CreateSimplePdf()
{
    Document pdfDocument = new Document();
    pdfDocument.Pages.Add();
    pdfDocument.Save("output.pdf");
}

Run this code on your server to see if it executes without errors. If it does, the issue likely lies in the HTML conversion process.

Next Steps: Please follow the steps outlined above and let me know if these steps help or if you need further assistance! I’m here to help you resolve this issue.