guinix international

Computing Without Borders


guinix TechNote: Printer Control with PJL

Configure your LaserJet with shell scripts


Sometimes you want--or need--to change the configuration of an HP printer from the command-line:

  • Want: easier than getting up and twiddling with a control panel.

  • Need: the printer doesn't even have a control panel.

It was the second case that we faced. Our HP5MP was set up for "letter" sized paper, while the standard paper-stock in Africa and most of the world is A4. Problem was, the HP5MP provides nothing in the way of buttons or switches on the device itself to make the change.

For performing such configurations, HP expects "consumers" to use a GUI tool running on one of those proprietary operating systems, I forget the name. We don't use those systems ourselves, and just knew there had to be another way to accomplish what we wanted from our preferred BSD and Linux systems.

Turns out the answer is PJL: "Printer Job Language". This is a protocol and command set designed by HP for use in configuring their printer's features.


The best way to see how to use PJL from the command line is with the simple script that solved our problem:

# setup laserjet for a4 paper:

printf '\033%-12345X@PJL\r\n'
printf '@PJL DEFAULT PAPER=A4\r\n'
printf '@PJL RESET\r\n'
printf '\033%-12345X'

### that's all, folks!

This script has four printf lines:

  1. The first line issues the escape sequence %-12345X@PJL that puts the printer into PJL mode.

  2. The second line sends the PJL command which sets the default paper size to A4.

  3. The third line sends the PJL command which resets the printer to its default values (and now the default value for paper size is A4).

  4. The last command exits PJL mode; there should be no terminating on this line. (HP also refers to this command as UEL, for "Universal Exit Language".)

Note: we use printf here in order to embed the control code (octal 033) in the output. Some versions of the echo command --such as built in to bash-- support an -e option to accomplish the same thing. Use whatever you prefer.

Now it is simply a matter of executing this script, redirecting output to the printer. For example, on a BSD system with the printer connected to the first parallel port:

  # /bin/sh > /dev/lpt0

One of the mistakes we made --so you don't have to-- was using the SET command, instead of DEFAULT. That is:

  printf '@PJL SET PAPER=A4\r\n'

But this only changes the setting for the current job; the configuration of the printer itself remains unchanged. Use the equivalent DEFAULT commands if you want the settings to persist.

The range of configuration commands available with PJL is extensive. HP publishes a number of PJL resources and manuals on their website. We are using a PDF titled "Printer Job Language Technical Reference Manual" that describes a zillion PJL commands and the printers that support them. A quick Google will get you all the links you need.

If your printer is connected to a network interface, use the netcat utility (nc) to redirect the PJL commands to the printer device. Shell and other programmers can develop more generic scripts and/or menu-driven systems to accomplish a variety of common tasks.

Copyright © 2002 - 2005, Wayne Marshall. All rights reserved.
Last edit 2005.03.07, wcm.