Connect Thermal printer and print using Flutter App
Packages we will be using to connect and print through Thermal Printer using Flutter App
- bluetooth_thermal_printer to connect with Thermal Printer
- esc_pos_utils package to print receipts
- Image to print images
Step 1
import 'package:bluetooth_thermal_printer/bluetooth_thermal_printer.dart';
import 'package:esc_pos_utils/esc_pos_utils.dart';
Step 2
Get your Thermal Printer mac address ready
You can get it under bluetooth in your Phone or print a test page through printer.
Call this Function
Future<void> connectThermalPrinter(String mac) async {
print(mac);
result = await BluetoothThermalPrinter.connect(mac);
if (result == "true") {
setState(() {
connected = true;
});
}
}
Step 3
Design your Receipt through this function
Future<Ticket> getTicket() async { CapabilityProfile profile = await CapabilityProfile.load(); final Ticket ticket = Ticket(PaperSize.mm80, profile); ticket.text("Demo Shop", styles: PosStyles( align: PosAlign.center, height: PosTextSize.size2, width: PosTextSize.size2, ), linesAfter: 1); ticket.text( "18th Main Road, 2nd Phase, J. P. Nagar, Bengaluru, Karnataka 560078", styles: PosStyles(align: PosAlign.center)); ticket.text('Tel: +919591708470', styles: PosStyles(align: PosAlign.center)); ticket.hr(); ticket.row([ PosColumn( text: 'No', width: 1, styles: PosStyles(align: PosAlign.left, bold: true)), PosColumn( text: 'Item', width: 5, styles: PosStyles(align: PosAlign.left, bold: true)), PosColumn( text: 'Price', width: 2, styles: PosStyles(align: PosAlign.center, bold: true)), PosColumn( text: 'Qty', width: 2, styles: PosStyles(align: PosAlign.center, bold: true)), PosColumn( text: 'Total', width: 2, styles: PosStyles(align: PosAlign.right, bold: true)), ]); ticket.row([ PosColumn(text: "1", width: 1), PosColumn( text: "Tea", width: 5, styles: PosStyles( align: PosAlign.left, )), PosColumn( text: "10", width: 2, styles: PosStyles( align: PosAlign.center, )), PosColumn(text: "1", width: 2, styles: PosStyles(align: PosAlign.center)), PosColumn(text: "10", width: 2, styles: PosStyles(align: PosAlign.right)), ]); ticket.row([ PosColumn(text: "2", width: 1), PosColumn( text: "Sada Dosa", width: 5, styles: PosStyles( align: PosAlign.left, )), PosColumn( text: "30", width: 2, styles: PosStyles( align: PosAlign.center, )), PosColumn(text: "1", width: 2, styles: PosStyles(align: PosAlign.center)), PosColumn(text: "30", width: 2, styles: PosStyles(align: PosAlign.right)), ]); ticket.row([ PosColumn(text: "3", width: 1), PosColumn( text: "Masala Dosa", width: 5, styles: PosStyles( align: PosAlign.left, )), PosColumn( text: "50", width: 2, styles: PosStyles( align: PosAlign.center, )), PosColumn(text: "1", width: 2, styles: PosStyles(align: PosAlign.center)), PosColumn(text: "50", width: 2, styles: PosStyles(align: PosAlign.right)), ]); ticket.row([ PosColumn(text: "4", width: 1), PosColumn( text: "Rova Dosa", width: 5, styles: PosStyles( align: PosAlign.left, )), PosColumn( text: "70", width: 2, styles: PosStyles( align: PosAlign.center, )), PosColumn(text: "1", width: 2, styles: PosStyles(align: PosAlign.center)), PosColumn(text: "70", width: 2, styles: PosStyles(align: PosAlign.right)), ]); ticket.hr(); ticket.row([ PosColumn( text: 'TOTAL', width: 6, styles: PosStyles( align: PosAlign.left, height: PosTextSize.size4, width: PosTextSize.size4, )), PosColumn( text: "160", width: 6, styles: PosStyles( align: PosAlign.right, height: PosTextSize.size4, width: PosTextSize.size4, )), ]); ticket.hr(ch: '=', linesAfter: 1); // ticket.feed(2); ticket.text('Thank you!', styles: PosStyles(align: PosAlign.center, bold: true)); ticket.text("26-11-2020 15:22:45", styles: PosStyles(align: PosAlign.center), linesAfter: 1); ticket.text('Note: Goods once sold will not be taken back or exchanged.', styles: PosStyles(align: PosAlign.center, bold: false)); ticket.cut(); return ticket; }
Step 4
Print using Thermal Printer by this Function in Flutter App
Future<void> printTicket() async { String isConnected = await BluetoothThermalPrinter.connectionStatus; if (isConnected == "true") { Ticket ticket = await getTicket(); final result = await BluetoothThermalPrinter.writeBytes(ticket.bytes); print("Print $result"); } else { //Hadnle Not Connected Senario } }
Comments
Post a Comment