Search code examples
flutterpdffonts

Trying to create a pdf in different languages in flutter


I want to print Hindi language in my PDF, I've also tried using Raleway-Regular.ttf font by Google but it also didn't worked.

final font = await rootBundle.load("fonts/ARIAL.TTF");
final ttf = pw.Font.ttf(font);

pdf.addPage(
  pw.MultiPage(
    pageFormat: PdfPageFormat.a4,
    margin: pw.EdgeInsets.all(32),
    build: (pw.Context context){
    return <pw.Widget>[
      pw.Center(
        child: pw.Text("मेन",style: pw.TextStyle(fontSize: 16,font: ttf))
      ),
      pw.SizedBox(height: 20),
      pw.Center(
      child: pw.Text("ABC",style: pw.TextStyle(fontSize: 16))
    ),
  ),
),

I'm getting following Error:

***I/flutter (24862): Cannot decode the string to Latin1. I/flutter (24862): This font does not support Unicode characters. I/flutter (24862): If you want to use strings other than Latin strings, use a TrueType (TTF) font instead. I/flutter (24862): See https://github.com/DavBfr/dart_pdf/wiki/Fonts-Management I/flutter (24862): --------------------------------------------- E/flutter (24862): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: Invalid argument (string): Contains invalid characters.: "मेन"



Solution

  • I am also facing the same error its because the fonts you are using does not support that language , if you want to use hindi language then you can use hind-regular.ttl font it supports both the fonts hindi and english.

    here is the example what i got.

    // step 1- add font locatin in pubspec

    fonts: - family: hind fonts: - asset: assets/hind.ttf

    //step 2-

    final pdf = pw.Document();

    final font = await rootBundle.load("assets/hind.ttf");
    
    final ttf = pw.Font.ttf(font);
    

    then just apply this ttf in the fontStyle

    packages i m using -

    import 'package:universal_html/html.dart' as html;

    import 'package:pdf/widgets.dart' as pw;

    universal_html: ^1.2.3 enter image description here pdf: ^1.11.1