Regarding How to draw Unicode text in a PDF file

Viewed 24
1 Answers

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);
}

}

Note: I work for Syncfusion.

Regards,

Gowthamraj K

Related