esc_pos_printer 2.1.2

  • Readme
  • Changelog
  • Example
  • Installing
  • 93

esc_pos_printer #

Pub

The library allows to print receipts using an ESC/POS thermal WiFi/Bluetooth printer.

[pub.dev page] | [Documentation]

WiFi printing can be used in Flutter or pure Dart projects. For Flutter projects, both Android and iOS are supported.

Bluetooth printing can also be used on both Android and iOS platforms.

To discover existing printers in your subnet, consider using ping_discover_network package. Note that most of the ESC/POS printers by default listen on port 9100.

Main Features #

  • Connect to Wi-Fi / Bluetooth printers
  • Simple text printing using text method
  • Tables printing using row method
  • Text styling:
    • size, align, bold, reverse, underline, different fonts, turn 90°
  • Print images
  • Print barcodes
    • UPC-A, UPC-E, JAN13 (EAN13), JAN8 (EAN8), CODE39, ITF (Interleaved 2 of 5), CODABAR (NW-7)
  • Paper cut (partial, full)
  • Beeping (with different duration)
  • Paper feed, reverse feed

Note: Your printer may not support some of the presented features (especially for underline styles, partial/full paper cutting, reverse feed, ...).

Getting started (Generate a ticket) #

Ticket testTicket() {
  final Ticket ticket = Ticket(PaperSize.mm80);

  ticket.text(
      'Regular: aA bB cC dD eE fF gG hH iI jJ kK lL mM nN oO pP qQ rR sS tT uU vV wW xX yY zZ');
  ticket.text('Special 1: àÀ èÈ éÉ ûÛ üÜ çÇ ôÔ',
      styles: PosStyles(codeTable: PosCodeTable.westEur));
  ticket.text('Special 2: blåbærgrød',
      styles: PosStyles(codeTable: PosCodeTable.westEur));

  ticket.text('Bold text', styles: PosStyles(bold: true));
  ticket.text('Reverse text', styles: PosStyles(reverse: true));
  ticket.text('Underlined text',
      styles: PosStyles(underline: true), linesAfter: 1);
  ticket.text('Align left', styles: PosStyles(align: PosTextAlign.left));
  ticket.text('Align center', styles: PosStyles(align: PosTextAlign.center));
  ticket.text('Align right',
      styles: PosStyles(align: PosTextAlign.right), linesAfter: 1);

  ticket.text('Text size 200%',
      styles: PosStyles(
        height: PosTextSize.size2,
        width: PosTextSize.size2,
      ));

  ticket.feed(2);
  ticket.cut();
  return ticket;
}

Print table row:

ticket.row([
    PosColumn(
      text: 'col3',
      width: 3,
      styles: PosStyles(align: PosTextAlign.center, underline: true),
    ),
    PosColumn(
      text: 'col6',
      width: 6,
      styles: PosStyles(align: PosTextAlign.center, underline: true),
    ),
    PosColumn(
      text: 'col3',
      width: 3,
      styles: PosStyles(align: PosTextAlign.center, underline: true),
    ),
  ]);

Print image:

import 'dart:io';
import 'package:image/image.dart';

const String filename = './logo.png';
final Image image = decodeImage(File(filename).readAsBytesSync());
// Using (ESC *) command
ticket.image(image);
// Using an alternative obsolette (GS v 0) command
ticket.imageRaster(image);

Print barcode:

final List<int> barData = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 4];
ticket.barcode(Barcode.upcA(barData));

Getting Started (WiFi printer) #

import 'package:esc_pos_printer/esc_pos_printer.dart';

final PrinterNetworkManager printerManager = PrinterNetworkManager();
printerManager.selectPrinter('192.168.0.123', port: 9100);
final PosPrintResult res = await printerManager.printTicket(testTicket());

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

For more details, check example/example.dart and example/discover_printers.

Getting Started (Bluetooth printer) #

PrinterBluetoothManager printerManager = PrinterBluetoothManager();

printerManager.scanResults.listen((printers) async {
  // store found printers
});
printerManager.startScan(Duration(seconds: 4));

// ...

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

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

For more details, check demo project example/blue.

TODO #

  • Print QR codes
  • USB printers support

Test print #

test receipt

Support #

If this package was helpful, a cup of coffee would be highly appreciated :)

[2.1.2]

  • Bluetooth printing support for Android

[2.1.0]

  • Printer class replaced by PrinterNetworkManager
  • Generate tickets using Ticket class
  • Unified interfaces for WiFi/Bluetooth printings
  • Better error handling using PosPrintResult class
  • Examples updated

[2.0.0]

  • Bluetooth printers support (beta version, iOS-only)

[1.5.0]

  • printlnMixedKanji merged to println
  • Can print rows/cols with mixed chinese/non-chinese texts

[1.4.1]

  • Print mixed (chinese + latin) text. Only for printers supporting Kanji mode

[1.4.0]

  • Print barcodes

[1.3.2]

  • Added alternative printImageRaster method using (GS v 0) obsolete command
  • Code refactoring

[1.3.1]

  • Removed raw lib
  • Code refactoring

[1.3.0]

  • Image printing method has been improved
  • Using ESC * command to print images instead of outdated GS v 0
  • Added print image Flutter example

[1.2.0]

  • Printing images (beta)

[1.1.3]

  • printCodeTable bug fixed
  • Updated test print example

[1.1.2]

  • Better alignment

[1.1.1]

  • Fixed columns alignment bug

[1.1.0]

  • Added code page support

[1.0.2]

  • Send raw command(s)
  • Turn 90° clockwise rotation mode on/off
  • Flutter example: discover active Wi-Fi printers
  • Select character code table

[1.0.1]

  • exported PosColumn

[1.0.0]

  • Removed PosString class
  • Added PosStyles and PosColumn classes
  • Updated examples and readme

[0.9.0] - [0.9.1]

  • Initial release
  • Added basic functions (cut paper, write line, ...)
  • Text styling (bold, underline, reverted, ...)
  • Text align
  • Table row printing (up to 12 columns of different width)

example/example.dart

import 'dart:io';
import 'dart:typed_data';
import 'package:flutter/services.dart';
import 'package:image/image.dart';
import 'package:esc_pos_printer/esc_pos_printer.dart';

void main() async {
  final PrinterNetworkManager printerManager = PrinterNetworkManager();
  // To discover network printers in your subnet, consider using
  // ping_discover_network package (https://pub.dev/packages/ping_discover_network).
  // Note that most of ESC/POS printers are available on port 9100 by default.
  printerManager.selectPrinter('192.168.0.123', port: 9100);

  final PosPrintResult res =
      await printerManager.printTicket(await testTicket());
  print('Print result: ${res.msg}');
}

Future<Ticket> testTicket() async {
  final Ticket ticket = Ticket(PaperSize.mm80);

  ticket.text(
      'Regular: aA bB cC dD eE fF gG hH iI jJ kK lL mM nN oO pP qQ rR sS tT uU vV wW xX yY zZ');
  ticket.text('Special 1: àÀ èÈ éÉ ûÛ üÜ çÇ ôÔ',
      styles: PosStyles(codeTable: PosCodeTable.westEur));
  ticket.text('Special 2: blåbærgrød',
      styles: PosStyles(codeTable: PosCodeTable.westEur));

  ticket.text('Bold text', styles: PosStyles(bold: true));
  ticket.text('Reverse text', styles: PosStyles(reverse: true));
  ticket.text('Underlined text',
      styles: PosStyles(underline: true), linesAfter: 1);
  ticket.text('Align left', styles: PosStyles(align: PosTextAlign.left));
  ticket.text('Align center', styles: PosStyles(align: PosTextAlign.center));
  ticket.text('Align right',
      styles: PosStyles(align: PosTextAlign.right), linesAfter: 1);

  ticket.row([
    PosColumn(
      text: 'col3',
      width: 3,
      styles: PosStyles(align: PosTextAlign.center, underline: true),
    ),
    PosColumn(
      text: 'col6',
      width: 6,
      styles: PosStyles(align: PosTextAlign.center, underline: true),
    ),
    PosColumn(
      text: 'col3',
      width: 3,
      styles: PosStyles(align: PosTextAlign.center, underline: true),
    ),
  ]);

  ticket.text('Text size 200%',
      styles: PosStyles(
        height: PosTextSize.size2,
        width: PosTextSize.size2,
      ));

  // Print image
  final ByteData data = await rootBundle.load('assets/logo.png');
  final Uint8List bytes = data.buffer.asUint8List();
  final Image image = decodeImage(bytes);
  ticket.image(image);
  // Print image using an alternative (obsolette) command
  // ticket.imageRaster(image);

  // Print barcode
  final List<int> barData = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 4];
  ticket.barcode(Barcode.upcA(barData));

  // Print mixed (chinese + latin) text. Only for printers supporting Kanji mode
  // ticket.text(
  //   'hello ! 中文字 # world @ éphémère &',
  //   styles: PosStyles(codeTable: PosCodeTable.westEur),
  //   containsChinese: true,
  // );

  ticket.feed(2);

  ticket.cut();
  return ticket;
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  esc_pos_printer: ^2.1.2

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:esc_pos_printer/esc_pos_printer.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
87
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
93
Learn more about scoring.

We analyzed this package on Jan 27, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.4
  • Flutter: 1.12.13+hotfix.5

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
flutter_bluetooth_basic ^0.1.2 0.1.2
gbk_codec ^0.3.1+3 0.3.2
hex ^0.1.2 0.1.2
image ^2.1.4 2.1.12
rxdart ^0.23.1 0.23.1
Transitive dependencies
archive 2.0.13
args 1.5.2
charcode 1.1.2
collection 1.14.11 1.14.12
convert 2.1.1
crypto 2.1.4
csslib 0.16.1
html 0.14.0+3
json_annotation 3.0.1
meta 1.1.8
path 1.6.4
petitparser 3.0.0
sky_engine 0.0.99
source_span 1.6.0
term_glyph 1.1.0
typed_data 1.1.6
vector_math 2.0.8
xml 3.7.0
Dev dependencies
flutter_test