I'm encountering difficulties when attempting to print simple text using PCL in Java with my Deskjet 2752e printer. The printer supports HP PCL 3 GUI, HP PCLm (HP Apps/UPD), and URF (AirPrint)1. Despite multiple attempts and variations of the PCL code, I have been unable to achieve successful printing. Even sending plain text results in no response from the printer.
My objective is to send a print job directly to my printer network through 9100 port raw data, with the ability to position the text using x and y coordinates, if possible. I am developing an Android app that can print text without prompting the user for print settings or leaving the app. It is crucial for the app to automatically and efficiently send the necessary data to the printer and initiate the printing process promptly. As the sole user of the app, I aim to streamline the printing experience and eliminate unnecessary prompts, thereby making the app faster and more efficient. So Android printing framework is not an option
Here is the last code I have been working with:
private void sendPrintJob() {
System.out.println("Connecting to the printer...");
try (Socket socket = new Socket("192.168.0.6", 9100)) {
System.out.println("Connected.");
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
out.write(generatePCL3GUI().getBytes());
out.flush();
out.close();
System.out.println("Print job successfully sent.");
} catch (Exception e) {
System.out.println(e);
}
}
public String generatePCL3GUI() {
StringBuilder pclCode = new StringBuilder();
pclCode.append("\u001B%-12345X@PJL JOB\r\n");
pclCode.append("@PJL ENTER LANGUAGE=PCL3GUI\r\n");
pclCode.append("\u001B*s0M"); // Set color mode to monochrome
pclCode.append("\u001B*t300R"); // Set resolution to 300 dpi
pclCode.append("\u001B*r0F"); // Set orientation to portrait
pclCode.append("\u001B(8U"); // Select font: Courier New
pclCode.append("\u001BE"); // Start of the print job
pclCode.append("\u001B&a100H"); // Set position (x=100, y=100)
pclCode.append("Hello, world!\r\n"); // Print text
pclCode.append("\u001B%-12345X@PJL EOJ\r\n");
return pclCode.toString();
}