sendZplToPrinter method
Sends ZPL code to the printer
macAddress MAC address of the printer
zplData ZPL code
Returns result message if successful, throws an error if failed
Implementation
Future<String> sendZplToPrinter(String macAddress, String zplData) async {
if (macAddress.isEmpty) {
throw Exception("MAC address cannot be empty.");
}
try {
// If ZPL code already starts with ^XA and ends with ^XZ, send as is
final String finalZplToSend;
if (zplData.trim().startsWith("^XA") && zplData.trim().endsWith("^XZ")) {
finalZplToSend = zplData;
} else {
// Otherwise, wrap the ZPL code between ^XA and ^XZ
const String initCommands = "^XA";
finalZplToSend = "$initCommands$zplData^XZ";
}
final String result = await _channel.invokeMethod('printLabel', {'address': macAddress, 'data': finalZplToSend});
return result;
} on PlatformException catch (e) {
throw Exception("Print Error (${e.code}): ${e.message}");
}
}