2

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();
}

enter image description here

5
  • Your print job data contains raw bytes, don't write it into a String or StringBuilder. Raw data should always be saved to a byte array or in your case an ByterArrayOutputStream. How do you know which protocol your printer supports on port 9100 (TCP or UDP)?
    – Robert
    Commented Jun 8, 2023 at 5:44
  • @Robert the web interface says 9100 is used for tcp raw data. I'll screenshot the section where it says it when i get home and get back to you. Thanks for replying
    – Jose Lopez
    Commented Jun 8, 2023 at 6:03
  • @Robert i added a SS of the web interfaces where indicates that 9100 is used for raw printing
    – Jose Lopez
    Commented Jun 8, 2023 at 12:02
  • I would try to first test on a PC what happens if you send certain files (generated using print to file and different printer drivers) to that port before trying the writing the code by hand.
    – Robert
    Commented Jun 8, 2023 at 12:28
  • @Robert Do you recomment any GUI programs to do that? Like select a printer driver, ip, port and file and send them? Or do you mean in terminal? I have a Mac BTW
    – Jose Lopez
    Commented Jun 8, 2023 at 18:32

1 Answer 1

1

Unfortunately there is no solution to your problem. The reason is very simple: PCL3 does not support the printing of plain text, nor does it support any of the PCL5 commands you are sending to it with your code. The printer will simply ignore anything except the kind of data it expects. Hence all your effort "results in no response from the printer".

PCL3 has nothing in common with PCL5 or PCL6. Whereas PCL5/6 sends commands to the printer, which are converted into pixels by the printer's processor, PCL3 is a GDI-style protocol, which requires the host computer to convert the page into printer pixels. It is that raster data, in the correct format, that the printer expects from the host.

PCLm is supposed to be a subset of the PDF format, but again it supports raster data only. So again your software has to convert the data into dots on the paper.

I do not know why HP use the PCL moniker for this. I can only assume that they want to ride on the success of PCL5/6 to sell cheaper printers.

The only realistic way to use this (or any other GDI printer) is by installing the manufacturer-supplied driver driver.

If you are unable to use the correct driver, your only option is to invest in a printer that supports PCL5. They do cost a bit more than GDI printers. However, for not much more than your current printer, you could get a mono laser with PCL5 support. That would also avoid all the head-clog hassles with inkjets and, if you skip the HP brand, you get away from HP's consumables prison.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .