如何使用打印预览创建定制的打印设计,最后使用Flutter Dart使用Bluetooth Thermal Printer打印它?

问题描述

我正在使用 Flutter Dart 创建一个 Android应用,我对如何使用打印预览创建自己的自定义设计(即需要垂直打印设计)感到有些困惑屏幕,最后使用蓝牙热敏打印机打印该设计。

换句话说,我只是想要像波纹管图像那样的结果。

enter image description here

所以我正在寻找一点帮助。预先感谢。

解决方法

在浏览其他插件githubs之后,我使用了自己的自定义代码,因为我无法从这些插件中获得大部分可运行的命令。 我使用了局域网热敏打印机,但是我确定这也适用于蓝牙。

https://www.starmicronics.com/support/Mannualfolder/dot_star_cm_en.pdf 我使用该网站查看了可以发送到打印机的命令,但那只是反复试验。 希望您可以从那里获得所需的命令。

使用此插件连接打印机,然后尝试使用其方法进行打印,以查看是否已连接。 https://pub.dev/packages/esc_pos_bluetooth

我的代码中最底层的方法“ printTicketViaLan()”是通过LAN发送的。因此,要使用蓝牙,我已经制作了另一种方法“ printUsingBluetooth()”,但我没有蓝牙打印机,因此无法对其进行测试。 使用ticket.rawBytes(bytes);

发送数据

以下是我尝试过并适用于我的打印机的命令。

PS。使用LAN方法,您不需要任何插件。

import 'dart:convert' show utf8;
import 'dart:io';
import 'package:flutter/material.dart';

class _MyHomePageState extends State<MyHomePage> {

  void _printOrder() async {
    const esc = '\x1B';
    const gs = '\x1D';
    const fs = '\x1C';
    
    const reset = '$esc@'; // Initialize printer
    const underline = '$esc-1';
    const noUnderline = '$esc-0';
    const whiteOnBlack = '${esc}4';
    const noWhiteOnBlack = '${esc}5';
    const doubleWide = '${esc}W1';
    const doubleHeight = '${esc}h1';
    const tripleWide = '${esc}W2';
    const tripleHeight = '${esc}h2';
    const cutPaper = '${esc}d3';
    const cutPaper = '${esc}d3';

    List<int> bytes = List<int>();
    bytes += reset.codeUnits;

    bytes += tripleWide.codeUnits;
    bytes += tripleHeight.codeUnits;
    bytes += utf8.encode('Some Headline \n');
    bytes += doubleWide.codeUnits;
    bytes += doubleHeight.codeUnits;
    bytes += utf8.encode('Subtitle');
    bytes += reset.codeUnits;

    
    bytes += utf8.encode('Normal Text \n');
    bytes += whiteOnBlack.codeUnits;
    bytes += utf8.encode('White Text with black background\n');
    bytes += reset.codeUnits;

    bytes += underline.codeUnits;
    bytes += noUnderline.codeUnits;
    bytes += utf8.encode('Underlined Text \n');
    
    bytes += cutPaper.codeUnits;
    

    printUsingBluetooth(bytes);

    printTicketViaLan(bytes);
  }

  void printUsingBluetooth(List<int> bytes) {
    PrinterBluetoothManager printerManager = PrinterBluetoothManager();
    printerManager.scanResults.listen((printers) async {
      // store found printers and choose the right one from a list
    });
    printerManager.startScan(Duration(seconds: 4));

    //Creating new ticket and using bytes to send.
    Ticket ticket = Ticket();
    ticket.rawBytes(bytes);
    //Not sure if isKanji must be set to true... Try both.
    ticket.rawBytes(bytes,isKanji: true);


    printerManager.selectPrinter(printer);
    final PosPrintResult res = await printerManager.printTicket(ticket);

    print('Print result: ${res.msg}');
  }

  void printTicketViaLan(List<int> bytes) async {
    Duration timeout = const Duration(seconds: 5);
    var result = await Socket.connect('192.168.0.34',9100,timeout: timeout)
        .then((Socket socket) {
      socket.add(bytes);
      socket.destroy();
      return 'Data sent Success';
    }).catchError((dynamic e) {
      return 'Error sending Data';
    });
    print(result);
  }