SpreadJS 14
Import and Export Reference / Export to PDF / Use Custom Font for PDF Export
In This Topic
    Use Custom Font for PDF Export
    In This Topic

    The Export to PDF feature in SpreadJS allows users to embed both standard fonts and custom fonts. If users don't want to use the standard fonts while exporting to PDF, they can use the custom fonts.

    When custom fonts are used, PDF is embedded as the subset of the font in the file. But users must embed the font file while performing the export operation.

    In order to register a custom font for exporting PDF, you can use the registerFont method of the PDFFontManager class. You can also provide a custom font file for the specific font string by using the fallbackFont method of the PDFFontManager class.

    Note: SpreadJS doesn't provide support for exporting the .ttc fonts. Only the True Type Font (.ttf fonts) and Open Type Font (.otf) can be exported successfully. If users want to use .ttc fonts, then they can convert them to .ttf using online converters and then use them while exporting to PDF.

    Using Code

    This example shows how to use the registerFont method to register a custom font.

    JavaScript
    Copy Code
    var font = {
        normal: fonts['SIMSUN.ttf']
    };
    GC.Spread.Sheets.PDF.PDFFontsManager.registerFont('SimSun', font);
    

    Using Code

    This example shows how to use the fallbackFont method to provide a custom font file for the specific font string.

    JavaScript
    Copy Code
    GC.Spread.Sheets.Print.PDFFontsManager.fallbackFont = function (font) {
        var fontInfoArray = font.split(' '), fontName = fontInfoArray[fontInfoArray.length - 1];
        if (fontName === 'Calibri') {
            return Calibri.ttf(encode with base64);
        }
        return Arial.ttf(in ArrayBuffer)
    }
    

    Note: While using custom font for PDF export, it is necessary to ensure that the font contains all the charsets that you have used. Not doing so can make your PDF file non-readable.