How to Cancel a Printing Job using PJL and Java after throwing it over netwok?

261 views Asked by At

I have a problem with printing jobs cancellation where I can print success after executing a print method using java like this

    public static void print(byte[] bytes, String jobName, String printerIpAdd, Integer printerPort, String trayNumber) {

        System.out.println("port " + printerPort);
        try (Socket socket = new Socket(printerIpAdd, printerPort)) {
            DataOutputStream out = new DataOutputStream(socket.getOutputStream());
            System.out.println("bytes[] = " + bytes.length);
            out.write(27); //esc
            out.write("%-12345X@PJL\n".getBytes());
            out.write(("@PJL SET JOBNAME=" + jobName + "\n").getBytes());
            out.write(("@PJL SET LPAPERSOURCE=TRAY" + trayNumber + "\n").getBytes());
            out.writeBytes("@PJL SET PALETTESOURCE = DEVICE\n");
            out.write(("@PJL SET PAPER=A5\n").getBytes());
            out.write(("@PJL SET COPIES=1"\n").getBytes());
            out.write("@PJL ENTER LANGUAGE=PDF\n".getBytes());
            out.write(bytes); //data
            out.write(27); //esc
            out.write("%-12345X@PJL\n".getBytes());
            out.flush();
            out.close();
        } catch (IOException ex) {
            LOGGER.error(ex);
        }
    }

the problem appears when is no paper in the printer or open the cover or tray is removed the printing job has become pending so I need to cancel the previous order for the printer. so I'm trying using this code but not working with me and create another job for printing instead of cancellation

public static void cancelPrintJob(String jobName, String printerIpAdd, Integer printerPort) {
        System.out.println("port " + printerPort);
        try (Socket socket = new Socket(printerIpAdd, printerPort)) {
            DataOutputStream out = new DataOutputStream(socket.getOutputStream());

            /*
            <ESC>%-12345X@PJL <CR><LF>
            @PJL DEFAULT JOBID =ON <CR><LF>
            @PJL USTATUS JOB = ON <CR><LF>
            @PJL JOB <CR><LF>
                <ESC>%-12345X@PJL <CR><LF>
                @PJL ENTER LANGUAGE = PCL <CR><LF>
                <ESC>E...PCLJob.!!!JobCancel !
            @PJL EOJ <CR><LF>
            <ESC>%-12345X

             */
            out.write(27); //esc
            out.write("%-12345X@PJL\r\n".getBytes());
            out.write(("DEFAULT JOBID =ON\r\n").getBytes());
            out.write(("@PJL USTATUS JOB = ON\r\n").getBytes());
            out.write(("@PJL JOB\r\n").getBytes());
            out.write(27); //esc
            out.write("%-12345X@PJL\\rn".getBytes());
            out.write("@PJL ENTER LANGUAGE = PCL\r\n".getBytes());
            out.write(27); //esc
            out.write(("@PJL EOJ NAME =" + jobName + "\r\n").getBytes());
            out.write(27); //esc
            out.write("%-12345X@PJL\n".getBytes());
            out.flush();
            out.close();
        } catch (IOException ex) {
            LOGGER.error(ex);
        }
    }
0

There are 0 answers