/sunmi_ext_printer

Flutter Plugin for Sunmi external thermal printer

Primary LanguageDartMIT LicenseMIT

sunmi_ext_printer

Support Sunmi external printer, based on Sunmi official SDK documnet for Sunmi external printer.

Main Features

  • Print text
  • Print image
  • Print QrCode
  • Print Barcode
  • Print table
  • Paper cut
  • Paper feed
  • Alignment:
    • left, center, right
  • Styling:
    • bold, scale up in X, Y coordinate
  • Send raw data
  • Find bluetooth printer
    • For bluetooth permission, should be handled within Flutter App, for example, by using permission_handler

Examples

Define a variable for later use

final pos = SunmiExtPrinter();

Scan bluetooth for paired devices

Set<String> _bluetoothIds = {};
// final pos = SunmiExtPrinter();

void findBleDevices() async {
    var res = await pos.findBleDevice();
    Set<String> ids = {};
    if (res != null) {
        for (var i = 0; i < res.length; i++) {
        ids.add('${res[i]}');
        }
    }

    setState(() {
        _bluetoothIds = ids;
    });
}

Print texts

// Please **change** deviceID to the bluetooth address found by scanning result
var deviceID = '74:F7:F:FD:41:0D';

// final pos = SunmiExtPrinter();
await pos.setPrinter(SunmiPrinter.SunmiBlueToothPrinter, deviceID);
await pos.connectPrinter();

await pos.setAlignMode(AlignMode.Center);
await pos.printText("Rubybear\n");

Print QrCode

// final pos = SunmiExtPrinter();
await pos.lineWrap();
await pos.printQrCode("https://edesoft.com/en.html");
await pos.lineWrap();

Print image

var bytes = await rootBundle.load('images/logo.png');
var buf = bytes.buffer.asUint8List();
// final pos = SunmiExtPrinter();
await pos.printImage(buf);
await pos.lineWrap();
  • Make sure assets are defined in pubspec.yaml
assets:
  - images/logo.png
  • Make sure image file images/logo.png exists in the right folder.

  • [Optional] You may resize image before send to printer, in case too big to print. Package image is a good one to do that. E.g.

import 'package:image/image.dart' as img;

....

  Future<Uint8List> _convertImage(Uint8List imageBytes, int? printWidth,
      int? printHeight, bool invert) async {
    var cmd = img.Command()
      ..decodeImage(imageBytes)
      ..grayscale();
    await cmd.executeThread();

    if (printWidth == null && printHeight == null) {
      printWidth = 256;
    }

    cmd.copyResize(width: printWidth, height: printHeight);

    if (invert) {
      cmd.invert();
    }
    cmd.encodeBmp();
    await cmd.executeThread();

    var bitmapBytes = cmd.outputBytes ?? imageBytes;
    return bitmapBytes;
  }

Print table

// final pos = SunmiExtPrinter();
await pos.enableBold(true);
await pos.printColumnsText(["Item", "Qty", "Amt"], [18, 6, 8], [0, 2, 2]);

await pos.enableBold(false);
await pos.printColumnsText(["Carlsberg Bottle", "2", "20.00"], [18, 6, 8], [0, 2, 2]);
await pos.printColumnsText(["Milk 2L", "1", "3.50"], [18, 6, 8], [0, 2, 2]);

Page cut

    // final pos = SunmiExtPrinter();
    await pos.cutPaper(PaperCutMode.FullCut);

Flush

    // final pos = SunmiExtPrinter();
    await pos.flush();

Tested in printers

  • Sunmi NT211

Printer SDK

Version

Document