How to send rendered Markdown files to the printer from the command line

big surcommand lineprintingterminal

I wrote a Markdown file which I wish to print. The rendered version, not the raw version.

Printing in GUI works fine. I wish to format Mardown files with mdcat and send the result/output to printer.

lpc status
HP_ENVY_5540_series:
    printer is on device ‘dnssd’ speed -1
    queuing is enabled
    printing is enabled
    no entries
    daemon present

mdcat example.md | lp
request id is HP_ENVY_5540_series-47 (0 file(s))

mdcat example.md | lp -d HP_ENVY_5540_series
request id is HP_ENVY_5540_series-48 (0 file(s))

Those commands didn't work.

Edit, after first comment:

brew info mdcat
mdcat: stable 0.22.2 (bottled)
Show markdown documents on text terminals
https://github.com/lunaryorn/mdcat
/usr/local/Cellar/mdcat/0.22.2 (8 files, 5.4MB) *
  Poured from bottle on 2021-01-02 at 08:42:16
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/mdcat.rb
License: MPL-2.0
==> Dependencies
Build: cmake ✔, rust ✘
==> Analytics
install: 336 (30 days), 1,518 (90 days), 10,040 (365 days)
install-on-request: 335 (30 days), 1,517 (90 days), 10,027 (365 days)
build-error: 0 (30 days)

Does the software need some kind of support? Maybe I don't remember correctly, but you can pipe any command to lp in Linux. There is not any kind of support needed for printers in the command before pipe.

Best Answer

TL;DR You need a markdown renderer which creates Postscript or PDF as output. How to convert from a Markdown file, to PDF may be a bit dated but lists some options on how to accomplish this.


Any output device (be it a Terminal, be it a Printer) uses control codes within the data to display in order to render it properly, usually these control codes are different for each device. Sometimes (e.g. for Postscript or PDF) they are even a whole language on its own.

Now lp relies on CUPS to talk to the printer, CUPS usually only understands how to handle plain text, PostScript, PDF, and image files directly. So you can do cat foo.md | lp/lp foo.md or cat foo.pdf | lp/lp foo.pdf to print the plain markdown file or any PDF. If you send text formatted for terminal output (as mdcat does) it will not lead to the expected result (neither does running man lp | lp usually).