Markdown to PDF

Todays note is nothing more than a quick tip. I’m a big fan of the Markdown markup language, but sometimes I need to exchange documents with people who don’t know how to read them. Often, they prefer just Word or PDF. So I looked for a way to convert a Markdown file to a PDF file.

My current solution is based on Node.js so you need to install that first. Then use npm, the package manager for javascript, to install the markdown-pdf package using

npm i -g markdown-pdf

As a little convenience for myself, I added a shell alias to my ~/.zsrc (but you can of course add it to ~/.bashrc or whatever shell you’re using):

alias md2pdf="markdown-pdf -f A4 -m '{\"breaks\":false}'"

It does two things:

  1. It make the command shorter and easier to remember. Very important to me :).
  2. It sets some default options that I tend to forget. -f A4 sets the format of the generated PDF to A4 -m '{\"breaks\":false}' tells the underlying Markdown engine not to treat a line break as a new paragraph. I usually have every sentence on a new line. Without this -m flag, it would generate a new paragraph for each of them, which is not what I wanted.