Free Support Forum - aspose.com

Cells not rendering foreign language fonts correctly when saving to PDF

Aspose Team,

We’re seeing problems when trying to save Excel documents with foreign language fonts like
MS Gothic or MingLiu to a PDF format. Here’s an example code excerpt below with two sample files
attached that are rendering with boxes or numbers in place of the correct font. The fonts are stored in a Aspose_ForeignChar_PDFIssues.zip (25.8 KB)
sub-directory beneath the /usr/share/fonts directory (installed as follows on this Ubuntu server):

/usr/share/fonts/truetype/mssupplemental/MS-PGothic-03.ttf: MS PGothic,MS Pゴシック:style=Regular,標準
/usr/share/fonts/truetype/mssupplemental/PMingLiU-ExtB-02.ttf: PMingLiU-ExtB,新細明體-ExtB:style=Regular

Java code sample (edited to show relevant contents)

public static final String FONTS_FOLDER = "/usr/share/fonts/";

public static void demoExcelToPdf(String excelFilePath, String destFilePath) throws Exception {

    // tell Aspose where to find our fonts
    FontConfigs.setFontFolder(FONTS_FOLDER, true);

    // set Aspose loading options for this Excel file
    com.aspose.cells.LoadOptions options = new com.aspose.cells.LoadOptions();
    options.setMemorySetting(MemorySetting.MEMORY_PREFERENCE);  // try to optimize mem usage for large files
    IndividualFontConfigs fontConfigs = new IndividualFontConfigs();
    fontConfigs.setFontFolder(FONTS_FOLDER, true);
    options.setStandardFont("Arial");
    options.setFontConfigs(fontConfigs);

    // prepare the Excel spreadsheet pages for saving to PDF
    String defaultFontName = "Arial";
    boolean defaultFontNameFound = false;
    Workbook workbook = new Workbook(excelFilePath, options);
    for (int i = 0; i < workbook.getWorksheets().getCount(); i++) {
        //Get all the fonts inside the workbook
        com.aspose.cells.Font[] fnts = workbook.getFonts();

        //Log all the fonts for debugging
        for (int f = 0; f < fnts.length; f++) {
            if (!defaultFontNameFound) {
                defaultFontNameFound = true;
                defaultFontName = fnts[f].getName();  // choose the first one just to test the output
            }
            Logger.Debug("Workbook font name=" + fnts[f].getName() + ", size=" + fnts[f].getSize());
        }
    }

    // set our default PDF output font
    PdfSaveOptions saveOptions = new PdfSaveOptions();
    saveOptions.setDefaultFont(defaultFontName);
    saveOptions.setEmbedStandardWindowsFonts(false);  // to make output smaller
    saveOptions.setCheckWorkbookDefaultFont(false);
    saveOptions.setOptimizationType(PdfOptimizationType.MINIMUM_SIZE);

    // finally, save the Excel file as a PDF
    workbook.save(destFilePath, saveOptions);
}

Please advise on how to make this work for correct PDF rendering.

Thank you for your help.

Jerry

@jmuth,
I have tried the scenario by downloading the mentioned fonts and copying them to usr/share/fonts folder but could not get the desired output. Could you please share your fonts folder for our reference? We will use those fonts to reproduce the issue here.

@jmuth

Please comment the line saveOptions.setEmbedStandardWindowsFonts(false); , we only embed a subset font with the glyphs used in the source file.

Also, fonts in the /usr/share/fonts/ are loaded by default, so there is no need to set fonts again by FontConfigs.setFontFolder(FONTS_FOLDER, true); and fontConfigs.setFontFolder(FONTS_FOLDER, true);. So the code will be:

public static void demoExcelToPdf(String excelFilePath, String destFilePath) throws Exception {

// tell Aspose where to find our fonts
// FontConfigs.setFontFolder(FONTS_FOLDER, true);

// set Aspose loading options for this Excel file
com.aspose.cells.LoadOptions options = new com.aspose.cells.LoadOptions();
options.setMemorySetting(MemorySetting.MEMORY_PREFERENCE);  // try to optimize mem usage for large files
// IndividualFontConfigs fontConfigs = new IndividualFontConfigs();
// fontConfigs.setFontFolder(FONTS_FOLDER, true);
options.setStandardFont("Arial");
// options.setFontConfigs(fontConfigs);

// prepare the Excel spreadsheet pages for saving to PDF
String defaultFontName = "Arial";
boolean defaultFontNameFound = false;
Workbook workbook = new Workbook(excelFilePath, options);
for (int i = 0; i < workbook.getWorksheets().getCount(); i++) {
    //Get all the fonts inside the workbook
    com.aspose.cells.Font[] fnts = workbook.getFonts();

    //Log all the fonts for debugging
    for (int f = 0; f < fnts.length; f++) {
        if (!defaultFontNameFound) {
            defaultFontNameFound = true;
            defaultFontName = fnts[f].getName();  // choose the first one just to test the output
        }
        Logger.Debug("Workbook font name=" + fnts[f].getName() + ", size=" + fnts[f].getSize());
    }
}

// set our default PDF output font
PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.setDefaultFont(defaultFontName);
// saveOptions.setEmbedStandardWindowsFonts(false);  // to make output smaller
saveOptions.setCheckWorkbookDefaultFont(false);
saveOptions.setOptimizationType(PdfOptimizationType.MINIMUM_SIZE);

// finally, save the Excel file as a PDF
workbook.save(destFilePath, saveOptions);

}

Ashan & Peyton,

I am unable to upload the zip file for the PMingLiu font since this site’s upload complains about the size of the 15MB zip file (although the dialog that pops up says the max size is 50MB). I am uploading a zip file with the other fonts, however. As mentioned originally, we install these fonts in the /usr/share/fonts/truetype/mssupplemental directory on this server.

ExampleFontsDirForAsposeIssue_228796_2.zip (8.7 MB)

I will also apply the code changes to our code that Peyton has suggested. Please let me know if you need anything else. Thank you.

Jerry

@jmuth,
I have tested this scenario using the docker image as follows:

cat /etc/os-release

NAME="Oracle Linux Server"
VERSION="8.3"
ID="ol"
ID_LIKE="fedora"
VARIANT="Server"
VARIANT_ID="server"
VERSION_ID="8.3"
PLATFORM_ID="platform:el8"
PRETTY_NAME="Oracle Linux Server 8.3"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:oracle:linux:8:3:server"
HOME_URL="https://linux.oracle.com/"
BUG_REPORT_URL="https://bugzilla.oracle.com/"
ORACLE_BUGZILLA_PRODUCT="Oracle Linux 8"
ORACLE_BUGZILLA_PRODUCT_VERSION=8.3
ORACLE_SUPPORT_PRODUCT="Oracle Linux"
ORACLE_SUPPORT_PRODUCT_VERSION=8.3 

I copied your fonts into usr/share/fonts folder and executed the code suggested by @Peyton.Xu here. It worked absolutely fine and output pdf contains proper text as shown in the attached file.

C1.PC.00000032-Copy.pdf (105.4 KB)

Ashan,

You say that you copied our fonts into the /usr/share/fonts directory. Does this mean that the fonts won’t be found if they are in a subdirectory beneath the /usr/share/fonts directory (as in our setup where the fonts are located here: /usr/share/fonts/truetype/mssupplemental)?

Thank you for clarifying.

Jerry

@jmuth,
I tried by copying the fonts in the said folder and it works fine. All the text is properly rendered and Aspose.Cells used fonts from the subdirectories as well.

ls /usr/share/fonts -R
/usr/share/fonts:
aajohan-comfortaa truetype
/usr/share/fonts/aajohan-comfortaa:
Comfortaa-Bold.ttf Comfortaa-Light.ttf Comfortaa-Regular.ttf
/usr/share/fonts/truetype:
mssupplemental
/usr/share/fonts/truetype/mssupplemental:
MS-Gothic-01.ttf MS-PGothic-03.ttf MS-UIGothic-02.ttf

Ashan & Peyton,

I can confirm that with Peyton’s suggested code changes, these documents are now rendering the documents correctly. Thank you for your help! We appreciate it.

Jerry

@jmuth,
You are welcome.