javajava-printpjl

Using Java to send PJL commands to HP 4515 Printer


I am trying to send Printer Job Language commands to a HP 4515 printer. However, the printer does not print anything. Below is my code. The printer is located remotely and I can only ask someone there to check if anything is printed out. Unfortunately nothing was printed out. Are the PJL commands not in good format? How can I get the job status using Java & PJL?

  socket = new Socket("192.168.1.101", 9100);
        out = new DataOutputStream(socket.getOutputStream());
        DataInputStream input = new DataInputStream(socket.getInputStream());


        final char ESC = 0x1b;
        final String UNESCAPED_UEL  = "%-12345X";
        String UEL = ESC + UNESCAPED_UEL;
        out.writeBytes(UEL); 
        out.writeBytes("@PJL\r\n");

        //out.writeBytes("@PJL SET MEDIASOURCE = TRAY2\r\n"); //I tried this line of code as well
        out.writeBytes("@PJL SET PAPER = LETTER\r\n");

        out.writeBytes("@PJL ENTER LANGUAGE = PDF\r\n");
        for(int i=0; i<copies; i++) {
            out.write(ps, 0, ps.length); //ps is of type byte[]. It contains the content of PostScript file
        }
        out.flush();

The printer's paper settings:

TRAY 1 SIZE 
TRAY 1 TYPE 
TRAY 2 SIZE LETTER
UNIT OF MEASURE 
X DIMENSION INCHES (5.83 - 8.5)
Y DIMENSION INCHES (8.27 - 14.0)
TRAY 2 TYPE 

Solution

  • As noted here, it looks like you are missing the closing 'Universal Exit Language' command (UEL). It is required in PJL. It defines beginning and end of any PJL-based data stream.

    E.g.:

    socket = new Socket("192.168.1.101", 9100);
    out = new DataOutputStream(socket.getOutputStream());
    DataInputStream input = new DataInputStream(socket.getInputStream());
    
    
    final char ESC = 0x1b;
    final String UNESCAPED_UEL  = "%-12345X";
    String UEL = ESC + UNESCAPED_UEL;
    out.writeBytes(UEL); 
    out.writeBytes("@PJL\r\n");
    out.writeBytes("@PJL SET PAPER = LETTER\r\n");
    out.writeBytes("@PJL ENTER LANGUAGE = PDF\r\n");
    for(int i=0; i<copies; i++) {
        out.write(ps, 0, ps.length);
    }
    out.writeBytes(UEL); // <-- add this
    out.flush();
    

    I can't tell if there is something off with your PJL command syntax, but for reference this is working for me.