Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This question is inspired by this one on SU. How can I print bold or color using lp and ANSI escape sequences? I know how to display, for example, bold text in the terminal:

$ echo -e '\033[01;1mthis text will be bold\033[00;0m,this will not'
this text will be bold,this will not

However, when I pipe this directly to lp I get a file that looks like this:

01;1mthis text will be bold00;0m,this will not

So, I figure the way to do this would be to use groff to create a postscript file and print that. It looks like groff should be able to do this, I know it can correctly convert a man page to a ps file and keep whatever is bold in the man page bold in the postscript. However, the groff documentation is enormous and kind of esoteric to someone with no postscript experience. I have tried various combinations of options, all of which result in a postscript file that looks like the line above:

echo  -e '\033[01;1mthis text will be bold\033[00;0m,this will not' | groff >a.ps
echo  -e '\033[01;1mthis text will be bold\033[00;0m,this will not' | groff -c >a.ps
echo  -e '\033[01;1mthis text will be bold\033[00;0m,this will not' | groff -Pc >a.ps
echo  -e '\033\[01;1m\]this text will be bold\033\[00;0m\],this will not' | groff -Tascii >a.ps 
echo  -e '\033[01;1mthis text will be bold'| groff -man >a.ps 
echo  -e '\033[01;1mthis text will be bold'| groff -mdoc >a.ps 
echo  -e '\033[01;1mthis text will be bold'| groff -me >a.ps
echo  -e '\033[01;1mthis text will be bold'| groff -mm >a.ps
echo  -e '\033[01;1mthis text will be bold'| groff -ms >a.ps

So, how can I use lp and groff or any other tool combination to print bold or colored text from the terminal?

share|improve this question

3 Answers 3

up vote 3 down vote accepted

That's more the reverse of what groff is designed to do.

What you're looking for can be achieved at least with this combination of tools:

Like:

printf '\e[31;1mfoo\e[mbar\n' |
  aha |
  wkhtmltopdf - - |
  pdf2ps - output.ps

A bit overkill but it does the trick. You can probably skip the last part as PDF is as easily printed as postscript nowadays:

printf '\e[31;1mfoo\e[mbar\n' |
  aha |
  wkhtmltopdf - output.pdf

Or you can feed it directly to lp for printing.

share|improve this answer

PostScript is a programming language for placing marks on paper. To make said marks colored, you have to include the correct instructions in the program. They take a very different form from the crude ANSI escape codes VT100 clones understand.

I don't remember much of troff (and when I wrote my thesis with it, color printers weren't even a dream...), but in LaTeX it is possible to write colored text that is translated into PDF or PS to print as such. But cobbling together something that handles escape secuences and spits out LaTeX won't be much fun, better write the colored/boldened/whatevered text directly in troff or LaTeX.

share|improve this answer
    
Aren't the bold fonts in man pages generated using ANSI escape sequences? If so, groff can read them and convert to bold postscript. –  terdon Mar 28 '13 at 18:00
1  
@terdon, the man pages are written in troff, using the an macro package (the package name is a pun, you call troff -man ... to use them). The man pages for showing on screen are generated by nroff -man .... –  vonbrand Mar 28 '13 at 19:21

Assuming your printer subsystem can handle coloured postscript files, you can use groff and friends at the terminal to mark up text and generate a postscript file with colours and bold and italic fonts for your printer:

$ cat i
.warn
.color
This is ordinary.
.ft B
This is bold.
.ft I
This is italic.
.ft R
This is ordinary.
.gcolor red
This is red.
.gcolor green
This is green.
.gcolor blue
This is blue.

$ groff i >out.ps
$ lpr out.ps         # printer subsystem must be able to handle postscript

You can easily format exactly the same file as ASCII with ANSI colour escape sequences and display it on your terminal:

$ groff -Tascii i    # output appears, in colour, on your screen

You can also use other -T output devices with exactly the same input file:

$ groff -Thtml i
<!-- Creator     : groff version 1.21 -->
<!-- CreationDate: Wed Oct 16 21:05:36 2013 -->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta name="generator" content="groff -Thtml, see www.gnu.org">
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<meta name="Content-Style" content="text/css">
<style type="text/css">
  p       { margin-top: 0; margin-bottom: 0; vertical-align: top }
  pre     { margin-top: 0; margin-bottom: 0; vertical-align: top }
  table   { margin-top: 0; margin-bottom: 0; vertical-align: top }
  h1      { text-align: center }
</style>
<title></title>
</head>
<body>
<hr>
<p>This is not bold. <b>This is bold.</b> <i>This is
italic.</i> This is not bold. <font color="#FF0000">This is
red.</font> <font color="#00FF00">This is green.</font>
<font color="#0000FF">This is blue.</font></p>
<hr>
</body>
</html>

Using groff as your mark-up language, you can print bold or coloured text from the terminal.

share|improve this answer

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.