Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print bold and sized text in bash printing

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

like image 975
CuriousNewbie Avatar asked Jun 19 '20 06:06

CuriousNewbie


People also ask

What does %f mean bash?

-f - file is a regular file (not a directory or device file)

How do I make text bold in printf?

How do I make text bold in printf? printf("\nThis is a normal text."); printf("\n%sThis is a BOLD text.


1 Answers

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
like image 66
Ted Lyngmo Avatar answered Oct 26 '22 13:10

Ted Lyngmo