Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

Thanks.

share|improve this question
    
The conversion from ANSI-escaped text to pdf (currently performed by aha+wkhtmltopdf) seems like something 'enscript' or 'paps' should natively be able to do; try submitting a 'wishlist' bug with one of them. –  Daniel C. Feb 22 at 1:24

1 Answer 1

up vote 1 down vote accepted

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.

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.

    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.
    

    Install:

    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.
    

    Install:

    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'

enter image description here

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.

share|improve this answer
    
It makes me feel very old that someone wouldn't know about those straight away. :) –  Alan B Mar 28 '13 at 14:30
1  
I think it has more to do with your general geekiness than your age :) –  terdon Mar 28 '13 at 14:38
    
@terdon I'm sure that it's some sort of escape code. I made a quick bash script to see what sort of characters it can print and its an odd set of 256 characters that aren't bash. Used \x and hex (ex. \x9e ) and it is very different from Lubuntu's bash characters for those same values. –  Blue Ice Mar 29 '13 at 13:46
    
For example, \x1 is a smiley face, and \xc is a page break. Don't see that much in bash :) –  Blue Ice Mar 29 '13 at 13:49
    
@BlueIce sorry but I have absolutely no idea what you are talking about :). But hang on, I've found out how to do this, I will be posting an updated answer soon. –  terdon Mar 29 '13 at 13:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.