I’m trying to print with ESC/P commands used a label printer Brother P750W. I’m using P-touch Editor 5.4 the printer works well but, when I’m tried to make my own labels from Java code, the printer doesn’t work, it’s look like it’s connect to the printer, but do nothing. What do I can try now?
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import org.zyx.common.cli.Command;
import org.zyx.common.cli.CommandException;
public class CommandVialLabelPrintBrotherTest implements Command {
@Override
public void execute() throws CommandException {
try {
// Your ESC/P command to print a label with barcode
PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
PrintService brotherPrinter = null;
for (PrintService printer : printServices) {
System.out.println("Printer: " + printer.getName());
}
// Find Brother PT-P750W
for (PrintService printer : printServices) {
System.out.println("Printer: " + printer.getName());
if (printer.getName().equalsIgnoreCase("Brother PT-P750W (Copy 1)")
|| printer.getName().equalsIgnoreCase("Brother PT-P750W")) {
brotherPrinter = printer;
}
}
if (brotherPrinter == null) {
System.out.println("Brother PT-P750W not found!");
return;
}
// Open a stream to send raw data to the printer
DocPrintJob job = brotherPrinter.createPrintJob();
String escCommands = "u001B@" + // ESC @ - Initialize printer
"u001Bi r 12" + // ESC i r 12 - Set label width to 12mm (0.47")
"u001Bi L" + // ESC i L - Start label layout
"u001Bi S 24" + // ESC i S 24 - Set text size to 24 points
"Hello Printer" + // The text to print
"u001Bi d 1" + // ESC i d 1 - Print the label
"u001Bi Z"; // ESC i Z - Cut the label (optional)
DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
Doc doc = new SimpleDoc(escCommands.getBytes(), flavor, null);
// Print the document
job.print(doc, null);
System.out.println("Raw ESC/P data sent to printer!");
} catch (PrintException e) {
e.printStackTrace();
}
}
}
2