7

1st question

printing "BOLD" text...using lp if possible.

example:

echo -e "\e[1mBOLD\e[0m" | lp

will print out this below on a white paper

1mBOLD0m

how to get:

BOLD

?

2nd question

Printing [font size=33]SIZED TEXT[/font], how?

the only way i know for now is using a2ps

example:

a2ps -B --portrait --columns=1 --rows=1 --borders=no --font-size=18 --margin=0 text.txt | lp

command above will print out big sized text, however there still a big gap of margin can be seen with naked eye there. it just too big to be missed. I want to print it just like lp printed it on the edge of my paper and with bigger font on top of that.

SUMMARY OF THE ANSWER

based on the answer I got here, and tested. It proves that printing a nice formatted and sized font is not that simple as printing an echo output. Many things should be considered here such as the Margins, the font sizes, the font formatting, and specifically the AddOns which backing up the lp. It is true that most of the system by default came with lp but not with aha also wkhtmltopdf, it might be because people can do it easily in Abiword or any word processing software. But for me, I need it for my regular basis on bash script. It is kinda complicated to set up, but once it set up, it is definitely faster than any word processor. The only un-avoidable problem here is the margin, as in my case by default it came with certain number of margins with comparisons left:top - 1:1.6cm, it gets wider when the font get smaller, and it gets thinner when font get bigger. That such problem can be easily eliminated in word processor, but again, I prefer speed and acceptable problems. From the answer, we know that aha will got updated related to font sizes, I don't know what that feature will be like, but I am looking forward for it.

Thank you very much for everyone who participated in this post

codeforester
  • 39,467
  • 16
  • 112
  • 140
CuriousNewbie
  • 319
  • 4
  • 13
  • 3
    You have to encode this in the way **your printer** understands it. – user1934428 Jun 19 '20 at 06:41
  • 1
    Does this answer your question? [How does one output bold text in Bash?](https://stackoverflow.com/questions/2924697/how-does-one-output-bold-text-in-bash) – Digvijay S Jun 19 '20 at 07:12
  • by using [https://stackoverflow.com/questions/2924697/how-does-one-output-bold-text-in-bash] now it prints `1mBOLDBm` the only different is `0` and `B`... – CuriousNewbie Jun 19 '20 at 07:31
  • 2
    I would be surprised if ANSI escape sequences are understood by printers. You might want to look into the PostScript format. – l0b0 Jun 19 '20 at 07:46
  • `a2ps` will work, but I dont like it abit. it prints with a large gap of margin, with no header setting. I want a clean print just like `lp` does. – CuriousNewbie Jun 19 '20 at 07:56
  • Maybe you could use an alternative to `a2ps`. Seems like `enscript` could at least solve the bold part using `^@font{fontname}`. Have a look at the `Special Escapes` section in `man enscript`. – Socowi Jun 23 '20 at 12:03
  • `\e[1m` is an [ANSI escape](https://en.wikipedia.org/wiki/ANSI_escape_code), that configures how your terminal formats text. [`man lp`](https://manpages.debian.org/lp.1) makes no mention of ANSI escapes and presumably doesn't support them. – dimo414 Jun 24 '20 at 05:13
  • 1
    I believe you will find this answer extremely useful: [SU: Enabling Bold and Color with LPT parallel printer](https://superuser.com/questions/573688/enabling-bold-and-color-with-lpt-parallel-printer) – kvantour Jun 25 '20 at 06:57
  • @kvantour I knew that already thats why I deliberatelly making this post and explosure the capability of lp command without any other "addon" help – CuriousNewbie Jun 25 '20 at 14:58

1 Answers1

12

For starters, don't use the raw ANSI escape sequences directly since it makes it hard to read and maintain. Use tput to produce the escape sequences for you.

There's a tool called aha that can convert ANSI escape sequences into HTML. You can probably install it using sudo apt-get install aha or sudo dnf install aha. If it's not available on your platform, you can download and compile it using the link.

You can then convert HTML to PDF using a tool called wkhtmltopdf. Install (or download and compile) that too if you don't have it already.

With these tools, you can use your ANSI escape sequences to produce a PDF and your printer is likely going to print that just fine. The sequence becomes:

(tput <commands>; ...) | aha | wkhtmltopdf - - | lp

The - - arguments to wkhtmltopdf makes it read from stdin and write to stdout

Put into an example:

#!/bin/bash

# force tput to use the ansi terminal capabilities
export TERM=ansi

# Margin settings
top=18
bottom=18
left=1.6
right=1.6
pagesize=A4

fontsize=30px

# convenience functions

function bold {
    tput bold
    echo -n "$@"
    # bold can't be turned off by itself so this
    # turns off all attributes
    tput sgr0
}

function ul {
    tput smul
    echo -n "$@"
    tput rmul
}

function rev {
    # standout mode really, but reverse mode for ansi
    tput smso
    echo -n "$@"
    tput rmso
}

# start a subshell to be able to pipe the output to aha
(
    echo "Lets try to make $(bold this) bold."
    echo "and $(ul this) is underlined."
    echo "Here we use $(bold $(ul both)) decorators."
    echo "This will be in $(rev reverse)."

    # using tput to produce a color map
    for fg_color in {0..7}; do
        set_foreground=$(tput setaf $fg_color)
        for bg_color in {0..7}; do
            set_background=$(tput setab $bg_color)
            echo -n $set_background$set_foreground
            printf ' F:%s B:%s ' $fg_color $bg_color
        done
        echo $(tput sgr0)
    done
) | aha | \
sed -E "s,^<pre>$,<div style=\"font-size:${fontsize}\"><pre>," | \
sed -E 's,^</pre>$,</pre></div>,' | \
wkhtmltopdf --page-size $pagesize -T $top -B $bottom -L $left -R $right - - | lp

This should produce something like this on paper:

enter image description here

Edit: I made a pull request in aha to add a --style option that has now been approved and is included in aha version 0.5.1. As of this version you can remove the sed lines and tell aha which font-size you want directly.

Example:

   ...
) | aha --style 'font-size:1.875em' | \
wkhtmltopdf --page-size $pagesize -T $top -B $bottom -L $left -R $right - - | lp
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108