The following is excellent instruction. This is helpful information.
However, I can't understand how to create the TTF file data by the getFont method. There is no "writeAsBytes" statement. This means the TTF file is always empty.
In what cases is the following condition TRUE?
// Line 11 of getFont
if (fontBytes != null && fontBytes.isNotEmpty) {
In the below KB documentation, we are getting the font from the Google fonts package in Flutter. The Google fonts package fetches the font files via HTTP at runtime and caches them in the application’s file system. In this article, we have used cached files to render the Unicode text in a PDF document. The reported problem is due to the Flutter Google fonts package being updated. And please make sure the device/emulator internet connectivity is properly connected or not. If not, please connect to the internet and try the below code snippet on your end and let us know the result.
Please refer to the below code snippet,
Future<PdfFont> getFont(TextStyle style) async {
//Get the external storage directory
Directory directory = await getApplicationSupportDirectory();
//Create an empty file to write the font data
File file = File('${directory.path}/${style.fontFamily}.ttf');
if (!file.existsSync()) {
List<FileSystemEntity> entityList = directory.listSync();
for (FileSystemEntity entity in entityList) {
if (entity.path.contains(style.fontFamily!)) {
file = File(entity.path);
break;
}
}
}
List<int>? fontBytes;
//Check if entity with the path exists
if (file.existsSync()) {
fontBytes = await file.readAsBytes();
}
if (fontBytes != null && fontBytes.isNotEmpty) {
//Return the google font
return PdfTrueTypeFont(fontBytes, 12);
} else {
//Return the default font
return PdfStandardFont(PdfFontFamily.helvetica, 12);
}
}