Enabling Bold and Color with LPT parallel printer

Posted on

Problem :

I know how to echo text to my HP Deskjet 420 printer on lubuntu with bash:

echo -e "line of text" > /dev/lp0

Is there a way to print text that is bold or in color? Should I echo some sort of escape command to the printer?


Solution :

The way to echo bold or colored text in a terminal is to use ANSI escape codes. For example, try this:

$ echo -e ‘33[01;1mthis text will be bold33[00;0m this will not’
this text will be bold this will not

The escape sequence 33[01;1m causes the following text to be bold and 33[00;0m turns all attributes off, so it goes back to normal.

See here for a nice list of the various escape sequences.

Unfortunately, lp does not know how to deal with this. It only knows how to print postscript files. We therefore need a way of turning the ANSI escape characters into formatted postscript. I was intrigued by this question so I posted one of my own on U&L asking about ways of interpreting the ANSI escapes as postscript commands. The following is from the answer I got there, please go upvote it!

You will need the following tools:

  • aha : Ansi HTML Adapter, this program can translate ANSI escape codes to HTML.

           aha takes SGR-colored Input and prints W3C conform HTML-Code.
           aha  reads the Input from a file or stdin and writes HTML-Code
           to stdout.


    sudo apt-get install aha
  • wkhtmltopdf : An HTML to PDF converter.

           Converts one or more HTML pages into a PDF document, not using
           wkhtmltopdf patched qt.


    sudo apt-get install wkhtmltopdf

Now, put them all together. I will use this command whose output is shown in the image below:

echo -e '33[01;1mbold33[00;0m,not bold, and 33[32;1mgreen'

enter image description here

So, let’s print it:

echo -e '33[01;1mbold33[00;0m,not bold, and 33[32;1mgreen' | 
      aha | wkhtmltopdf  - - | lpr

And that’s it, that will print a nice, formatted bold and colored line of text.

Leave a Reply

Your email address will not be published. Required fields are marked *