# 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.

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

• Thanks for your help. I've played with htmldoc and can see how to get it to do what I want, but you'll have to forgive me, because I'm not sure where to put that piece of code to actually have it run at a particular time. – CGP Grey Jan 27 '15 at 21:52
• Edit: Hazel to the rescue! – CGP Grey Jan 27 '15 at 21:57
• Great! I would have suggested a cronjob, but Hazel is much friendlier. – Drew Stephens Jan 27 '15 at 22:05
• There is still one problem: this loop just prints the first file over and over, not all the files once. for filename in ls *.md; do echo $(markdown$filename) "<p>$filename</p>" | htmldoc --no-toc --no-title --fontspacing 3 --size a4 -f${filename}.pdf -lpr ${filename}.pdf done – CGP Grey Jan 27 '15 at 22:18 • I think I just realized that this is my own dumb fault for having pipes in my file names. – CGP Grey Jan 27 '15 at 22:49 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.
• It would be more maintainable to just \input{my-template-file} from the command line. – Sean Allred Jan 31 '15 at 5:01
• Very interesting. I'll play around with the option. Thank you. – CGP Grey Feb 23 '15 at 8:28
• Running the test yields: "pandoc: xelatex not found. xelatex is needed for pdf output." – CGP Grey Nov 13 '15 at 5:15
• Just want to make sure: this is after installing the BasicTeX package as described above, right? Are you able to run "xelatex" or "pdflatex" normally from the terminal? – Tim Lin Apr 16 '16 at 23:37

I have not used WKPDF, but its author suggests a method by which it can be made to work in Yosemite: https://github.com/plessl/wkpdf/wiki/How-to-install-on-MacOS-10.10-Yosemite

• RVM: command not found. – CGP Grey Jan 27 '15 at 21:34

You could use PhantomJS. It's not as straight forward as WKPDF for PDF generation since it has a wider range of features, but works fine.