thon-ju / bluetooth_print

a flutter plugin connect to bluetooth thermal printer support both Android and IOS (eg. Gprinter pt-380、gp-1324、gp-2120)
MIT License
151 stars 180 forks source link

How do I print two linetext in same line? #138

Open balaji101010 opened 1 year ago

balaji101010 commented 1 year ago
list.add(LineText(
                                    type: LineText.TYPE_TEXT,
                                    content: 'this is conent left',                        
                                    align: LineText.ALIGN_LEFT,
                                    linefeed: 0));
                                list.add(LineText(
                                    type: LineText.TYPE_TEXT,
                                    content: 'this is conent right',
                                    align: LineText.ALIGN_RIGHT,
                                    linefeed: 0));

This prints this is conent leftthis is conent right like this in iOS

But I want this is conent left this is conent right like this

Any help is appreciated!

rifathossain82 commented 1 year ago

Do you find any solution?

charlie250990 commented 3 months ago

Hi, here's the solution:

String totalRow(Map<String, dynamic> order) { String total = "First text to the left"); String grandTotal = "Second text to the right"); int totalLength = total.length; int grandTotalLength = grandTotal.length; int lineLength = 46; // Adjust this value as needed int padding = lineLength - totalLength - grandTotalLength; String paddedTotal = total.padRight(totalLength + padding); String paddedGrandTotal = grandTotal.padLeft(grandTotalLength); return paddedTotal + paddedGrandTotal; }

After just add the print part: printContent.add( LineText( type: LineText.TYPE_TEXT, content: totalRow(order), align: LineText.ALIGN_LEFT, weight: 1, linefeed: 1, ) );

and done, took me long time to find this workaround but it works perfectly
KawindaWAD commented 1 week ago

We need to handle this manually. You can try following template and make any necessary changes.

Map<String, dynamic> config = {};
List<LineText> list = [];

List<Map<String, dynamic>> items = [
  {"name": "Item 1 has a very long text to print. Let's see it can print into 3 lines.", "price": 10.00},
  {"name": "Item 2", "price": 20.00},
  {"name": "Item 3", "price": 30.00},
];

// Add store header
list.add(LineText(
  type: LineText.TYPE_TEXT,
  content: 'Sample Store',
  weight: 2,
  align: LineText.ALIGN_CENTER,
  linefeed: 1,
));
list.add(LineText(
  type: LineText.TYPE_TEXT,
  content: 'Address Line 1',
  align: LineText.ALIGN_CENTER,
  linefeed: 1,
));
list.add(LineText(
  type: LineText.TYPE_TEXT,
  content: 'Phone: 123-456-7890',
  align: LineText.ALIGN_CENTER,
  linefeed: 1,
));
list.add(LineText(
  type: LineText.TYPE_TEXT,
  content: '--------------------------------',
  align: LineText.ALIGN_CENTER,
  linefeed: 1,
));

// Add customer and bill details
list.add(LineText(
  type: LineText.TYPE_TEXT,
  content: 'Customer: John Doe',
  align: LineText.ALIGN_LEFT,
  linefeed: 1,
));
list.add(LineText(
  type: LineText.TYPE_TEXT,
  content: 'Bill No: 123456',
  align: LineText.ALIGN_LEFT,
  linefeed: 1,
));
list.add(LineText(
  type: LineText.TYPE_TEXT,
  content: '--------------------------------',
  align: LineText.ALIGN_CENTER,
  linefeed: 1,
));

// Add items with right-aligned prices and wrapping names
int maxLineWidth = 32; // Adjust based on your printer's character limit per line
for (var item in items) {
  String name = item['name'];
  String price = item['price'].toStringAsFixed(2);

  int availableWidthForName = maxLineWidth - price.length - 2;

  List<String> words = name.split(' ');
  List<String> nameLines = [];
  String currentLine = '';

  for (String word in words) {
    if ((currentLine + word).length <= availableWidthForName) {
      currentLine += (currentLine.isEmpty ? '' : ' ') + word;
    } else {
      nameLines.add(currentLine);
      currentLine = word;
    }
  }
  if (currentLine.isNotEmpty) {
    nameLines.add(currentLine);
  }

  String firstLine = nameLines.first;
  int spacesCount = availableWidthForName - firstLine.length + 2;
  String spaces = ' ' * spacesCount;

  list.add(LineText(
    type: LineText.TYPE_TEXT,
    content: '$firstLine$spaces$price',
    align: LineText.ALIGN_LEFT,
    linefeed: 1,
  ));

  for (int i = 1; i < nameLines.length; i++) {
    list.add(LineText(
      type: LineText.TYPE_TEXT,
      content: nameLines[i],
      align: LineText.ALIGN_LEFT,
      linefeed: 1,
    ));
  }
}

list.add(LineText(
  type: LineText.TYPE_TEXT,
  content: '--------------------------------',
  align: LineText.ALIGN_CENTER,
  linefeed: 1,
));

// Add totals
double subTotal = items.fold(0, (sum, item) => sum + item['price']);
double discount = 5.00; // example discount
double total = subTotal - discount;
double paid = 50.00; // example paid amount
double balance = total - paid;

list.add(LineText(
  type: LineText.TYPE_TEXT,
  content: 'Subtotal: '.padRight(maxLineWidth - total.toStringAsFixed(2).length) + total.toStringAsFixed(2),
  align: LineText.ALIGN_LEFT,
  linefeed: 1,
));
list.add(LineText(
  type: LineText.TYPE_TEXT,
  content: 'Discount: '.padRight(maxLineWidth - discount.toStringAsFixed(2).length) + discount.toStringAsFixed(2),
  align: LineText.ALIGN_LEFT,
  linefeed: 1,
));
list.add(LineText(
  type: LineText.TYPE_TEXT,
  content: 'Total: '.padRight(maxLineWidth - total.toStringAsFixed(2).length) + total.toStringAsFixed(2),
  align: LineText.ALIGN_LEFT,
  weight: 2, // make total bold
  linefeed: 1,
  height: 50
));
list.add(LineText(
  type: LineText.TYPE_TEXT,
  content: 'Paid: '.padRight(maxLineWidth - paid.toStringAsFixed(2).length) + paid.toStringAsFixed(2),
  align: LineText.ALIGN_LEFT,
  linefeed: 1,
));
list.add(LineText(
  type: LineText.TYPE_TEXT,
  content: 'Balance: '.padRight(maxLineWidth - balance.toStringAsFixed(2).length) + balance.toStringAsFixed(2),
  align: LineText.ALIGN_LEFT,
  weight: 2, // make balance bold
  linefeed: 1,
  size: 20,
));

list.add(LineText(
  type: LineText.TYPE_TEXT,
  content: '--------------------------------',
  align: LineText.ALIGN_CENTER,
  linefeed: 1,
));
list.add(LineText(
  type: LineText.TYPE_TEXT,
  content: 'Thank you for your purchase!',
  align: LineText.ALIGN_CENTER,
  linefeed: 1,
));

await bluetoothPrint.printReceipt(config, list);