How to print from java code on a Brother-ql720nw printer

0

I need help printing on a brother-ql720 label printer from java code using escp mode

I have installed the drivers that come with the printer and also the p-touch software

Since the p-touch software recognizes the paper size and prints without any problem

I have tried the following code without success link

import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.HashAttributeSet;
import javax.print.attribute.standard.PrinterName;

public class PrintESC_P {   
public static void main(String[] args) {
    PrintService printService = null;
    String printerName = "Brother QL-720NW";
    HashAttributeSet attributeSet = new HashAttributeSet();
    attributeSet.add(new PrinterName(printerName, null));
    PrintService[] services = PrintServiceLookup.lookupPrintServices(null, attributeSet);
    if (services.length == 0) {
        throw new IllegalArgumentException("Printer not found.");
    } else if (services.length > 1) {
        System.out.println("Found more than one printer. Only the first printer will be used.");
    }
    printService = services[0];
    System.out.println("Printer found: "+printService.getName());
    try {
        DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;          
        String _ESC_P_Code = "ESC i a 00h\r\n"  +
                "ESC @\r\n" +
                "ESC i L 01h\r\n" +
                "ESC ( C 02h 00h FCh 02h\r\n" +
                "ESC $ 2Bh 00h\r\n" +
                "ESC ( V 02h 00h 6Dh 01h\r\n" +
                "ESC k 0bh\r\n" +
                "ESC X 00h 64h 00h\r\n" +
                "PRINTER TEST\r\n" +
                "ESC i C\r\n" +
                "FF\r\n";
        SimpleDoc doc = new SimpleDoc(_ESC_P_Code.getBytes(), flavor, null);
        DocPrintJob job = printService.createPrintJob();
        job.print(doc, null);
    } catch (Exception e) {
    e.printStackTrace();
    }
}
}

I just need an example of text printing

I enclose the p-touch output image which is what I want to do from the java code

    
asked by Blas Pico 14.07.2016 в 17:43
source

1 answer

0

With the class JTextComponent you should be able to print even formatted text.

JTextPane jtp = new JTextPane();
jtp.setBackground(Color.white);
jtp.setText("text to print");
boolean show = true;
try {
    jtp.print(null, null, show, null, null, show);
} catch (java.awt.print.PrinterException ex) {
    ex.printStackTrace();
}

I hope it serves, greetings.

    
answered by 14.07.2016 / 20:49
source