flutter_thermal_printer 3.0.0
flutter_thermal_printer: ^3.0.0 copied to clipboard
Plugin for Flutter to print on thermal printers via ESC/POS commands.
flutter_thermal_printer #
Print ESC/POS receipts on thermal printers over Bluetooth Low Energy, USB and the network, from Android, iOS, macOS and Windows.
Buy Me A Coffee #
If you find this project helpful and want to support its development, you can buy me a coffee:
What works where #
| Connection | Android | iOS | macOS | Windows |
|---|---|---|---|---|
| Bluetooth Low Energy | ✅ | ✅ | ✅ | ✅ |
| USB | ✅ | ✅ ¹ | ✅ ¹ | |
| Network (raw TCP, port 9100) | ✅ | ✅ | ✅ | ✅ |
| Installed network print queues | ✅ ¹ | ✅ ¹ |
Every row supports scanning (except raw network, see below), connect,
printData and printWidget.
- Bluetooth means BLE only. Many cheap printers speak only Classic Bluetooth (SPP); they never appear in a scan.
- ¹ On macOS and Windows, USB and network printers are the print queues installed in the OS, so the printer needs a driver (on Windows, for example the XPrinter driver). The plugin sends raw ESC/POS bytes through the spooler.
Install #
flutter pub add flutter_thermal_printer
Requires Flutter 3.38 or later.
Platform setup #
Android #
Bluetooth permissions in android/app/src/main/AndroidManifest.xml:
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" android:usesPermissionFlags="neverForLocation" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" android:maxSdkVersion="28" />
The runtime permission prompt appears when a scan starts. USB needs no
manifest entry: connect() asks the user for access to the printer and
returns false until it is granted. To open your app when a printer is
plugged in, add a USB_DEVICE_ATTACHED intent filter with a
device_filter.xml, as the example app
does.
iOS #
Add to ios/Runner/Info.plist:
<key>NSBluetoothAlwaysUsageDescription</key>
<string>Finds and connects to Bluetooth printers.</string>
macOS #
Add the same NSBluetoothAlwaysUsageDescription key to
macos/Runner/Info.plist, and these entitlements to both
DebugProfile.entitlements and Release.entitlements:
<key>com.apple.security.device.bluetooth</key>
<true/>
<key>com.apple.security.network.client</key>
<true/>
<key>com.apple.security.print</key>
<true/>
Windows #
Install the printer in Windows with its driver. Nothing else is needed.
Quick start #
final thermal = FlutterThermalPrinter.instance;
// Listen first: the list is re-sent whenever a printer appears, leaves or
// changes connection state.
final subscription = thermal.devicesStream.listen((printers) {
setState(() => _printers = printers);
});
try {
await thermal.getPrinters(
connectionTypes: [ConnectionType.BLE, ConnectionType.USB],
);
} catch (e) {
// Bluetooth is off, not permitted, or not supported on this device.
}
// Later, with the printer the user picked:
if (await thermal.connect(printer)) {
final profile = await CapabilityProfile.load();
final generator = Generator(PaperSize.mm58, profile);
final printed = await thermal.printData(printer, [
...generator.text('Hello printer', styles: const PosStyles(bold: true)),
...generator.feed(2),
...generator.cut(),
]);
// Or capture a widget and print it as an image:
await thermal.printWidget(context, printer: printer, widget: receipt);
}
// When you are done scanning:
await thermal.stopScan();
await subscription.cancel();
printData and printWidget return whether every byte reached the printer.
Jobs sent to one printer at the same time print one after another, never
mixed. The example app shows a complete screen.
Network printers #
There is no LAN scan, so create network printers from their IP address, as
host or host:port (port 9100 by default):
final printer = Printer(
name: 'Kitchen',
address: '192.168.1.50:9100',
connectionType: ConnectionType.NETWORK,
);
await FlutterThermalPrinter.instance.connect(printer); // optional
await FlutterThermalPrinter.instance.printData(printer, bytes);
Without connect(), each print opens and closes its own connection.
FlutterThermalPrinterNetwork is still available if you want the socket
client on its own.
macOS and Windows print queues #
Installed queues are typed by where they send jobs:
- macOS, by device URI:
usb://is USB;dnssd://,ipp://,ipps://,http://,https://,socket://,lpd://andsmb://are NETWORK. - Windows, by port:
USB001is USB;IP_…,WSD…and shared queues are NETWORK.
Anything else (PDF, fax, file, serial) has no connectionType and appears
only when a scan asks for both USB and NETWORK. Printer.queueName holds the
queue, Printer.deviceUri the URI or port, and paused or offline queues
report isConnected: false. Queues always print through the OS spooler, even
when typed NETWORK; this does not discover uninstalled LAN printers.
Bluetooth connection timeout #
// For every BLE connection
FlutterThermalPrinter.instance.bleConfig =
const BleConfig(connectionTimeout: Duration(seconds: 3));
// For one call (also used for network printers)
await FlutterThermalPrinter.instance.connect(
printer,
connectionTimeout: const Duration(seconds: 2),
);
The default is 10 seconds for BLE and 5 seconds for network printers.
isBleTurnedOnStream emits the current Bluetooth state and then every change.
Migrating to 3.0.0 #
printDataandprintWidgetreturnbool; check it instead of assuming success.chunkSizeis now the number of bytes per Bluetooth write, not the MTU.connectionStabilizationDelayis nowconnectionTimeout(the old name still works, deprecated).printWidgetno longer takesprintOnBle.getPrinters()throws when Bluetooth is off, not permitted or not supported. Wrap it intry.- Bluetooth device IDs are lower-case (from
universal_ble3.4.2). Compare saved addresses case-insensitively or save them again. - Android USB printers use their device path as
address. Printers saved with the old vendor-ID address still connect and print. - Windows queues report their real type, so a USB-only scan no longer lists PDF, fax or network queues.
- NETWORK printers now work through
connect(),printData()andprintWidget(). PrinterManager.initialize()and the unused platform-interface methods (convertImageToGrayscale,disconnect,stopScan,getPrinters) are gone.
See the changelog for every change.
Printer model #
String? address; // BLE id, USB device path, host[:port] or queue name
String? name;
ConnectionType? connectionType; // BLE, USB, NETWORK, or null for other queues
bool? isConnected;
String? vendorId;
String? productId;
String? deviceUri; // macOS device URI or Windows port of a queue
String? queueName; // macOS or Windows print queue
bool get isSystemPrinter; // true for macOS and Windows queues
Contributing #
Contributions are welcome: fork the repository, make your change and open a pull request. For bugs or feature requests, open an issue.
