-
Notifications
You must be signed in to change notification settings - Fork 74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to make genral setting to find printer terminal with one command #74
Comments
hi @mikebgrep, |
I don't know if it exists escpos printer emulator or simulator, but it is possible. |
Ok thank you I found emulator Just another question when i print the recipient when i don''t do escpos.close() it appear this in the emulator to much free space between each print (I don't know maybe is from emulator) and the name of the market got in cut_full section.If i close escpos.close(); I can't print again any hint here.Regards <<CUT_FULL>>M0My Market Client: John Doe Product Demo product 1 name x3 17.97 TOTAL 17.97Date: 06-11-2021 Time: 12:06:57 |
`M0�E �!"�-0�a1�2�B My Market �M0�E��! �-1�a0�3 �B RECEIPT �M0�E �! �-0�a0�2�B Item Unit Price Amount �M0�E �! �-0�a0�2�B Product Demo product 1 name 5.99 17.97 �M0�E �! �-0�a0�2�B ---------------------------------------- �M0�E��! �-0�a0�3 �B TOTAL 171.93 this is my system.out output how do you think this will print ok on real printer? |
Hi @mikebgrep Just for fun ... |
Hello again @anastaciocintra |
hi, @mikebgrep, import com.github.anastaciocintra.escpos.EscPos;
import com.github.anastaciocintra.escpos.EscPosConst;
import com.github.anastaciocintra.escpos.Style;
import com.github.anastaciocintra.output.PrinterOutputStream;
import javax.print.PrintService;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
class Order {
public String tableNumber;
public ArrayList<OrderProduct> products;
public double totalPrice;
public Order(String tableNumber, ArrayList<OrderProduct> products){
this.tableNumber = tableNumber;
this.products = products;
totalPrice = 143.0;
}
}
class OrderProduct {
public String productCode;
public String productName;
public int productQty;
public double productPrice;
public OrderProduct(String productCode, String productName, int productQty, double productPrice) {
this.productCode = productCode;
this.productName = productName;
this.productQty = productQty;
this.productPrice = productPrice;
}
public double getPrice(int qty) {
return this.productPrice *= qty;
}
@Override
public String toString() {
String productPriceStr = String.format("%.2f", productPrice);
String qtyPriceStr = String.format("%.2f", productPrice / productQty);
return "Product " + productName + " " + qtyPriceStr + " " + productPriceStr + "\n";
}
}
public class MikebGrep {
void printRecipent(String printerName){
// public void loadData() {
ArrayList<OrderProduct> products = new ArrayList();
OrderProduct orderProd = new OrderProduct("4324", "demo product 1", 1,14.4);
OrderProduct orderProd1 = new OrderProduct("4324", "demo product 2", 1,14.4);
OrderProduct orderProd2= new OrderProduct("4324", "demo product 3", 1,14.4);
products.add(orderProd);
products.add(orderProd1);
products.add(orderProd2);
Order order = new Order("1", products);
// }
//this call is slow, try to use it only once and reuse the PrintService variable.
PrintService printService = PrinterOutputStream.getPrintServiceByName(printerName);
try {
PrinterOutputStream printerOutputStream = new PrinterOutputStream(printService);
EscPos escpos = new EscPos(printerOutputStream);
Style title = new Style()
.setFontSize(Style.FontSize._3, Style.FontSize._3)
.setJustification(EscPosConst.Justification.Center);
Style subtitle = new Style(escpos.getStyle())
.setBold(true)
.setUnderline(Style.Underline.OneDotThick);
Style bold = new Style(escpos.getStyle())
.setBold(true);
escpos.writeLF("My Business")
.feed(3)
.writeLF(subtitle, " RECEIPT")
.writeLF(subtitle," ----------------- ")
.feed(2)
.writeLF("Item Unit Price Amount")
.writeLF("----------------------------------------")
.feed(2)
.writeLF(order.products.toString().replace("[", "").replace("]", "").replace(",", "") + "\n")
.writeLF("----------------------------------------")
.feed(2)
.writeLF(bold,
"TOTAL $" + order.totalPrice)
.writeLF("----------------------------------------")
.writeLF(" Thank you for shopping with us ")
.writeLF(" Have a nice day ")
.feed(8)
.cut(EscPos.CutMode.FULL);
// do not forget to close...
escpos.close();
} catch (IOException ex) {
Logger.getLogger(MikebGrep.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void main(String[] args) {
if(args.length!=1){
System.out.println("Usage: java -jar getstart.jar (\"printer name\")");
System.out.println("Printer list to use:");
String[] printServicesNames = PrinterOutputStream.getListPrintServicesNames();
for(String printServiceName: printServicesNames){
System.out.println(printServiceName);
}
System.exit(0);
}
MikebGrep obj = new MikebGrep();
obj.printRecipent(args[0]);
}
} |
Okay that's look kind of good just curious do the second receipt print the same.Because on the emulator cut paper command appear in the market name line.Thank you again is looking nice if you decide to test it with two recipient in a row message me if its ok or not.(Maybe is from emulator ).I see you close the escpos at the end when I do that i got java.io.IOException: Pipe closed and I can't print anymore what I need to do to print again I reuse tcpOutputStream.Thank you |
I am on Ubuntu maybe this is the problem I see same issue described |
Answer: Yeap, its necessary to close and its not reusable,
the stream can be reused?
source: https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html in this case, you need to re-create the instance... // another ticket ... another time...
printerOutputStream = new PrinterOutputStream(printService);
escpos = new EscPos(printerOutputStream);
escpos.writeLF(title,"My Market")
.feed(3)
.write("Client: ")
.writeLF(subtitle, "John Doe")
.feed(3)
.writeLF("Cup of coffee $1.00")
.writeLF("Botle of water $0.50")
.writeLF("----------------------------------------")
.feed(2)
.writeLF(bold,
"TOTAL $1.50")
.writeLF("----------------------------------------")
.feed(8)
.cut(EscPos.CutMode.FULL);
escpos.close();
// another ticket ... another time...
printerOutputStream = new PrinterOutputStream(printService);
escpos = new EscPos(printerOutputStream);
escpos.writeLF(title,"My Market")
.feed(3)
.write("Client: ")
.writeLF(subtitle, "John Doe")
.feed(3)
.writeLF("Cup of coffee $1.00")
.writeLF("Botle of water $0.50")
.writeLF("----------------------------------------")
.feed(2)
.writeLF(bold,
"TOTAL $1.50")
.writeLF("----------------------------------------")
.feed(8)
.cut(EscPos.CutMode.FULL);
escpos.close(); |
Hey, did you find any escpos emulator? is it open source? can you share any tip with the us? |
Yes is this one https://github.com/dacduong/escpos-printer-simulator is working like tcpip terminal, |
Its work fine I miss to re-create the instance. |
good, thank you |
nice, count on me if you have any other problematic situation. |
@anastaciocintra I just noticed on your receipt amount of the products its not double instead of 14.40 is 1440 and end price must be 14.40 x 3 not $143.00 witch on the emulator is not like this.Can you make a test again the problem must be on that line Edit 143.00 is right but why dot in Amount and Unit Price don't show can you check this thank you |
@anastaciocintra can you propose some solution for printing from list not with the toString() method. |
Hi @mikebgrep,
isn't it? |
@anastaciocintra yes do you have a solution of it.With toString() method the values of the double don't show properly on the receipt.At least I see in the test you make for me. |
Sure man, we have some, for example map with streams, but I think that your algorithm is pretty good and I made some little trick format adjusts , congrats, you have made a good printed receipt work! import com.github.anastaciocintra.escpos.EscPos;
import com.github.anastaciocintra.escpos.EscPosConst;
import com.github.anastaciocintra.escpos.Style;
import com.github.anastaciocintra.output.PrinterOutputStream;
import javax.print.PrintService;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
class Order {
public String tableNumber;
public ArrayList<OrderProduct> products;
public double totalPrice;
public Order(String tableNumber, ArrayList<OrderProduct> products){
this.tableNumber = tableNumber;
this.products = products;
totalPrice = 143.0;
}
}
class OrderProduct {
public String productCode;
public String productName;
public int productQty;
public double productPrice;
public OrderProduct(String productCode, String productName, int productQty, double productPrice) {
this.productCode = productCode;
this.productName = productName;
this.productQty = productQty;
this.productPrice = productPrice;
}
public double getPrice(int qty) {
return this.productPrice * qty;
}
@Override
public String toString() {
String productPriceStr = String.format("%.2f x %d", productPrice,productQty);
String qtyPriceStr = String.format("%.2f", getPrice(productQty));
return String.format("%-15s%12s %10s",productName,productPriceStr, qtyPriceStr );
}
}
public class MikebGrep {
void printRecipent(String printerName){
// public void loadData() {
ArrayList<OrderProduct> products = new ArrayList();
OrderProduct orderProd = new OrderProduct("4324", "demo product 1", 1,14.4);
OrderProduct orderProd1 = new OrderProduct("4324", "demo product 2", 2,30.4);
OrderProduct orderProd2= new OrderProduct("4324", "demo product 3", 8,74.8);
products.add(orderProd);
products.add(orderProd1);
products.add(orderProd2);
Order order = new Order("1", products);
// }
//this call is slow, try to use it only once and reuse the PrintService variable.
PrintService printService = PrinterOutputStream.getPrintServiceByName(printerName);
try {
PrinterOutputStream printerOutputStream = new PrinterOutputStream(printService);
EscPos escpos = new EscPos(printerOutputStream);
Style title = new Style()
.setFontSize(Style.FontSize._3, Style.FontSize._3)
.setJustification(EscPosConst.Justification.Center);
Style subtitle = new Style(escpos.getStyle())
.setBold(true)
.setUnderline(Style.Underline.OneDotThick);
Style bold = new Style(escpos.getStyle())
.setBold(true);
escpos.writeLF(title,"Mikebgrep\n")
.writeLF(title,"C o f f e e")
.feed(3)
.writeLF(subtitle.setJustification(EscPosConst.Justification.Center), "R E C E I P T")
.feed(2)
.writeLF("Item Unit Price Amount")
.writeLF("----------------------------------------")
.feed(2);
order.totalPrice =0;
for(OrderProduct product: order.products){
escpos.writeLF(product.toString());
order.totalPrice += product.getPrice(product.productQty);
}
String total = String.format("$ %.2f", order.totalPrice);
escpos.writeLF("----------------------------------------")
.feed(2)
.writeLF(bold,
"TOTAL " + String.format("%10s",total))
.writeLF("----------------------------------------")
.writeLF(" Thank you for shopping with us ")
.writeLF(" Have a nice day ")
.feed(8)
.cut(EscPos.CutMode.FULL);
// do not forget to close...
escpos.close();
} catch (IOException ex) {
Logger.getLogger(MikebGrep.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void main(String[] args) {
if(args.length!=1){
System.out.println("Usage: java -jar getstart.jar (\"printer name\")");
System.out.println("Printer list to use:");
String[] printServicesNames = PrinterOutputStream.getListPrintServicesNames();
for(String printServiceName: printServicesNames){
System.out.println(printServiceName);
}
System.exit(0);
}
MikebGrep obj = new MikebGrep();
obj.printRecipent(args[0]);
}
} |
@anastaciocintra Thank you man that is awesome.I actually try one time to do for each loop in the builder but I don't know why everything was red.But this is proper receipt.Thank you again. |
I like the library but I can't understand how to set a method with witch will connect to the terminal printer.If the printer is network based with Ip and port or the printer is connected to laptop/pc with a cable I am not very familiar with the connection types.I need this for a project witch the user can use what terminal printer he have.I need with one general setting the user can connect the printer.I am open to any suggestions.
If I use this example I need to change only the printer name value with the name of actual printer
if so what printers support can you give me live example for printer name
The text was updated successfully, but these errors were encountered: