Comments (3)
Do you find any solution?
from bluetooth_print.
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.
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)
- Connect success then disconnect immediately HOT 12
- App craches, the package bluetooth_print can't print images on ios, it's working in android but doesn't work in iOS HOT 6
- Application is getting crashed when connecting to the printer using bluetooth_print 4.3.0 Dependency in IOS but Working fine in android HOT 1
- Connected device address. HOT 2
- Arabic Support
- slow printing in tsc label printer HOT 1
- Cannot print using printer with TSPL command
- no print in android HOT 1
- Arabic Text support
- when will the new version come out? HOT 2
- Getting error for iOS - duplicate symbols for architecture x86_64 HOT 2
- [iOS] Signature and Logo Print issue HOT 3
- how change the text size like fontSize?
- Can't print after selecting the printer HOT 8
- device not showing HOT 1
- Not able to align image in center of thermal paper bluetooth_print 4.3.0 HOT 3
- can't align image and text in the same row
- Barcode {B all time print
- connectivity status issue
- Print image with IOS & Arabic text HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bluetooth_print.