Auto Generating and Printing PDF Files From Markdown?

I'm looking for a way to automatically have my computer generate and print PDF files from a folder full of markdown files.

So, every morning at 5AM I'd want my computer look at my /markdown-files folder, create a bunch of PDFs to go into a /PDF-folder and also run the printer so the pages are waiting for me.

There are a couple of requirements that might make this a bit tricky:

  1. The PDFs should have the file title at the top of each page
  2. The page needs to be A4
  3. The lines should be double spaced at least.

I used to use WKPDF to generate PDFs, but it no longer works on Yosemite.


Solution 1:

You can do this with Markdown.pl and htmldoc(1), both of which can be installed with Homebrew. The basic flow for making a PDF is:

markdown foo.markdown | htmldoc --no-toc --no-title -f foo.pdf -

As for your filename-per-page, you coul incorporate echo to add it as an HTML element:

echo $(markdown foo.markdown) "<p>foo</p>" | htmldoc --no-toc --no-title -f foo.pdf -

And for printing, the lpr(1) command will send a file to your default printer.

Tying this altogether:

for filename in `ls *.markdown`;
do
    echo $(markdown "$filename") "<p>$filename</p>" | 
        htmldoc --no-toc --no-title -f "${filename}.pdf" -
    lpr "${filename}.pdf"
done

Solution 2:

Here's a solution that uses LaTeX to generate the PDF. If you have long-form text in the Markdown files and would like to optimize for typographic readability (and I suspect you do), LaTeX tends to do a much better job compared to HTML engines.

It uses pandoc as an intermediary between Markdown and LaTeX, and also produces the PDF for you.

Main script

#!/bin/bash

md-dir="MARKDOWN_DIRECTORY_HERE"
pdf-dir="PDF_DIRECTORY_HERE"

cd "${md-dir}"
for filename in *.md; do
    target-pdf="${pdf-dir}/${filename}.pdf"
    pandoc "${filename}" -t latex --latex-engine=xelatex -V geometry=a4paper -V fontsize=11pt -V listings -V header-includes="\usepackage{fancyhdr}\pagestyle{fancyplain}\cfoot{}\rhead{\thepage}\lhead{\texttt{\lstinline/${filename}/}}" -V header-includes="\linespread{2.0}" -o "${target-pdf}"
    echo "produced ${filename}.pdf"
    lpr "${target-pdf}"
done

Required setup

  1. Get pandoc from Homebrew: brew install pandoc
  2. Install the minimal version of LaTeX called BasicTeX

Testing if you got the setup correct

You should be able to run in a new Terminal window (with bash)

echo "test" | pandoc -t latex --latex-engine=xelatex -o test.pdf

and get a valid test.pdf document with just the word "test"

Customizations

There's a lot you can tweak with the LaTeX settings from the command line

  1. tweak the line spacing by changing the \linespread{2.0} multiplier
  2. adjust markings by replacing geometry=a4paper with geometry=a4paper,left=XXmm,right=XXmm,top=XXmm,bottom=XXmm
  3. change font size by chaining 11pt to either 10pt or 12pt, and everything should scale accordingly
  4. change the main font by adding another argument to pandoc: -V mainfont="NAME OF FONT". You can also set monofont this way.