我们可以使用esc_pos_printer打印马拉地语文本吗:在Flutter中的^4.1.0

91zkwejq  于 2023-10-22  发布在  Flutter
关注(0)|答案(1)|浏览(137)

在我的项目中,我想打印马拉地语的文字是可能的。这是我的代码

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();
  }

我得到的错误是

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

有什么解决办法吗?

gfttwv5a

gfttwv5a1#

使用下面的代码:

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();
}

相关问题