Can we print Marathi language text using esc_pos_printer: ^4.1.0 in Flutter

204 Views Asked by At

In my project i want to print the text in Marathi Language is it possible. Here is my code

Future<void> printMarathiText() async {
    final profile = await CapabilityProfile.load();
    final printer = NetworkPrinter(PaperSize.mm80, profile);
    List<int> bytes = [];
    final generator = Generator(PaperSize.mm80, profile);
    await connectToPrinter();

    bytes += generator.text('मराठी मध्ये टेक्स्ट प्रिंट करणारा प्रिंटर',
        styles: const PosStyles(
          align: PosAlign.center,
          height: PosTextSize.size1,
          width: PosTextSize.size1,
        ),
        linesAfter: 1);

    printer.cut();
    printer.disconnect();
  }

I am getting error as

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Invalid argument (string): Contains invalid characters.: "मराठी मध्ये टेक्   स्ट प्रिंट करणारा प्रिंटर"

is there any solution for this?

1

There are 1 best solutions below

2
الرحمن الرحیم On

use below code:

Future<void> printMarathiText() async {
  final profile = await CapabilityProfile.load();
  final printer = NetworkPrinter(PaperSize.mm80, profile);
  final generator = Generator(PaperSize.mm80, profile);
  await connectToPrinter();

  // Encode Marathi text as UTF-8
  final marathiText = 'मराठी मध्ये टेक्स्ट प्रिंट करणारा प्रिंटर';
  final marathiBytes = utf8.encode(marathiText);

  // Use a suitable font that supports Marathi characters
  final font = PosFont.custom(
    codeTable: 'CP1252', // Use the code table that matches your font
    width: 2,
    height: 2,
  );

  // Print Marathi text
  generator.textCustom(
    marathiBytes,
    styles: const PosStyles(
      align: PosAlign.center,
      height: PosTextSize.size2,
      width: PosTextSize.size2,
    ),
    linesAfter: 1,
    customStyles: [font],
  );

  printer.cut();
  printer.disconnect();
}