esc_pos_printer
esc_pos_printer copied to clipboard
Image print not completed on T1 device,
I tested Wireless printer on T1 device, The image doesn't print completely. It works good on one of my android device.
When I test on my samsung device, small height image getting print correctly, but when the image height is getting bigger, it prints only some above part of image.
Here is a code i wrote to split byes into chunk, it works with big data:
Future<PosPrintResult> printLongTicket(Ticket ticket, { int chunkSizeBytes = 50000, int queueSleepTimeMs = 1000 }) async {
if (_host == null || _port == null) {
return Future<PosPrintResult>.value(PosPrintResult.printerNotSelected);
} else if (ticket == null || ticket.bytes.isEmpty) {
return Future<PosPrintResult>.value(PosPrintResult.ticketEmpty);
}
final List<List<int>> chunks = [];
final len = ticket.bytes.length;
//check if total length is lower than chunkSize
if(ticket.bytes.length <= chunkSizeBytes){
chunks.add( ticket.bytes );
}else{
for (var i = 0; i < len; i += chunkSizeBytes) {
final end = (i + chunkSizeBytes < len) ? i + chunkSizeBytes : len;
chunks.add(ticket.bytes.sublist(i, end));
}
}
try {
final Duration _timeout = Duration(milliseconds: (chunks.length * queueSleepTimeMs) + 5000); //5000 is more delay to avoid error
final Socket socket = await Socket.connect(_host,_port,timeout: _timeout,);
for (List<int> data in chunks) {
socket.add(data);
sleep(Duration(milliseconds: queueSleepTimeMs));
}
socket.destroy();
print('size ${ticket.bytes.length}');
return Future<PosPrintResult>.value(PosPrintResult.success);
} catch (e) {
print('Error: $e');
return Future<PosPrintResult>.value(PosPrintResult.timeout);
}
}
Thanks, @kechankrisna. I was thinking about adding delays too. I think this could be simpler:
int start = 0;
List<int> bytes;
do {
int end = start + 8192;
if (end > receipt.bytes.length) {
end = receipt.bytes.length;
}
bytes = receipt.bytes.sublist(start, end);
start += 8192;
await _bluetoothManager.writeData(bytes);
if (receipt.bytes.length > 8192) {
await _wait(5);
}
}
while (start < receipt.bytes.length);