Giter Club home page Giter Club logo

Comments (3)

rifathossain82 avatar rifathossain82 commented on September 22, 2024

Do you find any solution?

from bluetooth_print.

charlie250990 avatar charlie250990 commented on September 22, 2024

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

from bluetooth_print.

KawindaWAD avatar KawindaWAD commented on September 22, 2024

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);

from bluetooth_print.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.