Converting to PDF raises Exception about fonts

Hi.

I tried to find the same error description, but can’t. I sure, that it is widespread error:

we tried to convert xlsx book with font Calibri underlined to PDF in Linux-based Docker container and got error

Caused by: java.lang.InternalError: java.lang.reflect.InvocationTargetException
2025-04-14 10:06:09     at java.desktop/sun.font.FontManagerFactory$1.run(FontManagerFactory.java:87)
2025-04-14 10:06:09     at java.base/java.security.AccessController.doPrivileged(AccessController.java:318)
2025-04-14 10:06:09     at java.desktop/sun.font.FontManagerFactory.getInstance(FontManagerFactory.java:75)
2025-04-14 10:06:09     at java.desktop/java.awt.Font.<init>(Font.java:642)
2025-04-14 10:06:09     at java.desktop/java.awt.Font.createFont(Font.java:1250)
2025-04-14 10:06:09     at com.aspose.cells.a.b._h.a(Unknown Source)
2025-04-14 10:06:09     at com.aspose.cells.a.b._h.c(Unknown Source)

on Windows platfrom we don’t have the same error. Could you provide the way, when we can set linux OS font directory with fonts, that we are providing with Aspose.Cells

Best regards

@makarovalv

To resolve the font-related exception you are encountering when converting an XLSX file to PDF in a Linux-based Docker container, you can specify a custom font directory that contains the required fonts. This is particularly important for fonts like Calibri, which may not be installed by default in your Linux environment.

Here’s how you can set up a custom font directory in your application:

  1. Create a Directory for Fonts: First, ensure you have a directory that contains the necessary font files (e.g., Calibri). You can name this directory CustomFonts.

  2. Specify the Font Directory in Your Code: Use the IndividualFontConfigs class to set the font folder. This will allow your application to use the fonts from the specified directory during the rendering process.

Here’s a sample code snippet in C# that demonstrates how to set the custom font directory:

using System;
using Aspose.Cells;

namespace Aspose.Cells.Examples.CSharp.Fonts
{
    class SpecifyCustomFontDirectory
    {
        public static void Main()
        {
            // Path to your custom font directory.
            string customFontsDir = "path/to/your/CustomFonts";

            // Specify individual font configs for the custom font directory.
            IndividualFontConfigs fontConfigs = new IndividualFontConfigs();
            fontConfigs.SetFontFolder(customFontsDir, false);

            // Specify load options with font configs.
            LoadOptions opts = new LoadOptions(LoadFormat.Xlsx);
            opts.FontConfigs = fontConfigs;

            // Load the Excel file with the specified font configs.
            Workbook wb = new Workbook("path/to/your/file.xlsx", opts);

            // Save to PDF format.
            wb.Save("output.pdf", SaveFormat.Pdf);

            Console.WriteLine("PDF conversion executed successfully.");
        }
    }
}

In this code:

  • Replace "path/to/your/CustomFonts" with the actual path to your custom font directory.
  • Ensure that the directory contains the required font files.

By following these steps, you should be able to avoid the font-related exceptions during the PDF conversion process in your Linux environment. If you continue to experience issues, make sure that the font files are correctly placed in the specified directory and that your application has the necessary permissions to access them.

I’ve already set Property

System.setProperty("Aspose.Cells.FontDirExc", pathToFontDir);

Isn’t it enough

@makarovalv,

Could you please try to run the following line of code (without involving or using Aspose.Cells for Java) on your linux environment:

java.awt.Font[] fonts = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();

Do you get an exception? If so, give us complete error stack trace.

If you are getting an exception something like, java.lang.RuntimeException: Fontconfig head is null, suggests an issue with font configuration, particularly in a headless environment where no graphical user interface (GUI) is available. This is common in server environments where the system cannot access the default fonts or a graphical subsystem, such as when running in Docker containers or cloud-based servers.

To resolve this issue, you have a few options:

1). Run in Headless Mode
Since you’re likely running in a headless environment, you should configure Java to run in headless mode. Headless mode allows Java to operate without requiring access to a display or graphics environment, which is essential when running in environments like Azure or Docker.

You can enable headless mode by adding the following system property at the start of your Java application:

java

System.setProperty("java.awt.headless", "true");

Or set it via the command line when launching the JVM:

-Djava.awt.headless=true

This prevents Java from attempting to use any GUI-related classes, like font configuration, which is the root of the issue.

2). Install the Necessary Fonts
If you’re working in an environment where you can install packages, you can install relevant font-related packages to ensure that basic fonts are available. For Linux systems, you could install font packages such as:

sudo apt-get install -y fontconfig

On some systems, it might also be necessary to install specific fonts, such as:

sudo apt-get install -y ttf-dejavu-core

Hope, this helps a bit.

@makarovalv ,

Also, please check the document: