Commit 603c8483 authored by andrey's avatar andrey

New code generation concept: instead of generating the whole ticket, commands…

New code generation concept: instead of generating the whole ticket, commands sent separately for each command
parent 69361b5b
## [4.0.0]
- New code generation concept: instead of generating the whole ticket, commands sent separately for each command
- `PrinterNetworkManager` replaced by `NetworkPrinter`
- Bump esc_pos_utils
## [3.2.9] ## [3.2.9]
- Bump esc_pos_utils - Bump esc_pos_utils
......
...@@ -30,36 +30,31 @@ Here are some [printers tested with this library](printers.md). Please add the m ...@@ -30,36 +30,31 @@ Here are some [printers tested with this library](printers.md). Please add the m
### Simple Ticket with Styles: ### Simple Ticket with Styles:
```dart ```dart
Ticket testTicket() { void testReceipt(NetworkPrinter printer) {
// Using default profile printer.text(
final profile = await CapabilityProfile.load();
final Ticket ticket = Ticket(PaperSize.mm80, profile);
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'); '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: àÀ èÈ éÉ ûÛ üÜ çÇ ôÔ', printer.text('Special 1: àÀ èÈ éÉ ûÛ üÜ çÇ ôÔ',
styles: PosStyles(codeTable: PosCodeTable.westEur)); styles: PosStyles(codeTable: 'CP1252'));
ticket.text('Special 2: blåbærgrød', printer.text('Special 2: blåbærgrød',
styles: PosStyles(codeTable: PosCodeTable.westEur)); styles: PosStyles(codeTable: 'CP1252'));
ticket.text('Bold text', styles: PosStyles(bold: true)); printer.text('Bold text', styles: PosStyles(bold: true));
ticket.text('Reverse text', styles: PosStyles(reverse: true)); printer.text('Reverse text', styles: PosStyles(reverse: true));
ticket.text('Underlined text', printer.text('Underlined text',
styles: PosStyles(underline: true), linesAfter: 1); styles: PosStyles(underline: true), linesAfter: 1);
ticket.text('Align left', styles: PosStyles(align: PosAlign.left)); printer.text('Align left', styles: PosStyles(align: PosAlign.left));
ticket.text('Align center', styles: PosStyles(align: PosAlign.center)); printer.text('Align center', styles: PosStyles(align: PosAlign.center));
ticket.text('Align right', printer.text('Align right',
styles: PosStyles(align: PosAlign.right), linesAfter: 1); styles: PosStyles(align: PosAlign.right), linesAfter: 1);
ticket.text('Text size 200%', printer.text('Text size 200%',
styles: PosStyles( styles: PosStyles(
height: PosTextSize.size2, height: PosTextSize.size2,
width: PosTextSize.size2, width: PosTextSize.size2,
)); ));
ticket.feed(2); printer.feed(2);
ticket.cut(); printer.cut();
return ticket;
} }
``` ```
...@@ -70,9 +65,16 @@ You can find more examples here: [esc_pos_utils](https://github.com/andrey-ushak ...@@ -70,9 +65,16 @@ You can find more examples here: [esc_pos_utils](https://github.com/andrey-ushak
```dart ```dart
import 'package:esc_pos_printer/esc_pos_printer.dart'; import 'package:esc_pos_printer/esc_pos_printer.dart';
final PrinterNetworkManager printerManager = PrinterNetworkManager(); const PaperSize paper = PaperSize.mm80;
printerManager.selectPrinter('192.168.0.123', port: 9100); final profile = await CapabilityProfile.load();
final PosPrintResult res = await printerManager.printTicket(testTicket()); final printer = NetworkPrinter(paper, profile);
final PosPrintResult res = await printer.connect('192.168.0.123', port: 9100);
if (res == PosPrintResult.success) {
testReceipt(printer);
printer.disconnect();
}
print('Print result: ${res.msg}'); print('Print result: ${res.msg}');
``` ```
......
6d52d4ce4fb1beead3ee79b19e241be0 50d596c07db937a14e616078f41e1aa4
\ No newline at end of file \ No newline at end of file
...@@ -92,27 +92,24 @@ class _MyHomePageState extends State<MyHomePage> { ...@@ -92,27 +92,24 @@ class _MyHomePageState extends State<MyHomePage> {
}); });
} }
Future<Ticket> testTicket(PaperSize paper) async { Future<void> testReceipt(NetworkPrinter printer) async {
final profile = await CapabilityProfile.load(); printer.text(
final Ticket ticket = Ticket(paper, profile);
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'); '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: àÀ èÈ éÉ ûÛ üÜ çÇ ôÔ', printer.text('Special 1: àÀ èÈ éÉ ûÛ üÜ çÇ ôÔ',
styles: PosStyles(codeTable: 'CP1252')); styles: PosStyles(codeTable: 'CP1252'));
ticket.text('Special 2: blåbærgrød', printer.text('Special 2: blåbærgrød',
styles: PosStyles(codeTable: 'CP1252')); styles: PosStyles(codeTable: 'CP1252'));
ticket.text('Bold text', styles: PosStyles(bold: true)); printer.text('Bold text', styles: PosStyles(bold: true));
ticket.text('Reverse text', styles: PosStyles(reverse: true)); printer.text('Reverse text', styles: PosStyles(reverse: true));
ticket.text('Underlined text', printer.text('Underlined text',
styles: PosStyles(underline: true), linesAfter: 1); styles: PosStyles(underline: true), linesAfter: 1);
ticket.text('Align left', styles: PosStyles(align: PosAlign.left)); printer.text('Align left', styles: PosStyles(align: PosAlign.left));
ticket.text('Align center', styles: PosStyles(align: PosAlign.center)); printer.text('Align center', styles: PosStyles(align: PosAlign.center));
ticket.text('Align right', printer.text('Align right',
styles: PosStyles(align: PosAlign.right), linesAfter: 1); styles: PosStyles(align: PosAlign.right), linesAfter: 1);
ticket.row([ printer.row([
PosColumn( PosColumn(
text: 'col3', text: 'col3',
width: 3, width: 3,
...@@ -130,7 +127,7 @@ class _MyHomePageState extends State<MyHomePage> { ...@@ -130,7 +127,7 @@ class _MyHomePageState extends State<MyHomePage> {
), ),
]); ]);
ticket.text('Text size 200%', printer.text('Text size 200%',
styles: PosStyles( styles: PosStyles(
height: PosTextSize.size2, height: PosTextSize.size2,
width: PosTextSize.size2, width: PosTextSize.size2,
...@@ -140,39 +137,34 @@ class _MyHomePageState extends State<MyHomePage> { ...@@ -140,39 +137,34 @@ class _MyHomePageState extends State<MyHomePage> {
final ByteData data = await rootBundle.load('assets/logo.png'); final ByteData data = await rootBundle.load('assets/logo.png');
final Uint8List bytes = data.buffer.asUint8List(); final Uint8List bytes = data.buffer.asUint8List();
final Image image = decodeImage(bytes); final Image image = decodeImage(bytes);
ticket.image(image); printer.image(image);
// Print image using alternative commands // Print image using alternative commands
// ticket.imageRaster(image); // printer.imageRaster(image);
// ticket.imageRaster(image, imageFn: PosImageFn.graphics); // printer.imageRaster(image, imageFn: PosImageFn.graphics);
// Print barcode // Print barcode
final List<int> barData = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 4]; final List<int> barData = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 4];
ticket.barcode(Barcode.upcA(barData)); printer.barcode(Barcode.upcA(barData));
// Print mixed (chinese + latin) text. Only for printers supporting Kanji mode // Print mixed (chinese + latin) text. Only for printers supporting Kanji mode
// ticket.text( // printer.text(
// 'hello ! 中文字 # world @ éphémère &', // 'hello ! 中文字 # world @ éphémère &',
// styles: PosStyles(codeTable: PosCodeTable.westEur), // styles: PosStyles(codeTable: PosCodeTable.westEur),
// containsChinese: true, // containsChinese: true,
// ); // );
ticket.feed(2); printer.feed(2);
printer.cut();
ticket.cut();
return ticket;
} }
Future<Ticket> demoReceipt(PaperSize paper) async { Future<void> printDemoReceipt(NetworkPrinter printer) async {
final profile = await CapabilityProfile.load();
final Ticket ticket = Ticket(paper, profile);
// Print image // Print image
final ByteData data = await rootBundle.load('assets/rabbit_black.jpg'); final ByteData data = await rootBundle.load('assets/rabbit_black.jpg');
final Uint8List bytes = data.buffer.asUint8List(); final Uint8List bytes = data.buffer.asUint8List();
final Image image = decodeImage(bytes); final Image image = decodeImage(bytes);
ticket.image(image); printer.image(image);
ticket.text('GROCERYLY', printer.text('GROCERYLY',
styles: PosStyles( styles: PosStyles(
align: PosAlign.center, align: PosAlign.center,
height: PosTextSize.size2, height: PosTextSize.size2,
...@@ -180,14 +172,16 @@ class _MyHomePageState extends State<MyHomePage> { ...@@ -180,14 +172,16 @@ class _MyHomePageState extends State<MyHomePage> {
), ),
linesAfter: 1); linesAfter: 1);
ticket.text('889 Watson Lane', styles: PosStyles(align: PosAlign.center)); printer.text('889 Watson Lane', styles: PosStyles(align: PosAlign.center));
ticket.text('New Braunfels, TX', styles: PosStyles(align: PosAlign.center)); printer.text('New Braunfels, TX',
ticket.text('Tel: 830-221-1234', styles: PosStyles(align: PosAlign.center)); styles: PosStyles(align: PosAlign.center));
ticket.text('Web: www.example.com', printer.text('Tel: 830-221-1234',
styles: PosStyles(align: PosAlign.center));
printer.text('Web: www.example.com',
styles: PosStyles(align: PosAlign.center), linesAfter: 1); styles: PosStyles(align: PosAlign.center), linesAfter: 1);
ticket.hr(); printer.hr();
ticket.row([ printer.row([
PosColumn(text: 'Qty', width: 1), PosColumn(text: 'Qty', width: 1),
PosColumn(text: 'Item', width: 7), PosColumn(text: 'Item', width: 7),
PosColumn( PosColumn(
...@@ -196,7 +190,7 @@ class _MyHomePageState extends State<MyHomePage> { ...@@ -196,7 +190,7 @@ class _MyHomePageState extends State<MyHomePage> {
text: 'Total', width: 2, styles: PosStyles(align: PosAlign.right)), text: 'Total', width: 2, styles: PosStyles(align: PosAlign.right)),
]); ]);
ticket.row([ printer.row([
PosColumn(text: '2', width: 1), PosColumn(text: '2', width: 1),
PosColumn(text: 'ONION RINGS', width: 7), PosColumn(text: 'ONION RINGS', width: 7),
PosColumn( PosColumn(
...@@ -204,7 +198,7 @@ class _MyHomePageState extends State<MyHomePage> { ...@@ -204,7 +198,7 @@ class _MyHomePageState extends State<MyHomePage> {
PosColumn( PosColumn(
text: '1.98', width: 2, styles: PosStyles(align: PosAlign.right)), text: '1.98', width: 2, styles: PosStyles(align: PosAlign.right)),
]); ]);
ticket.row([ printer.row([
PosColumn(text: '1', width: 1), PosColumn(text: '1', width: 1),
PosColumn(text: 'PIZZA', width: 7), PosColumn(text: 'PIZZA', width: 7),
PosColumn( PosColumn(
...@@ -212,7 +206,7 @@ class _MyHomePageState extends State<MyHomePage> { ...@@ -212,7 +206,7 @@ class _MyHomePageState extends State<MyHomePage> {
PosColumn( PosColumn(
text: '3.45', width: 2, styles: PosStyles(align: PosAlign.right)), text: '3.45', width: 2, styles: PosStyles(align: PosAlign.right)),
]); ]);
ticket.row([ printer.row([
PosColumn(text: '1', width: 1), PosColumn(text: '1', width: 1),
PosColumn(text: 'SPRING ROLLS', width: 7), PosColumn(text: 'SPRING ROLLS', width: 7),
PosColumn( PosColumn(
...@@ -220,7 +214,7 @@ class _MyHomePageState extends State<MyHomePage> { ...@@ -220,7 +214,7 @@ class _MyHomePageState extends State<MyHomePage> {
PosColumn( PosColumn(
text: '2.99', width: 2, styles: PosStyles(align: PosAlign.right)), text: '2.99', width: 2, styles: PosStyles(align: PosAlign.right)),
]); ]);
ticket.row([ printer.row([
PosColumn(text: '3', width: 1), PosColumn(text: '3', width: 1),
PosColumn(text: 'CRUNCHY STICKS', width: 7), PosColumn(text: 'CRUNCHY STICKS', width: 7),
PosColumn( PosColumn(
...@@ -228,9 +222,9 @@ class _MyHomePageState extends State<MyHomePage> { ...@@ -228,9 +222,9 @@ class _MyHomePageState extends State<MyHomePage> {
PosColumn( PosColumn(
text: '2.55', width: 2, styles: PosStyles(align: PosAlign.right)), text: '2.55', width: 2, styles: PosStyles(align: PosAlign.right)),
]); ]);
ticket.hr(); printer.hr();
ticket.row([ printer.row([
PosColumn( PosColumn(
text: 'TOTAL', text: 'TOTAL',
width: 6, width: 6,
...@@ -248,9 +242,9 @@ class _MyHomePageState extends State<MyHomePage> { ...@@ -248,9 +242,9 @@ class _MyHomePageState extends State<MyHomePage> {
)), )),
]); ]);
ticket.hr(ch: '=', linesAfter: 1); printer.hr(ch: '=', linesAfter: 1);
ticket.row([ printer.row([
PosColumn( PosColumn(
text: 'Cash', text: 'Cash',
width: 8, width: 8,
...@@ -260,7 +254,7 @@ class _MyHomePageState extends State<MyHomePage> { ...@@ -260,7 +254,7 @@ class _MyHomePageState extends State<MyHomePage> {
width: 4, width: 4,
styles: PosStyles(align: PosAlign.right, width: PosTextSize.size2)), styles: PosStyles(align: PosAlign.right, width: PosTextSize.size2)),
]); ]);
ticket.row([ printer.row([
PosColumn( PosColumn(
text: 'Change', text: 'Change',
width: 8, width: 8,
...@@ -271,14 +265,14 @@ class _MyHomePageState extends State<MyHomePage> { ...@@ -271,14 +265,14 @@ class _MyHomePageState extends State<MyHomePage> {
styles: PosStyles(align: PosAlign.right, width: PosTextSize.size2)), styles: PosStyles(align: PosAlign.right, width: PosTextSize.size2)),
]); ]);
ticket.feed(2); printer.feed(2);
ticket.text('Thank you!', printer.text('Thank you!',
styles: PosStyles(align: PosAlign.center, bold: true)); styles: PosStyles(align: PosAlign.center, bold: true));
final now = DateTime.now(); final now = DateTime.now();
final formatter = DateFormat('MM/dd/yyyy H:m'); final formatter = DateFormat('MM/dd/yyyy H:m');
final String timestamp = formatter.format(now); final String timestamp = formatter.format(now);
ticket.text(timestamp, printer.text(timestamp,
styles: PosStyles(align: PosAlign.center), linesAfter: 2); styles: PosStyles(align: PosAlign.center), linesAfter: 2);
// Print QR Code from image // Print QR Code from image
...@@ -296,33 +290,33 @@ class _MyHomePageState extends State<MyHomePage> { ...@@ -296,33 +290,33 @@ class _MyHomePageState extends State<MyHomePage> {
// final imgFile = await qrFile.writeAsBytes(uiImg.buffer.asUint8List()); // final imgFile = await qrFile.writeAsBytes(uiImg.buffer.asUint8List());
// final img = decodeImage(imgFile.readAsBytesSync()); // final img = decodeImage(imgFile.readAsBytesSync());
// ticket.image(img); // printer.image(img);
// } catch (e) { // } catch (e) {
// print(e); // print(e);
// } // }
// Print QR Code using native function // Print QR Code using native function
// ticket.qrcode('example.com'); // printer.qrcode('example.com');
ticket.feed(2); printer.feed(1);
ticket.cut(); printer.cut();
return ticket;
} }
void testPrint(String printerIp, BuildContext ctx) async { void testPrint(String printerIp, BuildContext ctx) async {
final PrinterNetworkManager printerManager = PrinterNetworkManager();
printerManager.selectPrinter(printerIp, port: 9100);
// TODO Don't forget to choose printer's paper size // TODO Don't forget to choose printer's paper size
const PaperSize paper = PaperSize.mm80; const PaperSize paper = PaperSize.mm80;
final profile = await CapabilityProfile.load();
final printer = NetworkPrinter(paper, profile);
// TEST PRINT final PosPrintResult res = await printer.connect(printerIp, port: 9100);
// final PosPrintResult res =
// await printerManager.printTicket(await testTicket(paper));
if (res == PosPrintResult.success) {
// DEMO RECEIPT // DEMO RECEIPT
final PosPrintResult res = await printDemoReceipt(printer);
await printerManager.printTicket(await demoReceipt(paper)); // TEST PRINT
// await testReceipt(printer);
printer.disconnect();
}
final snackBar = final snackBar =
SnackBar(content: Text(res.msg, textAlign: TextAlign.center)); SnackBar(content: Text(res.msg, textAlign: TextAlign.center));
......
...@@ -18,7 +18,7 @@ dependencies: ...@@ -18,7 +18,7 @@ dependencies:
intl: ^0.16.1 intl: ^0.16.1
qr_flutter: ^3.2.0 qr_flutter: ^3.2.0
path_provider: ^1.6.5 path_provider: ^1.6.5
esc_pos_utils: ^0.4.9 esc_pos_utils: ^1.0.0
# esc_pos_utils: # esc_pos_utils:
# path: ../../../esc_pos_utils # path: ../../../esc_pos_utils
charset_converter: ^1.0.3 charset_converter: ^1.0.3
......
// 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';
import 'package:esc_pos_utils/esc_pos_utils.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 profile = await CapabilityProfile.load();
final Ticket ticket = Ticket(PaperSize.mm80, profile);
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: 'CP1252'));
ticket.text('Special 2: blåbærgrød', styles: PosStyles(codeTable: 'CP1252'));
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 alternative commands
// ticket.imageRaster(image);
// ticket.imageRaster(image, imageFn: PosImageFn.graphics);
// 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;
}
...@@ -8,4 +8,4 @@ ...@@ -8,4 +8,4 @@
library esc_pos_printer; library esc_pos_printer;
export './src/enums.dart'; export './src/enums.dart';
export './src/printer_network_manager.dart'; export 'src/network_printer.dart';
/*
* esc_pos_printer
* Created by Andrey Ushakov
*
* Copyright (c) 2019-2020. All rights reserved.
* See LICENSE for distribution and usage details.
*/
import 'dart:io';
import 'dart:typed_data' show Uint8List;
import 'package:esc_pos_utils/esc_pos_utils.dart';
import 'package:image/image.dart';
import './enums.dart';
/// Network Printer
class NetworkPrinter {
NetworkPrinter(PaperSize paperSize, CapabilityProfile profile,
{int spaceBetweenRows = 5}) {
_generator =
Generator(paperSize, profile, spaceBetweenRows: spaceBetweenRows);
}
Generator _generator;
Socket _socket;
Future<PosPrintResult> connect(String host,
{int port = 91000, Duration timeout = const Duration(seconds: 5)}) async {
try {
_socket = await Socket.connect('192.168.0.101', port, timeout: timeout);
_socket.add(_generator.reset());
return Future<PosPrintResult>.value(PosPrintResult.success);
} catch (e) {
return Future<PosPrintResult>.value(PosPrintResult.timeout);
}
}
void disconnect() {
_socket.destroy();
}
// ************************ Printer Commands ************************
void reset() {
_socket.add(_generator.reset());
}
void text(
String text, {
PosStyles styles = const PosStyles(),
int linesAfter = 0,
bool containsChinese = false,
int maxCharsPerLine,
}) {
_socket.add(_generator.text(text,
styles: styles,
linesAfter: linesAfter,
containsChinese: containsChinese,
maxCharsPerLine: maxCharsPerLine));
}
void setGlobalCodeTable(String codeTable) {
_socket.add(_generator.setGlobalCodeTable(codeTable));
}
void setGlobalFont(PosFontType font, {int maxCharsPerLine}) {
_socket
.add(_generator.setGlobalFont(font, maxCharsPerLine: maxCharsPerLine));
}
void setStyles(PosStyles styles, {bool isKanji = false}) {
_socket.add(_generator.setStyles(styles, isKanji: isKanji));
}
void rawBytes(List<int> cmd, {bool isKanji = false}) {
_socket.add(_generator.rawBytes(cmd, isKanji: isKanji));
}
void emptyLines(int n) {
_socket.add(_generator.emptyLines(n));
}
void feed(int n) {
_socket.add(_generator.feed(n));
}
void cut({PosCutMode mode = PosCutMode.full}) {
_socket.add(_generator.cut(mode: mode));
}
void printCodeTable({String codeTable}) {
_socket.add(_generator.printCodeTable(codeTable: codeTable));
}
void beep({int n = 3, PosBeepDuration duration = PosBeepDuration.beep450ms}) {
_socket.add(_generator.beep(n: n, duration: duration));
}
void reverseFeed(int n) {
_socket.add(_generator.reverseFeed(n));
}
void row(List<PosColumn> cols) {
_socket.add(_generator.row(cols));
}
void image(Image imgSrc, {PosAlign align = PosAlign.center}) {
_socket.add(_generator.image(imgSrc, align: align));
}
void imageRaster(
Image image, {
PosAlign align = PosAlign.center,
bool highDensityHorizontal = true,
bool highDensityVertical = true,
PosImageFn imageFn = PosImageFn.bitImageRaster,
}) {
_socket.add(_generator.imageRaster(
image,
align: align,
highDensityHorizontal: highDensityHorizontal,
highDensityVertical: highDensityVertical,
imageFn: imageFn,
));
}
void barcode(
Barcode barcode, {
int width,
int height,
BarcodeFont font,
BarcodeText textPos = BarcodeText.below,
PosAlign align = PosAlign.center,
}) {
_socket.add(_generator.barcode(
barcode,
width: width,
height: height,
font: font,
textPos: textPos,
align: align,
));
}
void qrcode(
String text, {
PosAlign align = PosAlign.center,
QRSize size = QRSize.Size4,
QRCorrection cor = QRCorrection.L,
}) {
_socket.add(_generator.qrcode(text, align: align, size: size, cor: cor));
}
void drawer({PosDrawer pin = PosDrawer.pin2}) {
_socket.add(_generator.drawer(pin: pin));
}
void hr({String ch = '-', int len, int linesAfter = 0}) {
_socket.add(_generator.hr(ch: ch, linesAfter: linesAfter));
}
void textEncoded(
Uint8List textBytes, {
PosStyles styles = const PosStyles(),
int linesAfter = 0,
int maxCharsPerLine,
}) {
_socket.add(_generator.textEncoded(
textBytes,
styles: styles,
linesAfter: linesAfter,
maxCharsPerLine: maxCharsPerLine,
));
}
// ************************ (end) Printer Commands ************************
}
/*
* esc_pos_printer
* Created by Andrey Ushakov
*
* Copyright (c) 2019-2020. All rights reserved.
* See LICENSE for distribution and usage details.
*/
import 'dart:io';
import 'package:esc_pos_utils/esc_pos_utils.dart';
import './enums.dart';
/// Printer Network Manager
class PrinterNetworkManager {
String _host;
int _port;
Duration _timeout;
/// Select a network printer
///
/// [timeout] is used to specify the maximum allowed time to wait
/// for a connection to be established.
void selectPrinter(
String host, {
int port = 9100,
Duration timeout = const Duration(seconds: 5),
}) {
_host = host;
_port = port;
_timeout = timeout;
}
Future<PosPrintResult> printTicket(Ticket ticket) {
if (_host == null || _port == null) {
return Future<PosPrintResult>.value(PosPrintResult.printerNotSelected);
} else if (ticket == null || ticket.bytes.isEmpty) {
return Future<PosPrintResult>.value(PosPrintResult.ticketEmpty);
}
return Socket.connect(_host, _port, timeout: _timeout)
.then((Socket socket) {
socket.add(ticket.bytes);
socket.destroy();
return Future<PosPrintResult>.value(PosPrintResult.success);
}).catchError((dynamic e) {
return Future<PosPrintResult>.value(PosPrintResult.timeout);
});
}
}
name: esc_pos_printer name: esc_pos_printer
description: The library allows to print receipts using an ESC/POS thermal WiFi printer. description: The library allows to print receipts using an ESC/POS thermal WiFi printer.
version: 3.2.9 version: 4.0.0
homepage: https://github.com/andrey-ushakov/esc_pos_printer homepage: https://github.com/andrey-ushakov/esc_pos_printer
environment: environment:
...@@ -9,7 +9,7 @@ environment: ...@@ -9,7 +9,7 @@ environment:
dependencies: dependencies:
flutter: flutter:
sdk: flutter sdk: flutter
esc_pos_utils: ^0.4.9 esc_pos_utils: ^1.0.0
# esc_pos_utils: # esc_pos_utils:
# path: ../esc_pos_utils # path: ../esc_pos_utils
charset_converter: ^1.0.3 charset_converter: ^1.0.3
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment