How to Print Receipts with ESCPOS Printers in Flutter and Dart

Sep 27, 2021 | Programming

If you’ve ever wanted to print receipts using an ESCPOS thermal printer, whether through WiFi or Ethernet, you’re in luck! The esc_pos_printer library allows you to accomplish this seamlessly with both Android and iOS in your Flutter projects. For Bluetooth printing, there’s also a dedicated library called esc_pos_bluetooth.

Getting Started

Before diving into the code, ensure you have the right setup:

  • Install the esc_pos_printer package.
  • Make sure your ESCPOS printer is connected to the same network.
  • If you need to discover available printers on your network, consider using the ping_discover_network package.

Generating a Simple Receipt

Generating a receipt is like crafting a work of art—from text alignment to font styles, take a look at this Dart method:

void testReceipt(NetworkPrinter printer) {
    printer.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');
    printer.text('Special 1: àÀ èÈ éÉ ûÛ üÜ çÇ ôÔ', styles: PosStyles(codeTable: CP1252));
    printer.text('Special 2: blåbærgrød', styles: PosStyles(codeTable: CP1252));
    printer.text('Bold text', styles: PosStyles(bold: true));
    printer.text('Reverse text', styles: PosStyles(reverse: true));
    printer.text('Underlined text', styles: PosStyles(underline: true), linesAfter: 1);
    printer.text('Align left', styles: PosStyles(align: PosAlign.left));
    printer.text('Align center', styles: PosStyles(align: PosAlign.center));
    printer.text('Align right', styles: PosStyles(align: PosAlign.right), linesAfter: 1);
    printer.text('Text size 200%', styles: PosStyles(height: PosTextSize.size2, width: PosTextSize.size2));
    printer.feed(2);
    printer.cut();
}

Now imagine you’re preparing a delightful platter of food. Each style—from normal text to bold and underlined—represents different dishes that complement each other. Just like a balanced meal, each line enhances the overall receipt presentation.

Connecting to the Printer

The next step is connecting to the printer. Here’s how you can do that:

import 'package:esc_pos_printer/esc_pos_printer.dart';

const PaperSize paper = PaperSize.mm80;
final profile = await CapabilityProfile.load();
final printer = NetworkPrinter(paper, profile);
final PosPrintResult res = await printer.connect('192.168.0.123', port: 9100);

if (res == PosPrintResult.success) {
    await testReceipt(printer);
}
printer.disconnect();
print('Print result: ${res.msg}');

In this setup, we establish a connection to the printer through its IP address (like dialing a phone number). Then, if the connection is successful, we execute our receipt printing method!

Troubleshooting Tips

If you encounter any issues while trying to set up or print from your ESCPOS printer, consider the following:

  • Ensure the printer is powered on and connected to the network.
  • Check if the correct IP address and port are being used.
  • Try using the default port 9100 if you haven’t changed it.
  • If you’re unable to connect, confirm there’s no firewall blocking the connection.
  • For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Additionally, you can refer to the built-in methods provided in the esc_pos_utils repository for more examples on creating various printed outputs.

Contributing to the Library

Your participation in enhancing this library is always welcome! Whether through testing your printers or contributing new features, every little bit counts.

Final Thoughts

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox