skip to Main Content

I have a flutter application that makes use of a thermal printer, when the following format is printed:

C. Order
1 Order one description long too and more
2 Order two description long too and more

If the column order is quite long, only the part where it reaches the row is printed.
I am using the following packages for printing:

  • print_bluetooth_thermal: ^1.0.9
  • esc_pos_utils: ^1.1.0
    This piece of code is where the print command is added.
final Generator ticket = Generator(paper, profile);
List<int> bytes = [];
String product = _cleanText(detail['product'].toString());
bytes += ticket.row([
        PosColumn(text: detail['amount'].toString(), width: 1),
        PosColumn(text: product, width: 11)
]);

It is required that if the order does not reach the row, it is printed on the next line.
The result should be like this:

C. Order
1 Order one description long
too and more
2 Order two description long
too and more

2

Answers


  1. To print text in more than one row on a thermal printer in a Flutter application, you might need to split the text into multiple lines manually and then print each line separately. You can create a function to handle the splitting of the text based on the maximum width of the text that the printer can handle on a single line.

    Here’s how you could modify your code:

    import 'package:esc_pos_utils/esc_pos_utils.dart';
    import 'package:print_bluetooth_thermal/print_bluetooth_thermal.dart';
    
    // Function to split the text into multiple lines
    List<String> splitText(String text, int maxWidth) {
      List<String> lines = [];
      while (text.length > maxWidth) {
        int index = text.lastIndexOf(' ', maxWidth);
        if (index == -1) index = maxWidth;
        lines.add(text.substring(0, index));
        text = text.substring(index).trim();
      }
      lines.add(text);
      return lines;
    }
    
    // Printing function
    void printOrder(Map detail, CapabilityProfile profile) {
      const PaperSize paper = PaperSize.mm80;
      final Generator ticket = Generator(paper, profile);
      List<int> bytes = [];
      
      String product = _cleanText(detail['product'].toString());
      List<String> productLines = splitText(product, 20); // assuming max 20 chars per line
      
      bytes += ticket.text('${detail['amount']}');
      for (String line in productLines) {
        bytes += ticket.text(line);
      }
      
      // Send bytes to the printer
      // ... your printing code goes here ...
    }
    
    Login or Signup to reply.
  2. use log instead of print. Remember to import import ‘dart:developer’ as developer.

     import 'dart:developer' as developer;
        
        void main() {
          var myCustomObject = MyCustomObject();
        
          developer.log(
            'log me',
            name: 'my.app.category',
            error: jsonEncode(myCustomObject),
          );
        }
       
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search