I converted the widget into an image and then printed it, I do not know what the problem is for the image to appear with these strange texts, but I tried repeatedly to solve the problem and unfortunately I could not, so I hope you will help me to print the image perfectly with the printers
My Printer Model : Citypos mini printer cp-p100u
and my code :
void pos_print() {
screenshotController.capture().then((Uint8List? image) async {
if (image == null) return null;
Completer<PrinterDevice> completer = Completer<PrinterDevice>();
Future.delayed(Duration.zero, () {
PrinterManager.instance
.discovery(type: PrinterType.usb)
.listen((printer) {
completer.complete(printer);
});
});
msg_alert("Searching for printers ...");
PrinterDevice printer;
try {
printer = await completer.future.timeout(Duration(seconds: 10));
} on TimeoutException catch (e) {
msg_alert("We dont Find Any Printer");
return;
}
msg_alert("We Found the printer , Now We Connect with it");
try {
await PrinterManager.instance
.connect(
type: PrinterType.usb,
model: UsbPrinterInput(
name: printer.name,
productId: printer.productId,
vendorId: printer.vendorId))
.timeout(Duration(seconds: 5));
} on TimeoutException catch (e) {
msg_alert("Failed To Connect With Printer");
return;
}
msg_alert("Printing ...");
List<int> bytes = [];
final generator =
Generator(PaperSize.mm80, await CapabilityProfile.load());
bytes += generator.feed(1);
bytes += generator.image(img.decodePng(image)!, align: PosAlign.center);
bytes += generator.feed(1);
bytes += generator.cut(mode: PosCutMode.partial);
bool success;
try {
success = await PrinterManager.instance
.send(type: PrinterType.usb, bytes: bytes)
.timeout(Duration(seconds: 5));
} on TimeoutException catch (e) {
success = false;
}
if (success)
msg_alert("Print success");
else
msg_alert("print error");
}).catchError((onError) {
print(onError);
});
}
Fix the Image Issue