如何在不丢失颜色的情况下将树的输出打印为pdf?

Bru*_*uni 18 command-line pdf tree ansi

我使用以下命令将目录结构打印到文件:

tree -h somepath/ > tree_of_somepath.txt
Run Code Online (Sandbox Code Playgroud)

tree在终端上提供了一个漂亮的彩色输出,但正如预期的那样,这无法重定向到文本文件。我想将输出打印tree到 pdf 文件并保留颜色。

有任何想法吗?

Syl*_*eau 23

  1. 安装以下依赖项:

    sudo apt-get install aha wkhtmltopdf
    
    Run Code Online (Sandbox Code Playgroud)
  2. 使用以下tree命令将命令输出保存到 html aha

    tree -C -h | aha > foo.html
    
    Run Code Online (Sandbox Code Playgroud)

    tree手册页,-C强制着色:

        -C     Turn colorization on always, using built-in color defaults
               if the LS_COLORS environment variable is not set. Useful to
               colorize output to a pipe.
    
    Run Code Online (Sandbox Code Playgroud)
  3. 最后将 html 导出为 pdf wkhtmltopdf

    wkhtmltopdf foo.html foo.pdf
    
    Run Code Online (Sandbox Code Playgroud)

例子:

cd /tmp
tree -C -h | aha > foo.html
wkhtmltopdf foo.html foo.pdf
xdg-open foo.pdf
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明