【问题标题】:How to print bold and sized text in Bash?如何在 bash 打印中打印粗体和大小的文本
【发布时间】:2020-10-09 08:46:24
【问题描述】:

第一个问题

打印“BOLD”文本...尽可能使用lp

示例:

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

将在下面的白纸上打印出来

1mBOLD0m

如何获得:

粗体

?

第二个问题

打印[font size=33]SIZED TEXT[/font],怎么样?

我现在知道的唯一方法是使用a2ps

示例:

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

上面的命令会打印出大尺寸的文字,但是肉眼仍然可以看到很大的空白。它太大了,不容错过。我想打印它,就像lp 打印在我的纸张边缘并在其上使用更大的字体。

答案摘要

基于我在这里得到的答案,并进行了测试。它证明printing a nice formatted and sized font 并不像打印echo 输出那么简单。这里应该考虑很多事情,例如边距、字体大小、字体格式,特别是支持lp 的插件。的确,大多数系统默认都带有lp,但没有aha,也没有wkhtmltopdf,这可能是因为人们可以在Abiword或任何文字处理软件中轻松完成。但对我来说,我在 bash 脚本中经常需要它。设置起来有点复杂,但是一旦设置好,它肯定比任何文字处理器都快。这里唯一无法避免的问题是边距,因为在我的情况下,默认情况下它带有一定数量的边距,比较left:top - 1:1.6cm,当字体变小时它会变宽,当字体变大时它会变薄。在文字处理器中可以轻松消除此类问题,但同样,我更喜欢速度和可接受的问题。从答案中,我们知道aha 将更新与font sizes 相关的功能,我不知道该功能会是什么样,但我很期待。

非常感谢所有参与这篇文章的人

【问题讨论】:

标签: bash printing


【解决方案1】:

对于初学者,不要直接使用原始的 ANSI 转义序列,因为它难以阅读和维护。使用tput 为您生成转义序列。

有一个名为aha 的工具可以将ANSI 转义序列转换为HTML。您可以使用sudo apt-get install ahasudo dnf install aha 安装它。如果在您的平台上不可用,您可以使用链接下载并编译它。

然后,您可以使用名为 wkhtmltopdf 的工具将 HTML 转换为 PDF。如果您还没有,也安装(或下载并编译)它。

使用这些工具,您可以使用 ANSI 转义序列生成 PDF,并且您的打印机可能会很好地打印出来。序列变为:

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

wkhtmltopdf- - 参数使其从 stdin 读取并写入 stdout

举个例子:

#!/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

这应该在纸上产生类似这样的东西:

编辑: 我在aha 中创建了一个pull request,以添加一个--style 选项,该选项现已获得批准并包含在aha 0.5.1 版中。从这个版本开始,您可以删除 sed 行并直接告诉 aha 您想要哪种字体大小。

例子:

   ...
) | aha --style 'font-size:1.875em' | \
wkhtmltopdf --page-size $pagesize -T $top -B $bottom -L $left -R $right - - | lp

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-16
    • 2019-09-25
    • 2021-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多