Convert TTF to WOFF2 | Java

[ ]

On this page you will find the example of using the Aspose solution for converting font format from TrueType font to Web Open Font Format version 2.

Use the following statements:

    package com.aspose.font;

    import java.nio.file.Files;
    import java.nio.file.Paths;

    import com.aspose.font.ByteContentStreamSource;
    import com.aspose.font.FileSystemStreamSource;
    import com.aspose.font.Font;
    import com.aspose.font.FontDefinition;
    import com.aspose.font.FontFileDefinition;
    import com.aspose.font.FontSavingFormats;
    import com.aspose.font.FontType;
    import com.aspose.font.TtfFont;

If you need to know the Aspose conversion fundamentals get to How to convert the font into the desired format? chapter.

Then take the next steps:

  1. Open TTF font.

        // Open ttf font
        String fontPath = Paths.get(getDataDir(), "Montserrat-Regular.ttf").toString();
        FontDefinition fontDefinition = new FontDefinition(FontType.TTF, new FontFileDefinition(new FileSystemStreamSource(fontPath)));
        Font font =;

  2. Write the output settings for the WOFF2 format.

        // Woff2 output settings
        String outPath = Paths.get(getOutputDir(), "TtfToWoff2_out1.woff2").toString();
        FileOutputStream outStream = new FileOutputStream(outPath);

  3. Make conversion from TTF to WOFF2 and save the result using saveToFormat() method of the base abstract Font class instance.

        // Convert ttf to woff2
        font.saveToFormat(outStream, FontSavingFormats.WOFF2);