JAVA - Print 2 tickets generated in thermal printer and that cut them

1

I would like some help, it is the first time I have used a thermal printer and I have no idea how to do it, what I want it to do is print a txt file or a String that is generated with the data that make up a ticket (quantity, price, etc), I want you to print it twice and cut each ticket to be able to deliver one to the client and I will stay with another one

    
asked by JOSE ANGEL SICAIROS ELIZALDE 06.09.2018 в 04:44
source

2 answers

2

In most POS printers when operating in text mode there is a sequence of characters by which paper is cut

These characters usually vary by brand and model but are usually in the technical manual of the printer.

Simply sending the correct combination of characters is enough for example

w = new FileWriter(fichero);
bw = new BufferedWriter(fw);
StringBuilder builder = new StringBuilder();

// Message
builder.append("MOTEL OASIS\r\n");
builder.append("AV ESPINOZA 267, COL OBRERA\r\n");
builder.append("ENSENADA, B.C.\r\n");
builder.append("\r\n");
builder.append("DIA EXTRA\r\n);
builder.append("Fecha : ").append(fecha).append("       Hora: ").append(hora).append("\r\n");
builder.append(cantidad).append(" ").append(producto).append("            $").append(total).append("\r\n");
builder.append("\r\n");
builder.append("GRACIAS POR SU PREFERENCIA");
builder.append("\r\n");

// Paper Cut
char[] cutPaper = new char[] { 0x1d, 'i'};
builder.append(cutPaper);

bw.write(builder.toString());
bw.close();

In this case I send the escape character 27 (I have set it so that it is easy to see) and then the character "i"

This example corresponds to the Bematec LR2000 printers link

For an EPSON T20 or similar (All Epson TM) for example you should change the cutting commands by:

byte[] cutPaper = new byte[] { 0x1d, 'V', 1 };

In short, you should look at the manuals of compatible printers.

Normally there is a set of commands for the different types of printers that allow these operations to be carried out.

The ideal would be to have a class that implements the printer model to use it with the necessary commands.

Also indicate that these commands can allow you to give more life to the text and other functions. There are fast-forward commands (eg move the paper 10 or 100 lines before cutting), center the text, align to the right, ...

Once you have the implementing class you can always send the text block with the cut order twice to have two copies.

Or if you stop the methods call a function print (text) and then cut () twice.

    
answered by 06.09.2018 / 09:55
source
0

This is the method by which I generate my ticket:

public void Dextra (String Time, String Date, String Quantity, String Total, String Product) {

    try
    {
        String carpeta = "C:\MotelOasis\Tickets\";
        String archivo = "C:\MotelOasis\Tickets\#LPT1.txt";
        File folder = new File(carpeta);
        folder.mkdirs();
        File fichero = new File(archivo);
        BufferedWriter bw;
        FileWriter fw;
        if(!fichero.exists())
        {   
            fichero.createNewFile();
            fw = new FileWriter(fichero);
            bw = new BufferedWriter(fw);
            bw.write("MOTEL OASIS"+"\r\n"
            +"AV ESPINOZA 267, COL OBRERA\r\n"
            +"ENSENADA, B.C.\r\n"
            +"\r\n"
            +"DIA EXTRA\r\n"
            +"Fecha : "+Fecha+"       Hora: "+Hora+"\r\n"
            +"\r\n"
            +Cantidad+" "+Producto+"            $"+Total+"\r\n"
            +"\r\n"
            +"GRACIAS POR SU PREFERENCIA");
            bw.close();
        }
        else if(fichero.exists())
        {
            int R=0;
            String archivo2 = "C:\MotelOasis\Tickets\#LPT1.txt";
            String D2=null;
            FileReader f2 = null;
            try {
                f2 = new FileReader(archivo2);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(Estado.class.getName()).log(Level.SEVERE, null, ex);
            }
            BufferedReader b2 = new BufferedReader(f2);
            try {
                D2=b2.readLine();
            } catch (IOException ex) {
                Logger.getLogger(Estado.class.getName()).log(Level.SEVERE, null, ex);
            }
            fw = new FileWriter(fichero);
            bw = new BufferedWriter(fw);
            bw.write("MOTEL OASIS"+"\r\n"
            +"AV ESPINOZA 267, COL OBRERA\r\n"
            +"ENSENADA, B.C.\r\n"
            +"\r\n"
            +"DIA EXTRA\r\n"
            +"Fecha : "+Fecha+"       Hora: "+Hora+"\r\n"
            +"\r\n"
            +Cantidad+" "+Producto+"            $"+Total+"\r\n"
            +"\r\n"
            +"GRACIAS POR SU PREFERENCIA");
            bw.close();
        }
    }
    catch (IOException ex)
    {
        Logger.getLogger(Reportes.class.getName()).log(Level.SEVERE, null, ex);
    }
}
    
answered by 06.09.2018 в 07:39