esc_pos_utils 0.3.6

  • Readme
  • Changelog
  • Example
  • Installing
  • 94

esc_pos_utils #

Pub Version

Base Flutter/Dart classes for ESC/POS printing. Ticket class generates ESC/POS commands that can be sent to a thermal printer.

This is the "base" library that used for:

Main Features #

  • Connect to Wi-Fi / Ethernet 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), CODE128
  • Paper cut (partial, full)
  • Beeping (with different duration)
  • Paper feed, reverse feed

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

Generate a Ticket #

Simple ticket with styles: #

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: PosAlign.left));
  ticket.text('Align center', styles: PosStyles(align: PosAlign.center));
  ticket.text('Align right',
      styles: PosStyles(align: PosAlign.right), linesAfter: 1);

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

  ticket.feed(2);
  ticket.cut();
  return ticket;
}
ticket.row([
    PosColumn(
      text: 'col3',
      width: 3,
      styles: PosStyles(align: PosAlign.center, underline: true),
    ),
    PosColumn(
      text: 'col6',
      width: 6,
      styles: PosStyles(align: PosAlign.center, underline: true),
    ),
    PosColumn(
      text: 'col3',
      width: 3,
      styles: PosStyles(align: PosAlign.center, underline: true),
    ),
  ]);

This package implements 3 ESC/POS functions:

  • ESC * - print in column format
  • GS v 0 - print in bit raster format (obsolete)
  • GS ( L - print in bit raster format

Note that your printer may support only some of the above functions.

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

final ByteData data = await rootBundle.load('assets/logo.png');
final Uint8List bytes = data.buffer.asUint8List();
final Image image = decodeImage(bytes);
// Using `ESC *`
ticket.image(image);
// Using `GS v 0` (obsolete)
ticket.imageRaster(image);
// Using `GS ( L`
ticket.imageRaster(image, imageFn: PosImageFn.graphics);
final List<int> barData = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 4];
ticket.barcode(Barcode.upcA(barData));

Using native ESC/POS commands:

ticket.qrcode('example.com');

To print a QR Code as an image (if your printer doesn't support native commands), add qr_flutter and path_provider as a dependency in your pubspec.yaml file.

String qrData = "google.com";
const double qrSize = 200;
try {
  final uiImg = await QrPainter(
    data: qrData,
    version: QrVersions.auto,
    gapless: false,
  ).toImageData(qrSize);
  final dir = await getTemporaryDirectory();
  final pathName = '${dir.path}/qr_tmp.png';
  final qrFile = File(pathName);
  final imgFile = await qrFile.writeAsBytes(uiImg.buffer.asUint8List());
  final img = decodeImage(imgFile.readAsBytesSync());

  ticket.image(img);
} catch (e) {
  print(e);
}

Using Code Tables #

Thanks to the charset_converter package, it's possible to print in different languages. The source text should be encoded using the corresponding charset and the correct charset should be passed to the Ticket.textEncoded method.

Here are some examples:

/// Portuguese
Uint8List encTxt1 = await CharsetConverter.encode(
    "cp860", "Portuguese: Olá, Não falo português, Cão");
ticket.textEncoded(encTxt1,
    styles: PosStyles(codeTable: PosCodeTable.pc860_1));

/// Greek
Uint8List encTxt2 =
    await CharsetConverter.encode("windows-1253", "Greek: αβγδώ");
ticket.textEncoded(encTxt2,
    styles: PosStyles(codeTable: PosCodeTable.greek));

/// Polish
Uint8List encTxt3 = await CharsetConverter.encode(
    "cp852", "Polish: Dzień dobry! Dobry wieczór! Cześć!");
ticket.textEncoded(encTxt3,
    styles: PosStyles(codeTable: PosCodeTable.pc852_1));

/// Russian
Uint8List encTxt4 =
    await CharsetConverter.encode("cp866", "Russian: Привет мир!");
ticket.textEncoded(encTxt4,
    styles: PosStyles(codeTable: PosCodeTable.pc866_2));

/// Thai: x-mac-thai | iso-8859-11 | cp874
Uint8List encThai =
    await CharsetConverter.encode("cp874", "Thai: ใบเสร็จ-ใบรับผ้า");
ticket.textEncoded(encThai,
    styles: PosStyles(codeTable: PosCodeTable.thai_1));

/// Arabic
/// Possible charsets for CharsetConverter.encode: cp864, windows-1256
/// Possible codeTables for PosStyles: arabic, pc864_1, pc864_2, pc1001_1, pc1001_2, wp1256, pc720
Uint8List encArabic = await CharsetConverter.encode("windows-1256", "اهلا");
ticket.textEncoded(encArabic,
    styles: PosStyles(codeTable: PosCodeTable.arabic));

Note that CharsetConverter.encode takes a platform-specific charset (check the library documentation for more info).

Note that different printers may support different sets of codetables and the above examples may not work on some printer models. It's also possible to pass a codetable by its code (according to your printer's documentation): PosStyles(codeTable: PosCodeTable(7)).

How to Help #

  • Test your printer and add it in the table: Wifi/Network printer or Bluetooth printer
  • Test and report bugs
  • Share your ideas about what could be improved (code optimization, new features...)

[0.3.6] #

  • Print QR Codes using native function

[0.3.5] #

  • Added Ticket.setGlobalFont method
  • Ticket.codeTable renamed to Ticket.printCodeTable
  • Added maxCharsPerLine custom config + default values depending on current font and ticket size
  • Code refactoring

[0.3.4] #

  • Added hr method
  • Updated commands (using hex codes)
  • setStyles bug fix

[0.3.3] #

  • Slow printing issue on some printer models fixed

[0.3.2] #

  • PosColumn can contain encoded text (textEncoded field)
  • Bug fix: Columns with PosTextSize > size1
  • Added Barcode Code128
  • Added new code pages
  • imageRaster bug fixed
  • Ticket bytecode optimization: do not generate align left command (it's a default value)
  • Added new image print function: GS ( L

[0.3.1] #

  • Added Open cash drawer command

[0.3.0] #

  • Image alignment (left, center, right). Align center by default.
  • Barcode alignment (left, center, right). Align center by default.
  • PosTextAlign renamed to PosAlign

[0.2.0] #

  • Ticket._text function takes an Uint8List of bytes instead of a String
  • Ticket._text function refactored: removed styling commands when it's unnecessary which makes ticket's final byte code much shorter
  • PosCodeTable: private constructor replaced by public one to allow passing custom code table code
  • PosCodeTable: added new predefined code tables
  • Added Ticket.textEncoded function taking Uint8List textBytes (encoded text) to support different languages

[0.1.0 - 0.1.2] #

  • Initial release

example/example.dart

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

Future<void> main() 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: PosAlign.left));
  ticket.text('Align center', styles: PosStyles(align: PosAlign.center));
  ticket.text('Align right',
      styles: PosStyles(align: PosAlign.right), linesAfter: 1);

  ticket.row([
    PosColumn(
      text: 'col3',
      width: 3,
      styles: PosStyles(align: PosAlign.center, underline: true),
    ),
    PosColumn(
      text: 'col6',
      width: 6,
      styles: PosStyles(align: PosAlign.center, underline: true),
    ),
    PosColumn(
      text: 'col3',
      width: 3,
      styles: PosStyles(align: PosAlign.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();
}

Use this package as a library

1. Depend on it

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


dependencies:
  esc_pos_utils: ^0.3.6

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_utils/esc_pos_utils.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
88
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]
94
Learn more about scoring.

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

  • Dart: 2.8.2
  • pana: 0.13.8-dev
  • Flutter: 1.17.1

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
gbk_codec ^0.3.2 0.3.2
hex ^0.1.2 0.1.2
image ^2.1.4 2.1.13
Transitive dependencies
archive 2.0.13
args 1.6.0
charcode 1.1.3
collection 1.14.12
convert 2.1.1
crypto 2.1.5
csslib 0.16.1
html 0.14.0+3
meta 1.1.8
path 1.7.0
petitparser 3.0.4
sky_engine 0.0.99
source_span 1.7.0
term_glyph 1.1.0
typed_data 1.1.6
vector_math 2.0.8
xml 3.7.0 4.2.0
Dev dependencies
flutter_test