Learn how to convert TTF to WOFF and WOFF2 using Java.
The following topics are covered in this article:
- Java API to Convert TTF to WOFF & WOFF2
- Convert TTF to WOFF using Java
- Convert TTF to WOFF2 in using Java
Learn how to convert TTF to WOFF and WOFF2 using Java.
The following topics are covered in this article:
// This code example demonstrates how to convert TTF to WOFF. | |
// TTF file path | |
String fontPath = "C:\\Files\\Montserrat-Regular.ttf"; | |
// Load the Font file | |
FileSystemStreamSource source = new FileSystemStreamSource(fontPath); | |
// Create font file definition | |
FontFileDefinition fileDefinition = new FontFileDefinition(source); | |
// Create font definition | |
FontDefinition fontDefinition = new FontDefinition(FontType.TTF, fileDefinition); | |
// Open font | |
Font font = Font.open(fontDefinition); | |
// WOFF output path | |
String outPath = "C:\\Files\\TtfToWoff_out1.woff"; | |
FileOutputStream outStream = new FileOutputStream(outPath); | |
// Convert TTF to WOFF | |
font.saveToFormat(outStream, FontSavingFormats.WOFF); |
// This code example demonstrates how to convert TTF to WOFF2. | |
// TTF file path | |
String fontPath = "C:\\Files\\Montserrat-Regular.ttf"; | |
// Load the Font file | |
FileSystemStreamSource source = new FileSystemStreamSource(fontPath); | |
// Create font file definition | |
FontFileDefinition fileDefinition = new FontFileDefinition(source); | |
// Create font definition | |
FontDefinition fontDefinition = new FontDefinition(FontType.TTF, fileDefinition); | |
// Open font | |
Font font = Font.open(fontDefinition); | |
// WOFF output path | |
String outPath = "C:\\Files\\TtfToWoff_out1.woff2"; | |
FileOutputStream outStream = new FileOutputStream(outPath); | |
// Convert TTF to WOFF | |
font.saveToFormat(outStream, FontSavingFormats.WOFF2); |