How to print Arabic letters with thermal printer using Flutter esc_pos_printer library?

Viewed 1646

I'm using a printer: EPSON TM-m30

I'm currently using :

esc_pos_printer: ^4.0.3
esc_pos_utils: ^1.0.0

When I run this code

printDemoReceipt(NetworkPrinter printer) async {
    printer.text('ا ب ت ث ج ح خ د ذ ر ز س ش ص ض ف ق ك ل م ن ه و ي');
    printer.feed(2);
    printer.cut();
    printer.disconnect();
}

It causes this error

[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: Invalid argument (string): Contains invalid characters.: "ا ب ت ث ج ح خ د ذ ر ز س ش ص ض ف ق ك ل م ن ه و ي"
_UnicodeSubsetEncoder.convert  (dart:convert/ascii.dart:88:9)

Did anyone fix this issue?

Thank you.

3 Answers

Actually I have the same issue and I believe for now the only solution is to create a PDF then covert it to an image and then you can print it.

Try to

import 'dart:convert' show utf8;

printDemoReceipt(NetworkPrinter printer) async {

    final arabicText = utf8.encode('ا ب ت ث ج ح خ د ذ ر ز س ش ص ض ف ق ك ل م ن ه و ي');

    printer.textEncoded(arabicText);
    printer.feed(2);
    printer.cut();
    printer.disconnect();
}

You have to find the character table for your printer and write the corresponding commands to let the printer know that you are printing multi byte characters such as Arabic characters. In my case, I was using sunmi printer and the only thing that worked for me was finding its character table and I wrote the commands and it worked very well. Here's a picture of what they said in the documentation. enter image description here

And this is what I did and it worked perfectly

const utf8Encoder = Utf8Encoder();
final encodedStr = utf8Encoder.convert(invoice.description);
bytes += generator.textEncoded(Uint8List.fromList([
  ...[0x1C, 0x26, 0x1C, 0x43, 0xFF],
  ...encodedStr
]));
Related