Font Issue on Linux machine

Hi,

I’m using Arial font in my .docx template, and everything works correctly when generating the output PDF on a Windows environment. However, after hosting the application on a Linux server, the output PDF no longer displays the Arial font correctly.

To address this, I preloaded the font in my code as follows:

csharp

CopyEdit

var fontPath = Path.Combine(Directory.GetCurrentDirectory(), "prerequisites/fonts");
FontSettings.DefaultInstance.SetFontsFolder(fontPath, recursive: true);

in prerequisites/fonts i put all required font and deployed in solution.

Despite this, the output PDF still does not render using Arial. Could you please suggest a solution to ensure Arial is correctly applied when running on Linux?

Thank you.

@lalit2207

To ensure that the Arial font is correctly applied when generating PDFs on a Linux server, you can follow these steps:

  1. Verify Font Installation: Make sure that the Arial font is installed on your Linux server. Unlike Windows, Linux may not have Arial installed by default. You can install the Microsoft core fonts package, which includes Arial, using the following command:

    sudo apt-get install ttf-mscorefonts-installer
    
  2. Set Font Folder: You mentioned that you are preloading the font in your code. Ensure that the path you are using to set the font folder is correct and that it contains the Arial font files. You can set the font folder in your code like this:

    var fontPath = Path.Combine(Directory.GetCurrentDirectory(), "prerequisites/fonts");
    FontSettings.DefaultInstance.SetFontsFolder(fontPath, recursive: true);
    
  3. Check Font Availability: After setting the font folder, you can check if the Arial font is available in your application. You can do this by retrieving the available fonts from the font settings:

    FontSourceBase[] fontSources = FontSettings.DefaultInstance.GetFontsSources();
    foreach (var source in fontSources)
    {
        var availableFonts = source.GetAvailableFonts();
        foreach (var font in availableFonts)
        {
            Console.WriteLine(font.FullFontName);
        }
    }
    
  4. Font Substitution: If Arial is still not rendering correctly, you can set a default font substitution rule. This will ensure that if Arial is not found, another font can be used as a fallback:

    FontSettings.DefaultInstance.SubstitutionSettings.DefaultFontSubstitution.DefaultFontName = "Arial";
    
  5. Debugging Font Issues: Implement a warning callback to capture any font substitution warnings. This will help you identify if Arial is being substituted with another font:

    doc.WarningCallback = new HandleDocumentWarnings();
    

By following these steps, you should be able to resolve the font issue and ensure that Arial is correctly applied when generating PDFs on your Linux server. If you continue to experience issues, consider checking the font files for corruption or permissions that may prevent them from being accessed.

If you need further assistance, feel free to ask!

@lalit2207 Usually, the such problems occur because the fonts used in your input document are not available on the machine where document is converted to PDF. The fonts are required to build document layout. If Aspose.Words cannot find the font used in the document, the font is substituted . This might lead into fonts mismatch and document layout differences due to the different fonts metrics. You can implement IWarningCallback to get notifications when font substitution is performed.
Please see our documentation to learn where Aspose.Words looks for fonts:
https://docs.aspose.com/words/net/specifying-truetype-fonts-location/

In your code you already set the folder with fonts, please make sure the folder is accessible. To get more information you can set FolderFontSource and specify FontSourceBase.WarningCallback property to get notification about problems upon reading fonts from the font source.