Trying to create a pdf in different languages in flutter

Viewed 3970

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.: "मेन"


4 Answers

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

Some issues are there with Unicode translations . Please take care of it https://github.com/DavBfr/dart_pdf/issues/198

You can use syncfusion pdf library (You need to buy subscription). You will get all kind of support including search,rtl, unicode support .

final  font = await rootBundle.load("assets/fonts/hind.ttf");
final  ttf = Font.ttf(font);
final pdf = pw.Document();
pw.Text(
  'Your text',
  textDirection:TextDirection.rtl, // If you need in opp direction like urdu
  style: pw.TextStyle(
  font: ttf,))

If someone still has this problem

 final data = await rootBundle.load('assets/fonts/opensans_regular.ttf');

 final  dataint = data.buffer.asUint8List(data.offsetInBytes,data.lengthInBytes);

 final  PdfFont  font  =  PdfTrueTypeFont (date,12);

Most of the language not works well with the flutter pdf plugin

In Tamil language, I face problems when two characters merge.

so, i modified the tff with available tools and replaced that character with that character. as the result, i can able to get the output

String path = 'assets/custom_Anand_MuktaMalar.ttf';
pw.Font? tamilFont = pw.Font.ttf(await rootBundle.load(path));
pw.Text('உவவுமதி உருவின் ஓங்கல் வெண்குடை'.toPrintPdf)

pdf.addPage(
    pw.Page(
        theme: pw.ThemeData(defaultTextStyle: pw.TextStyle(font: tamilFont)),
  ));

for further ref, you can see my project: https://github.com/anandsubbu007/anand-work/tree/BLOC/tamil_pdf

enter image description here

Related