Print Hello World using KPL in java

0

I try to send a Hello World to a Zebra thermal printer model KR203 , these printers use the language KPL (Kiosk Printer Language), when printing a test page it goes out correctly but with my code it stays in: "Printing ...".

This is my code:

public void Imprimir() {
    PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
    PrintRequestAttributeSet attributeSet = new HashPrintRequestAttributeSet();
    System.out.println("Default" + printService);
    String zplCommand = "^XA\n"
            + "^FO10,0^ARN,11,7^FD SOME TEXT ^FS\n"
            + "^FO300,0^ARN,11,7^FD SOME VALUE ^FS\n"
            + "^FO10,35^ARN,11,7^FD SOME TEXT ^FS\n"
            + "^FO300,35^ARN,11,7^FD SOME VALUE ^FS\n"
            + "^FO10,70^ARN,11,7^FD SOME CODE ^FS\n"
            + "^FO10,115^ARN,11,7^BCN,60,Y,Y,N^FD 23749237439827 ^FS\n"
            + "^XZ";
    byte[] by = zplCommand.getBytes();
    DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
    Doc doc = new SimpleDoc(by, flavor, null);
    DocPrintJob job = printService.createPrintJob();
    try {
        job.print(doc, null);  
        //job.print(doc, attributeSet);
    } catch (PrintException e) {
        System.out.println("e " + e);
    }
}

Note: It's a desktop application

    
asked by Rastalovely 04.01.2018 в 18:50
source

1 answer

0

To solve my problem I thought it was necessary to use the kpl commands to print later by trial and error I could print using these lines:

public void PrintTicketZ() {        
    PrintRequestAttributeSet attributeSet = new HashPrintRequestAttributeSet();
    attributeSet.add(OrientationRequested.PORTRAIT);
    attributeSet.add(new Copies(1));
    attributeSet.add(new JobName("PrintService", null));
    attributeSet.add(new MediaPrintableArea(10, 0, 60, 400, MediaPrintableArea.MM));

    PrinterJob pj = PrinterJob.getPrinterJob();
    pj.setPrintable(new Printable() {
        public int print(Graphics g, PageFormat pf, int pageIndex) {
            if (pageIndex == 0) {
                int h = 100;
                int w = 100;
                String company="--";                      
                try {
                    double x = pf.getImageableX();
                    double y = pf.getImageableY();
                    Paper paper = pf.getPaper();
                    pf.getPaper().setImageableArea(0, 0, paper.getWidth(), paper.getHeight());
                    Graphics2D g2d = (Graphics2D) g;
                    g2d.translate(x, y);
                    g2d.setColor(Color.black);
                    g2d.setFont(new Font("TimesRoman", Font.PLAIN, 10));
                    g2d.drawString("FOLIO: ", 105, 40);
                    g2d.drawString("FECHA: ", 0, 60);
                    g2d.drawString("HORA: ", 0, 75);
                    g2d.drawString("N° EMPLEADO: " , 0, 100);
                    g2d.drawString("EMPRESA: " , 0, 115);
                    g2d.drawString("EMPLEADO: ", 0, 130);   
                } catch (WriterException e) {
                    System.out.println("Error " + e);
                }
                return Printable.PAGE_EXISTS;
            } else {
                return Printable.NO_SUCH_PAGE;
            }
        }
    });

    PrintService defaultPrintService = PrintServiceLookup.lookupDefaultPrintService();
    if (defaultPrintService != null) {
        try{
            String print_default = defaultPrintService.getName();
            pj.setPrintService(defaultPrintService);
            pj.print(attributeSet);
            log.info("Printing...");

            PrintServiceAttributeSet printServiceAttributeSet = defaultPrintService.getAttributes();
            String trabajo = printServiceAttributeSet.get(QueuedJobCount.class).toString();
            int trabajos = Integer.parseInt(trabajo);

        } catch (PrinterException pe) {
            log.error("Error al imprimir: " + pe);
            System.out.println(pe);
        }
    }
}
    
answered by 18.01.2018 в 19:36