How to print the output of tree to pdf without losing the color?
I use the following command to print a directory structure to a file:
tree -h somepath/ > tree_of_somepath.txt
tree
gives a nice colorized output on the terminal, but as expected this cannot be redirected to a text file. I would like to print the output of tree
to a pdf file and preserve the color.
Any ideas?
-
Install the following dependencies:
sudo apt-get install aha wkhtmltopdf
-
Save your
tree
command output to html withaha
:tree -C -h | aha > foo.html
From the
tree
man page,-C
forces colorization:-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.
-
Finally export the html to pdf with
wkhtmltopdf
:wkhtmltopdf foo.html foo.pdf
Example:
cd /tmp
tree -C -h | aha > foo.html
wkhtmltopdf foo.html foo.pdf
xdg-open foo.pdf