Cannot find any fonts installed on the system when converting from PDF to DocX

Below is my code. The code was run on a Linux server and it is impossible for me to install the fonts on the system. I have tried many methods mentioned in different topics including FontRepository.setLocalFontPaths, FontRepository.getSources()
.add(new FolderFontSource(FONT_DIR)), and FontRepository.addSystemFont(font) but still got Cannot find any fonts installed on the system. I thought FontRepository.addSystemFont will have already added the fonts to the system path???

  // ------------------------
  // Before adding system font:
  // Font arial = FontRepository.findFont("Arial"); // Raises "Font Arial was not found."
  com.aspose.pdf.FontRepository.setLocalFontPaths(ImmutableList.of(FONT_DIR));
  FontRepository.getSources().add(new FolderFontSource(FONT_DIR));
  // msttcorefonts
  List<String> fontFiles =
      ImmutableList.of("/arial.ttf", "/cour.ttf", "/georgia.ttf", "/times.ttf", "/verdana.ttf");
  for (String fontFile : fontFiles) {
    // Add system font.
    String fontDir = FONT_DIR + fontFile;
    InputStream fontStream = new FileInputStream(fontDir);
    Font font = FontRepository.openFont(fontStream, FontTypes.TTF);
    FontRepository.addSystemFont(font);
  }
  // ------------------------
  // After adding system font:
  Font arial = FontRepository.findFont("Arial");
  System.out.println("Font name: " + arial.getFontName()); // Arial
  System.out.println("IsEmbedded: " + arial.isEmbedded()); // true
  System.out.println("IsAccessible: " + arial.isAccessible()); // true
  System.out.println("IsSubset: " + arial.isSubset()); // true

  System.out.println(com.aspose.pdf.FontRepository.getLocalFontPaths());
  com.aspose.pdf.Document document = new com.aspose.pdf.Document(input.newInput());
  ByteString.Output outputStream = ByteString.newOutput();
  document.save(outputStream, com.aspose.pdf.SaveFormat.DocX);
  return outputStream.toByteString();

Below is the error trace:
class com.aspose.pdf.internal.ms.System.l6n: Cannot find any fonts installed on the system.

com.aspose.pdf.internal.l25l.lv.lI(Unknown Source)
com.aspose.pdf.internal.l25l.lv.lI(Unknown Source)
com.aspose.pdf.internal.l25l.lv.lI(Unknown Source)
com.aspose.pdf.internal.l99t.lc.lI(Unknown Source)
com.aspose.pdf.internal.l99t.l0t.lI(Unknown Source)
com.aspose.pdf.internal.l0j.l0l.lI(Unknown Source)
com.aspose.pdf.internal.l0j.l0l.lI(Unknown Source)
com.aspose.pdf.internal.l0j.le.lI(Unknown Source)
com.aspose.pdf.internal.l0u.l0if.lI(Unknown Source)
com.aspose.pdf.internal.l0j.lc.lI(Unknown Source)
com.aspose.pdf.internal.l0j.le.lI(Unknown Source)
com.aspose.pdf.internal.l0u.lk.lI(Unknown Source)
com.aspose.pdf.internal.l0j.lu.lI(Unknown Source)
com.aspose.pdf.internal.l0j.le.lI(Unknown Source)
com.aspose.pdf.internal.l0u.l0l.lI(Unknown Source)
com.aspose.pdf.internal.l0j.lv.lj(Unknown Source)
com.aspose.pdf.internal.l0j.lv.lI(Unknown Source)
com.aspose.pdf.internal.l0j.le.lI(Unknown Source)
com.aspose.pdf.internal.l0u.lh.lI(Unknown Source)
com.aspose.pdf.internal.l99t.lk.lI(Unknown Source)
com.aspose.pdf.internal.l0u.lh.lI(Unknown Source)
com.aspose.pdf.internal.l99t.lk.lf(Unknown Source)
com.aspose.pdf.internal.l99t.le.lk(Unknown Source)
com.aspose.pdf.internal.l15p.lv.lI(Unknown Source)
com.aspose.pdf.internal.l15p.lb.lf(Unknown Source)
com.aspose.pdf.internal.l15t.lj.lI(Unknown Source)
com.aspose.pdf.internal.l0j.lf.lI(Unknown Source)
com.aspose.pdf.l4j.lI(Unknown Source)
com.aspose.pdf.l4j.lI(Unknown Source)
com.aspose.pdf.ADocument.lj(Unknown Source)
com.aspose.pdf.ADocument.lj(Unknown Source)
com.aspose.pdf.ADocument.lf(Unknown Source)
com.aspose.pdf.Document.lf(Unknown Source)
com.aspose.pdf.ADocument.save(Unknown Source)
com.aspose.pdf.Document.save(Unknown Source)

I have confirmed that the font files do exist and can be opened successfully. What should I do?

Thanks!

Here is the link to the PDF I’m trying to convert. I don’t think there’s anything special about this PDF. simple.pdf (29.5 KB)

@yiaayz

Can you please try placing fonts in “/usr/share/fonts/truetype/msttcorefonts” directory and use 21.7 version of the API to test the case again. In case you still face any issue, please let us know. We will further proceed to assist you accordingly.

I tried placing the fonts under “/usr/share/fonts/truetype/msttcorefonts” on a local server and it worked, but it is not feasible for us to place the font under that directory if we were to deploy the job on a remote server. Would it be possible for us to override the default font directory and customize the path to the fonts?

Thank you!

I also tried the solution in PDFJAVA_39210 by substituting all the fonts to “Arial”

public static class PDFJAVA_39210FontSubstitute extends CustomFontSubstitutionBase {
  @Override
  public boolean trySubstitute(
      CustomFontSubstitutionBase.OriginalFontSpecification originalFontSpecification,
      com.aspose.pdf.Font[] substitutionFont) {
    logger.atSevere().log(originalFontSpecification.getOriginalFontName());
    substitutionFont[0] = FontRepository.findFont("Arial");
    return true;
  }
}
...
FontRepository.getSubstitutions().add(new PDFJAVA_39210FontSubstitute());

I logged the font substitution with

  document.FontSubstitution.add(
      new com.aspose.pdf.Document.FontSubstitutionHandler() {
        public void invoke(Font font, Font newFont) {
          // print substituted FontNames into console
          logger.atSevere().log(
              "Warning: Font "
                  + font.getFontName()
                  + " was substituted with another font -> "
                  + newFont.getFontName());
        }
      });

And got the following log:
Font Calibri was substituted with another font -> Arial

It seems that FontRepository.findFont(“Arial”) is able to open the font fine and I have checked that the file was indeed opened correctly, but I am still getting “Cannot find any fonts installed on the system.” on document.save(…).

This issue has been hindering our product process. Could you please take a look asap? Thanks!

I would also love to hear about the root cause of this issue. I’ve already done addSystemFont but am still getting this error, which is counter-intuitive and confusing. Your help is really appreciated.

@yiaayz

After setting the path using FontRepository.setLocalFontPaths() method, please try to add the below line of code:

com.aspose.pdf.FontRepository.loadFonts()

And check if the issue still persists at save() method. In case it does not help, please share the complete environment details of your server so that we can further investigate the issue at our end and address it accordingly.

Added com.aspose.pdf.FontRepository.loadFonts() after FontRepository.setLocalFontPaths() but it still doesn’t work. Still got class com.aspose.pdf.internal.ms.System.l6n: Cannot find any fonts installed on the system. The task is run on a Linux server that does not have msttcorefonts installed in /usr/share/fonts/truetype/msttcorefonts. The job runner does not have permission to access files under /usr/share/fonts but has permission to read files under the custom directory. All msttcorefonts are installed in the custom directory.

I also tried both setting FontRepository.setThreadStaticConfigEnabled(false); and FontRepository.setThreadStaticConfigEnabled(true);. It still doesn’t work.

Also, I would like to share this topic with my colleagues. Is there any way I can make it public?

Thanks.

@yiaayz

We have made the topic public and now your colleagues can access it. Furthermore, we need to investigate this entire scenario in details. For further investigation, we have logged a ticket as PDFJAVA-40748 in our issue management system. We will look into details of the issue and let you know as soon as the ticket is resolved. Please be patient and give us some time.

We apologize for the inconvenience caused.