Skip to content

Instantly share code, notes, and snippets.

@aspose-font-gists
Created May 19, 2022 03:27
Convert TTF to WOFF and WOFF2 using Java

Learn how to convert TTF to WOFF and WOFF2 using Java.

The following topics are covered in this article:

  1. Java API to Convert TTF to WOFF & WOFF2
  2. Convert TTF to WOFF using Java
  3. Convert TTF to WOFF2 in using Java
// 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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment