I am trying to generate pdf in flutter application using " Pdf creation library". While generating pdf in other language it gives exception

3,361

I have had similar error when am trying to generate pdf in flutter application using “ Pdf creation library”. While generating pdf have right single quotation mark " ’ " "&#8217" in my source text.

Code with error

final String soutceString = 'My source text with " ’ "';

final Document pdf = Document(deflate: zlib.encode);

pdf.addPage(
    MultiPage(
       build: (Context context) => <Widget>[
            Paragraph(text: soutceString),
       ]
    )
);

Error log

flutter: 'package:pdf/src/font.dart': Failed assertion: line 145 pos 14: 'false':
---------------------------------------------
Can not decode the string to Latin1.
This font does not support Unicode characters.
If you want to use strings other than Latin strings, use a TrueType (TTF) font instead. 

When I found wrong symbol I just replace it.

Code with fast fix


final String sourceString = 'My source text with " ’ "';

final Document pdf = Document(deflate: zlib.encode);

sourceString.replaceAll('’', '`'),

pdf.addPage(MultiPage(
   build: (Context context) => <Widget>[
            Paragraph(text: sourceString)
        ]
    )
);

But I was afraid, of new wrong symbol come in future. I added arial.ttf fonts in to assets (arial.ttf contain my symbol) and use it. Adding assets

Best case for me

import 'package:flutter/services.dart' show rootBundle;



final String sourceString = 'My source text with " ’ "';

final Document pdf = Document(deflate: zlib.encode);

pdf.addPage(MultiPage(
     theme: Theme.withFont(
        base: Font.ttf(await rootBundle.load("assets/arial.ttf")),
        bold: Font.ttf(await rootBundle.load("assets/arial.ttf")),
        italic: Font.ttf(await rootBundle.load("assets/arial.ttf")),
        boldItalic: Font.ttf(await rootBundle.load("assets/arial.ttf")),
      ),
   build: (Context context) => <Widget>[
            Paragraph(text: sourceString)
          ]
    )
);

Share:
3,361
BlitzMan
Author by

BlitzMan

Updated on December 13, 2022

Comments

  • BlitzMan
    BlitzMan over 1 year

    This is the code using to generate pdf in other languages font:

    final Uint8List fontData = File('fonts/shivaji05.ttf').readAsBytesSync();
    final ttf = Font.ttf(fontData.buffer.asByteData());
    

    This is how I am using the defined font in the text:

    Text('साई बाबा', textScaleFactor: 2, style: new TextStyle(font: ttf)),
    

    Error log

    E/flutter (26251): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: 'package:pdf/src/font.dart': Failed assertion: line 145 pos 14: 'false': 
    E/flutter (26251): ---------------------------------------------
    E/flutter (26251): Can not decode the string to Latin1.
    E/flutter (26251): This font does not support Unicode characters.
    E/flutter (26251): If you want to use strings other than Latin strings, use a TrueType (TTF) font instead.</i>
    
  • Soon Santos
    Soon Santos over 3 years
    This solution worked indeed. You can read Custom fonts in flutter tutorial to insert the arial font in your project
  • Patrick Waweru
    Patrick Waweru about 3 years
    API changed rename Theme.withFont to ThemeData.with...