question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to make genral setting to find printer terminal with one command

See original GitHub issue

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

PrintService printService = PrinterOutputStream.getPrintServiceByName("printerName");
 PrinterOutputStream printerOutputStream = new PrinterOutputStream(printService);
 EscPos escpos = new EscPos(printerOutputStream);
 escpos.writeLF("Hello Wold");
 escpos.feed(5);
 escpos.cut(EscPos

if so what printers support can you give me live example for printer name

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:22 (10 by maintainers)

github_iconTop GitHub Comments

3reactions
mikebgrepcommented, Nov 30, 2021

@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. ps: I was missing a ; to can make the for loop

0reactions
anastaciocintracommented, Nov 29, 2021

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!

receipt

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


    }
}

Read more comments on GitHub >

github_iconTop Results From Across the Web

Command-Line Printer Administration - CUPS.org
The lpadmin command is used to create, modify, or delete a printer. The -p option specifies a printer to create or modify: lpadmin...
Read more >
8. How to set things up - The Linux Documentation Project
To add a printer from the command-line the general syntax is lpadmin -p printer -E -v device -m ppd Lpadmin with the -p...
Read more >
5.7. Command-Line Tools for the CUPS Printing System
To print a file, enter the System V style print command lp -d queuename file or a Berkeley style command like lpr -Pqueuename...
Read more >
How to List Printers on an Ubuntu Command Line
1. Launch the Ubuntu Terminal application. · 2. Type "sudo cat /etc/cups/printers.conf" at the command prompt then press the "Enter" key. · 3....
Read more >
Using lpadmin to Install Printers in macOS
If you want the script to go and get the respective printer driver ... for installing printers uses the lpadmin command in a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found