andrey-ushakov / esc_pos_printer

ESC/POS (thermal, receipt) printing for Flutter & Dart
BSD 3-Clause "New" or "Revised" License
346 stars 298 forks source link

Poscolumn not working #163

Open ameetaujla opened 1 year ago

ameetaujla commented 1 year ago

Hello, I have tested and tried everything but nothing works. I have attached a copy of my test receipt and self-test of my thermal printer, everything is working great but width and pos column not working for me, I have tried on 3 printers same issue with every printer,

please check this Img https://pasteboard.co/lvm8ZnvJKPIm.jpg please help, Thanks, AmeeT

yasngencc commented 2 days ago
  void printCustomRow(
    NetworkPrinter printer,
    String qty,
    String productName,
    String unitPrice,
    String totalPrice, {
    bool isBold = false,
    PosTextSize size = PosTextSize.size1,
  }) {
    const int totalChars = 48;
    const int qtyWidth = 4;
    const int unitPriceWidth = 8;
    const int totalPriceWidth = 8;
    const int productNameWidth =
        totalChars - qtyWidth - unitPriceWidth - totalPriceWidth - 3;

    List<String> wrappedProductName = wrapText(productName, productNameWidth);

    for (int i = 0; i < wrappedProductName.length; i++) {
      if (i == 0) {
        printer.text(
          '${qty.padRight(qtyWidth)}${wrappedProductName[i].padRight(productNameWidth).trimLeft()} ${unitPrice.padLeft(unitPriceWidth)} ${totalPrice.padLeft(totalPriceWidth)}',
          styles: PosStyles(
            align: PosAlign.left,
            bold: isBold,
            height: size,
            width: size,
            codeTable: 'CP1252',
          ),
        );
      } else {
        printer.text(
          '${''.padRight(qtyWidth)}${wrappedProductName[i].padRight(productNameWidth)}',
          styles: PosStyles(
            align: PosAlign.left,
            bold: isBold,
            height: size,
            width: size,
            codeTable: 'CP1252',
          ),
        );
      }
    }
  }

  List<String> wrapText(String text, int width) {
    List<String> lines = [];
    List<String> words = text.split(' ');

    String currentLine = '';

    for (String word in words) {
      if ((currentLine.length + word.length + 1) <= width) {
        if (currentLine.isEmpty) {
          currentLine = word;
        } else {
          currentLine += ' $word';
        }
      } else {
        lines.add(currentLine);
        currentLine = word;
      }
    }

    if (currentLine.isNotEmpty) {
      lines.add(currentLine);
    }

    return lines;
  }
ziddijatts commented 2 days ago

Thank you bro <3

ameetaujla commented 2 days ago

@yasngencc Thank you :) Respect ++++++