Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to send a .text file containing ESC/POS commands to a printer from java code

Tags:

java

printing

What i have so far is the following code:

 FileInputStream fin =new FileInputStream(filename);
 DocFlavor df = DocFlavor.INPUT_STREAM.AUTOSENSE;                    
 Doc d = new SimpleDoc(fin, df, null);   
 PrintService P = PrintServiceLookup.lookupDefaultPrintService();

  if (P != null) {              
     DocPrintJob job = P.createPrintJob();  
     job.print(d, null);  
            }
    fin.close;

the code is working fine but the printer does't interpret the commands if the file containing commands, it keep printing the exact string content of the file. so how to send command to Epson receipt printer ?

like image 691
ALMEK Avatar asked Feb 18 '23 06:02

ALMEK


2 Answers

As have been figured out that commands may have to be send directly not in ESC/POS format but you need to interpret the code to hexadecimal in you java code and send to printer as the way i post, whether from a file or string. as example instead of initializing the Epson receipt printer by:

  PRINT #1, CHR$(&H1B);"@";

and to cut the paper in receipt printer the code may be:

  PRINT #1, CHR$(&H1D);"V";CHR$(1);

so this is how it works for me.

    char[] initEP = new char[]{0x1b, '@'};
    char[] cutP = new char[]{0x1d,'V',1};
    String Ptxt=  new String(initEP)+ " text data \n \n \n"+ new String(cutP);

instead of

    Doc d = new SimpleDoc(new FileInputStream(filename), df, null);  

use

    InputStream pis = new ByteArrayInputStream(Ptxt.getBytes());
    Doc d = new SimpleDoc(pis, df, null);

however it maybe a way to send code as its command format but desperately could't do that so far. and not sure if it can done from java.

like image 74
ALMEK Avatar answered Apr 07 '23 13:04

ALMEK


The last step is inserting the printer commands using their true ASCII values in your input file--e.g., the escape character is ASCII value 0x1B. This can be done either by using an editor that allows inserting any ASCII value, such as a hex editor or Notepad++'s Character Panel (under the Edit menu), or by programmatically modifying the data sent to the printer after it is read from the file.

like image 45
David Duncan Avatar answered Apr 07 '23 13:04

David Duncan