Convert WOFF2 to WOFF | Java

Contents
[ ]

From this article, you will find out how to convert fonts from WOFF2 to WOFF format with the help of Aspose solution.


First, you need to iuse the following statements:

 
    package com.aspose.font;

    import java.io.FileOutputStream;
    import java.io.IOException;
    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;

Make sure you’ve learned the conversion fundamentals in How to convert the font into the desired format? section.

Then to fulfil WOFF2 to WOFF conversion you will need to take these three steps:

  1. Open Woff2 font file.

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

  2. Write the output setting for the WOFF format.

     
        // Woff output settings
        String outPath = Paths.get(getOutputDir(), "Woff2ToWoff_out1.woff").toString();
        FileOutputStream outStream = new FileOutputStream(outPath);

  3. Convert and save the font result.

     
        // Convert woff2 to woff
        font.saveToFormat(outStream, FontSavingFormats.WOFF);