THERMAL PRINTER FLUTTER
Flutter plugin for thermal printing with support for multiple platforms and connection types.
Support
Platform | USB | Bluetooth | Network |
---|---|---|---|
Android | ❌ | ✅ | ✅ |
iOS | ❌ | ✅ | ✅ |
macOS | ❌ | ✅ | ✅ |
Windows | ✅ | 🚧 | ✅ |
Linux | ❌ | ❌ | ✅ |
Web | ❌ | ❌ | 🚧 |
Project Setup
Android
- Add the following permissions to the
android/app/src/main/AndroidManifest.xml
file:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
- For Android 12 or higher, also add:
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
iOS
- Add the following keys to the
ios/Runner/Info.plist
file:
<key>NSBluetoothAlwaysUsageDescription</key>
<string>We need Bluetooth access to connect to thermal printers</string>
<key>NSBluetoothPeripheralUsageDescription</key>
<string>We need Bluetooth access to connect to thermal printers</string>
- For iOS 13 or higher, also add:
<key>NSBluetoothAlwaysAndWhenInUseUsageDescription</key>
<string>We need Bluetooth access to connect to thermal printers</string>
macOS
- Add the following keys to the
macos/Runner/Info.plist
file:
<key>NSBluetoothAlwaysUsageDescription</key>
<string>We need Bluetooth access to connect to thermal printers</string>
<key>NSBluetoothPeripheralUsageDescription</key>
<string>We need Bluetooth access to connect to thermal printers</string>
- Add the following keys to the
macos/Runner/DebugProfile.entitlements
file:
<key>com.apple.security.device.bluetooth</key>
<true/>
- Add the following keys to the
macos/Runner/Release.entitlements
file:
<key>com.apple.security.device.bluetooth</key>
<true/>
Windows
- For USB printers, ensure the printer drivers are installed.
- For Bluetooth printers, Windows must support Bluetooth LE (Bluetooth 4.0 or higher).
Linux
- For network printers, ensure that the firewall allows connections on port 9100 (or the configured port).
Web
- For network printers, ensure that the web server allows WebSocket connections.
- Add the following script to the
web/index.html
file:
<script>
if ("serviceWorker" in navigator) {
window.addEventListener("flutter-first-frame", function () {
navigator.serviceWorker.register("flutter_service_worker.js");
});
}
</script>
Usage
import 'package:thermal_printer_flutter/thermal_printer_flutter.dart';
// Create an instance of the plugin
final thermalPrinter = ThermalPrinterFlutter();
Printer? _selectedPrinter;
// Only Android, Ios, Macos
final bluetoothPrinters = await thermalPrinter.getPrinters(printerType: PrinterType.bluethoot);
// Only Windows
final usbPrinters = await thermalPrinter.getPrinters(printerType: PrinterType.usb);
// Connect to a printer only bluethoot
final connected = await thermalPrinter.connect(printer: selectedPrinter);
Future<void> _printTest({required ThermalPrinterFlutter termalPrinter, required Printer printer}) async {
try {
final generator = Generator(PaperSize.mm80, await CapabilityProfile.load());
List<int> bytes = [];
bytes += generator.text('Print Test',
styles: const PosStyles(
align: PosAlign.center,
bold: true,
height: PosTextSize.size2,
width: PosTextSize.size2,
));
bytes += generator.feed(2);
bytes += generator.text('Date: ${DateTime.now()}');
bytes += generator.feed(2);
bytes += generator.text('This is a test print');
bytes += generator.feed(2);
bytes += generator.cut();
await termalPrinter.printBytes(bytes: bytes, printer: printer);
} catch (e) {
print('Error printing: $e');
}
}
Dependency Compatibility
If you are using the packages below, we recommend using these specific versions for better compatibility:
dependencies:
web: ^0.5.1
image: ^4.5.4
Example
Check out the complete example at example/lib/main.dart
for a sample implementation with a graphical interface.
Contribution
Contributions are welcome! Feel free to open issues or submit pull requests.
License
This project is licensed under the MIT License - see the LICENSE file for details.