Printing plain text
Plain text formats are great for working with on screens. They’re easy to generate and manipulate, and often presented with basic styling to highlight their structure.
For example, the output of the tree
command shows a directory tree, with
box drawing characters
indicating the hierarchy, directory names in bold, and different colors for
different file types.
Generating a printable equivalent of such things can be tricky. Let’s take a look at the options.
Problematic options
-
Text editors will sometimes let you print, but generally without much control over how the output is styled.
-
Screenshots let you capture what exactly what’s on screen, however, as relatively low-resolution raster data the text won’t be very sharp, and will be a real problem if you want to adjust the size. The on-screen style may not be suited to to a medium that isn’t backlit.
-
LaTeX involves many concepts beyond plain text and basic styling. Use it for native print or multi-format documents, not for a paper equivalent of the plain text screen experience.
-
Pandoc converts between many markup formats, but is focussed on structured document formats rather than free-form text. Document semantics determine default styling but anything more needs to be applied separately.
-
unoconv converts between office document formats, including from plain text to ODF or PDF. You can specify a template document, but you’d need to open LibreOffice and apply manual styles to highlight particular parts of your content.
-
cupsfilter converts plain text to a PDF, but offers no styling options.
-
a2ps will convert plain text to PostScript, but it’s styling options are only for very basic syntax highlighting of code, and it doesn’t support Unicode.
-
Enscript can convert plain text to PostScript. It has a lot of options, can do automatic syntax highlighting of many programming languages, and lets you add your own basic styling using special escape sequences. Unfortunately it doesn’t support Unicode.
-
paps version 0.6.8 is widely packaged and often recommended as a UTF-8 compatible alternative to Enscript and a2ps. It allows basic styling via command-line options and Pango markup. There are a couple of issues with version 0.6.8. It’s support of Pango markup is limited: bold works but colors don’t. Also, rather than putting actual text into the output document, it renders text using its own mechanism, which results in large file sizes and unselectable text.
A lightweight solution
Fortunately, in 2015 the paps author released a new version on Github: paps 0.7.0. It now uses Cairo for its rendering and generates compact text-based files. It offers good basic styling options via a more complete implementation of Pango markup.
If you’re generating your own data, it’s easy to apply basic styling with Pango markup.
If you want to print some data that’s already already styled for the terminal, you can use Ansifilter to convert terminal control codes to Pango markup (or one of several other markup formats). You can remap the 16-color palette to your own set of RGB values.
This command converts the output of tree
to a printable PDF with styling intact:
tree -C | ansifilter --pango | paps --markup --format=pdf > tree.pdf
For printing programming languages, there are
several ways
to apply syntax highlighting with terminal control codes, which you can then
feed into Ansifilter. I like the pygmentize
command from the
Pygments project.
Heavyweights: HTML and CSS
To go beyond basic styling, you need to commit to a heavier-weight styling language, but you don’t have to write raw PostScript. You can use HTML and CSS.
First, generate some HTML yourself, or take HTML output from another tool. If your data can represented in a plain-text markup language like Markdown, reStructuredText or AsciiDoc, a tool like Pandoc can convert it to HTML, to which you can apply your own stylesheet.
CSS gives you a lot of control over presentation, including some features made specifically for print design, although you will need to check which features are implemented by your renderer.
You can manually print from a web browser, but there are also several tools for converting an HTML/CSS document to a PDF non-interactively. WeasyPrint focusses on CSS layout for paged media. PhantomJS and wkhtmltopdf can both produce PDFs using versions of the WebKit rendering engine.