Open rudroanik opened 1 year ago
@MohsinP07 Yes, I found the solution
@rudroanik How did you solved it can you suggest me?
@MohsinP07 You could not print direct bangla text in thermal printer. You have to convert text to image. Here is method for convert string to image.
Future<Uint8List> generateImageFromString(
String text,double xOffset,{bool isUnderline = false,double fontSize=22}) async {
PictureRecorder recorder = PictureRecorder();
Canvas canvas = Canvas(
recorder,
Rect.fromCenter(
center: Offset(0, 0),
width: 550,
height: 550, // cheated value, will will clip it later...
));
TextSpan span = TextSpan(
style: TextStyle(
color: Colors.black,
fontSize: fontSize,
decoration:TextDecoration.none,
),
text: text,
);
TextPainter tp = TextPainter(
text: span,
maxLines: 8,
textDirection: TextDirection.ltr);
tp.layout(minWidth: 400, maxWidth: 500);
tp.paint(canvas, Offset(xOffset, 0.0));
final picture = recorder.endRecording();
final pngBytes = await picture.toImage(
tp.size.width.toInt(),
tp.size.height.toInt(), // decrease padding
);
final byteData = await pngBytes.toByteData(format: ImageByteFormat.png);
return byteData!.buffer.asUint8List();
}
@rudroanik Hey Brother I tried but i am getting error as
Failed to handle method call E/MethodChannel#bluetooth_thermal_printer( 7453): java.lang.ClassCastException: byte[] cannot be cast to java.util.List E/MethodChannel#bluetooth_thermal_printer( 7453): at com.peoplewareinnovations.bluetooth_thermal_printer.BluetoothThermalPrinterPlugin.onMethodCall(BluetoothThermalPrinterPlugin.kt:106) E/MethodChannel#bluetooth_thermal_printer( 7453): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:258) E/MethodChannel#bluetooth_thermal_printer( 7453): at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295) E/MethodChannel#bluetooth_thermal_printer( 7453): at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$DartMessenger(DartMessenger.java:322) E/MethodChannel#bluetooth_thermal_printer( 7453): at io.flutter.embedding.engine.dart.-$$Lambda$DartMessenger$2j2MERcK825A5j1fv5sZ7xB2Iuo.run(Unknown Source:12) E/MethodChannel#bluetooth_thermal_printer( 7453): at android.os.Handler.handleCallback(Handler.java:938) E/MethodChannel#bluetooth_thermal_printer( 7453): at android.os.Handler.dispatchMessage(Handler.java:99) E/MethodChannel#bluetooth_thermal_printer( 7453): at android.os.Looper.loop(Looper.java:246) E/MethodChannel#bluetooth_thermal_printer( 7453): at android.app.ActivityThread.main(ActivityThread.java:8653) E/MethodChannel#bluetooth_thermal_printer( 7453): at java.lang.reflect.Method.invoke(Native Method) E/MethodChannel#bluetooth_thermal_printer( 7453): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602) E/MethodChannel#bluetooth_thermal_printer( 7453): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130) I/flutter ( 7453): Failed to write bytes: 'byte[] cannot be cast to java.util.List'.
Here is my code
`Future<void> printTicket(
CollectionItem collectionItem, Map _depositor) async {
String? isConnected = await BluetoothThermalPrinter.connectionStatus;
print('printing connection status');
print(isConnected);
if (isConnected == "true") {
List<int> bytes = await generateImageFromString();
final result = await BluetoothThermalPrinter.writeBytes(bytes);
print("Print $result");
} else {
await BluetoothThermalPrinter.connect(
bluetoothBox.get('printerCredentials')['macAddr']);
List<int> bytes = await generateImageFromString();
final result = await BluetoothThermalPrinter.writeBytes(bytes);
print("Print $result");
//Hadnle Not Connected Senario
}
}
Future<Uint8List> generateImageFromString() async {
ui.PictureRecorder recorder = ui.PictureRecorder();
Canvas canvas = Canvas(
recorder,
Rect.fromCenter(
center: Offset(0, 0),
width: 550,
height: 550, // cheated value, will will clip it later...
));
TextSpan span = TextSpan(
style: TextStyle(
color: Colors.black,
fontSize: 14,
decoration: TextDecoration.none,
),
text: "मराठी",
);
TextPainter tp = TextPainter(
text: span, maxLines: 8, textDirection: ui.TextDirection.ltr);
tp.layout(minWidth: 400, maxWidth: 500);
tp.paint(canvas, Offset(4, 0.0));
final picture = recorder.endRecording();
final pngBytes = await picture.toImage(
tp.size.width.toInt(),
tp.size.height.toInt(), // decrease padding
);
final byteData = await pngBytes.toByteData(format: ui.ImageByteFormat.png);
return byteData!.buffer.asUint8List();
}`
@MohsinP07
static Future<List<int>> thermalPrint(Pass your parameter) async {
List<int> bytes = [];
CapabilityProfile profile = await CapabilityProfile.load();
final generator = Generator(PaperSize.mm58, profile);
final imageBytes = await generateImageFromString("রশিদ", 90.0, fontSize: 26);
bytes += generator.image(decodeImage(imageBytes)!, align: PosAlign.right);
bytes += generator.cut();
return bytes;
}
final List<int> bytes = await thermalPrint(Pass your parameter);
await BluetoothThermalPrinter.writeBytes(bytes);
Use this package for Generator https://pub.dev/packages/esc_pos_utils
@rudroanik Thanks a lot bro solved this issue!.
One more thing how can i remove the unwanted padding which i am getting at the start and end of the content which is basically wasting the paper at large extend how can keep height according to content i changed this height
Canvas canvas = Canvas( recorder, Rect.fromCenter( center: Offset(0, 0), width: 550, height: 550, // cheated value, will will clip it later... ));
but still the issue occurs that i am getting unwanted spaces at start and end of the content.
Hey @rudroanik, any solution for this??
Hey @rudroanik I am also facing the same issue, Did you solved this?