A Flutter plugin for discovering and printing to network printers. This plugin allows you to find printers on your local network, connect to them, and send raw printing commands. You can generate ticket data using the flutter_esc_pos_utils package, which provides utilities for generating ESC/POS commands.
- Discover network printers on the local network
- Check printer availability
- Connect to network printers
- Send raw data for printing
- Cross-platform support (Android, iOS, macOS, Windows, Linux)
Add this to your package's pubspec.yaml
file:
dependencies:
flutter_net_printer: ^1.0.1
Then run:
flutter pub get
Add the following permissions to your AndroidManifest.xml
file:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
For Android 10 (API level 29) and above, add the following to your manifest's <application>
tag:
<application
android:usesCleartextTraffic="true">
</application>
Add the following to your Info.plist
file:
<key>NSLocalNetworkUsageDescription</key>
<string>This app needs access to find and connect to printers on your local network</string>
<key>NSBonjourServices</key>
<array>
<string>_ipp._tcp</string>
<string>_pdl-datastream._tcp</string>
</array>
Add the following to your Info.plist
file:
<key>NSLocalNetworkUsageDescription</key>
<string>This app needs access to find and connect to printers on your local network</string>
<key>NSBonjourServices</key>
<array>
<string>_ipp._tcp</string>
<string>_pdl-datastream._tcp</string>
</array>
Also, add the network capability to your macOS app by updating your macos/Runner/DebugProfile.entitlements
and macos/Runner/Release.entitlements
files:
<key>com.apple.security.network.client</key>
<true/>
<key>com.apple.security.network.server</key>
<true/>
No special setup required beyond standard Flutter setup for these platforms.
import 'package:flutter_net_printer/flutter_net_printer.dart';
import 'package:flutter_net_printer/model/network_device.dart';
final printer = FlutterNetPrinter();
Stream<List<NetworkDevice>> printers = printer.discoverPrinters(
port: 9100,
timeout: Duration(seconds: 1),
);
printers.listen((devices) {
for (var device in devices) {
print('Printer found: ${device.address}:${device.port}');
}
});
NetworkDevice? device = await printer.isDeviceAvailable(
'192.168.1.100',
9100,
Duration(seconds: 1),
);
if (device != null) {
print('Printer is available at ${device.address}:${device.port}');
} else {
print('Printer is not available');
}
NetworkDevice? connectedDevice = await printer.connectToPrinter(
'192.168.1.100',
9100,
timeout: Duration(seconds: 5),
);
if (connectedDevice != null) {
print('Connected to printer at ${connectedDevice.address}:${connectedDevice.port}');
} else {
print('Failed to connect to printer');
}
// Using ESC/POS commands with flutter_esc_pos_utils package
import 'package:flutter_esc_pos_utils/flutter_esc_pos_utils.dart';
final profile = PaperSize.mm80;
final generator = Generator(PaperSize.mm80);
List<int> bytes = [];
bytes += generator.text('Hello World!');
bytes += generator.cut();
await printer.printBytes(data: bytes);
await printer.disconnect();
print('Disconnected from printer');
try {
await printer.sendData(
'192.168.1.100',
9100,
[/* your byte data here */],
Duration(seconds: 1),
);
print('Data sent successfully');
} catch (e) {
print('Failed to send data: $e');
}
See the complete example here
This plugin should be works with network printers that support raw socket printing. Tested on:
- Thermal printers Iware
Feel free to test with other models and report compatibility through Pull Requests or Issues.
- This plugin only works with network printers and does not support Bluetooth or USB printers
- Requires the printer to be on the same network as the device
- The plugin does not include ESC/POS command generation (use with
flutter_esc_pos_utils
for a complete solution)
-
Cannot find printers on the network
- Ensure the printer is powered on and connected to the same network
- Verify that the correct permissions are added for your platform
- Check if the printer has a static IP address
-
Connection is refused
- Verify that port 9100 (or your specified port) is open on the printer
- Check printer's network settings
- Some networks may block raw socket connections
-
Printing garbage or incorrect characters
- Ensure you're using the correct ESC/POS commands for your printer model
- Check the printer's encoding settings
Contributions to the Flutter Net Printer plugin are welcome! If you'd like to contribute:
- Fork the repository
- Create a new branch for your feature (
git checkout -b feature/amazing-feature
) - Make your changes
- Run tests to ensure everything works as expected
- Commit your changes (
git commit -m 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
Please make sure your code follows the project's style guidelines and includes appropriate tests.
This project is licensed under the MIT License - see the LICENSE file for details.