Search code examples
flutterdartpdf-generation

Pdf.dart package implement fontFallback to print emojis


I tried without success many solutions proposed on the net I am trying to print texts (stored in a local database) which include emojis (smatphone origin) and special french caracters (ie Œ ) I can't solve the problem of "Future" when embedding a fontFallback... My code :



import 'package:google_fonts/google_fonts.dart';

static Future emoji() async =\> await PdfGoogleFonts.notoColorEmoji();

static pw.Widget cell(pw.Context context, int index) {
String texte = SQLHelper.listeItemsRepas()\[index\];

    // Text including some emojis
    
    
    // Create a custom TextStyle with fallback fonts
    pw.TextStyle(
      fontFallback: emoji(),
    );
    return pw.Container(
      constraints: const pw.BoxConstraints.tightFor(
          width: double.infinity, height: 22.0),
      padding: const pw.EdgeInsets.all(3),
      child: pw.Text(
        texte,
        style: const pw.TextStyle(
            fontSize: 9),
        textAlign: pw.TextAlign.left,
        softWrap: true,
        maxLines: 6,
    
        //  style: style,
      ),
    );

}

    // Text including some emojis
    

Can you give me the correct code ?


Solution

  • I placed instructions inside the PageTheme as follows (hope it's understable)

    Future<pw.PageTheme> myPageTheme(PdfPageFormat format) async {
    
    final icons = await rootBundle.load("assets/Materialicons.ttf");
    final noto = await rootBundle.load("assets/NotoColorEmoji-emojicompat.ttf");
    final alumni = await rootBundle.load("assets/AlumniSans-Regular.ttf");
    
      final iconsTtf = pw.Font.ttf(icons);
      return pw.PageTheme(
        pageFormat: PdfPageFormat.a4,
        orientation:pw.PageOrientation.portrait,
        margin: const pw.EdgeInsets.only(
            top: 50.0,
            bottom: 40.0,
            left: 55.0,
            right: 55.0
            ),
        theme: pw.ThemeData.withFont(
          base:   pw.Font.ttf(alumni),
          icons: iconsTtf,
          fontFallback: [pw.Font.ttf(noto)]
        ),
      );
    }
    

    and it worked. @JavierGarcia:Let me know if it's not clear.