PDF Automation – Generating and Printing Files from Markdown

automationmarkdownpdfprinting

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.

Best Answer

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