A lightweight and efficient C++ library for controlling thermal printers over TCP/IP using the ESC/POS protocol. This library features a modern, fluent builder-pattern API that makes receipt formatting and printer control intuitive and clean.
- Network Printing: Connect to any thermal printer via IP Address and Port (default: 9100).
- Fluent API: Chain methods for a readable, builder-like syntax.
- Text Formatting:
- Alignment (Left, Center, Right).
- Font styling (Bold, Underline, Double Strike).
- Dynamic Font Sizing (Multipliers for width and height).
- Graphics & Codes:
- QR Code generation.
- Barcode (Code128) support.
- Hardware Control:
- Paper cutting (Full/Partial/Feed-and-Cut).
- Cash drawer triggering (DK port).
- Flexibility: Easily switch between 58mm and 80mm paper widths.
- Low-Level Access: Direct access to raw ESC/POS byte commands if needed.
- C++17 or higher.
- CMake 3.10 or higher.
- A thermal printer that supports ESC/POS over TCP (e.g., Epson, Xprinter, SNBC, etc.).
mkdir build
cd build
cmake ..
make#include "include/socketservice.hpp"
#include "include/thermalprinter.hpp"
using namespace PrinterThermal;
int main() {
try {
// 1. Initialize Connection
SocketService socket("10.10.1.197", 9100);
socket.connectToPrinter();
// 2. Initialize Printer
ThermalPrinter printer(socket);
printer.init()
.alignCenter()
.fontSize(2, 2)
.bold(true)
.println("HELLO WORLD")
.bold(false)
.fontSizeNormal()
.println("Professional Receipt System")
.separator(48) // 48 chars for 80mm
.row("Item Name", "Price", 48)
.separator(48)
.row("C++ Library", "FREE", 48)
.feed(2)
.qrCode("https://github.com/hylmi")
.cut(4); // Feed 4 lines and cut
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
}
return 0;
}include/: Header files.escpos.hpp: Low-level ESC/POS command definitions.socketservice.hpp: TCP socket wrapper.thermalprinter.hpp: High-level fluent API wrapper.
src/: Implementation files.sockerservice.cpp: Socket implementation.main.cpp: Demo and testing entry point.
CMakeLists.txt: Build configuration.
In src/main.cpp, you can adjust the PAPER_WIDTH constant to match your printer:
- 58mm paper: Use
32. - 80mm paper: Use
48.
const uint8_t PAPER_WIDTH = 48; // For 80mmContributions are welcome! If you find a bug or have a feature request, please open an issue or submit a pull request.
This project is licensed under the MIT License - see the LICENSE file for details.