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?
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.Sign up to join this community
The way to echo bold or colored text in a terminal is to use ANSI escape codes. For example, try this:
$ echo -e '\033[01;1mthis text will be bold\033[00;0m this will not'
this text will be bold this will not
The escape sequence
\033[01;1m causes the following text to be bold and
\033[00;0m turns all attributes off, so it goes back to normal.
See here for a nice list of the various escape sequences.
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.
DESCRIPTION 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.
Description 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 '\033[01;1mbold\033[00;0m,not bold, and \033[32;1mgreen'
So, let's print it:
echo -e '\033[01;1mbold\033[00;0m,not bold, and \033[32;1mgreen' | aha | wkhtmltopdf - - | lpr
And that's it, that will print a nice, formatted bold and colored line of text.