Ask Different is a question and answer site for power users of Apple hardware and software. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I run the following two commands on markdown files to get a nicely formatted PDF.

pandoc -o file.html --css=markdown.css 
prince file.html file.pdf
rm file.html

How would I automate this? I'd like to right-click a markdown file, navigate to services, and choose convert markdown to PDF.

I know I should use Automator and I tried but couldn't link the right steps together. In my example above, file is the filename that needs to be the argument. I got as far as starting a new Automator service, choosing Files & Folders, and Get Selected Finder Items and got stuck there.

Update: Pandoc =

Prince =

Update #2 for clarity

If I am in a folder with a markdown file named, then the complete command line syntax would be:

pandoc --from=markdown --to=html --css=~path/to/css/markdown.css -o test.html

(Note: I have been able to automate this as a service and have named it Markdown to HTML)

Here is that code:

 for f in "$@"
/usr/local/bin/pandoc --from=markdown --to=html "$f" --css=/Users/fooo/Documents/Code/css/markdown.css -o "$f".html 

Now to convert that bit of html to PDF is something I am not able to link in. But the command line syntax for that would be:

prince test.html -o test.pdf
share|improve this question
I have built the "RTF" equivalent service and, assuming nobody beats me to it, I'll post it when I get "back to my Mac". Meanwhile, let me toss an unsolicited plug for Byword into the mix. I haven't touched that service in months. – jaberg Feb 3 '12 at 18:47
which pandoc? which prince? (As in what is the full path to each file) – Daniel Feb 3 '12 at 19:42
This "prince" command -- is there an option for it to read from stdin? If so, this can be made quite easy through the magic of piping. If not, yes, this gets a little trickier. – Daniel Feb 3 '12 at 20:02
path to pandoc/usr/local/bin/pandoc; path to prince: /usr/local/bin/prince – Maiasaura Feb 3 '12 at 20:38
Sorry, one more: where does your markdown.css file live? – Daniel Feb 3 '12 at 21:21

What about an Automator service like this one: enter image description here

The payload is the shell script

 for f in "$@"
    pandoc "$f" --css=markdown.css | prince - "$f".pdf

Whatever the selected file or files, the service runs them through pandoc's Markdown parser, then takes the resulting html and processes it with prince, naming the output the original file name with a .pdf extension added.

The piping removes the need for you to manage temporary files.

share|improve this answer
make that prince - "${f%.*}".pdf to avoid files named Also, you'll need a full path to markdown.css – Nathan Grigg Feb 3 '12 at 21:19

Go to Automator and create a new service:
Create new service
Then add a Shell Script:
Shell script - automator
Detailed information here

share|improve this answer
I saw that but it doesn't answer my question. How does it know where to insert the input? In my case, pandoc $file_name -o $file_name.html --css=markdown.css (The script has to place the file_name as is in the first location, change extension to html in the second, but leave the --css-markdown.css argument alone). – Maiasaura Feb 3 '12 at 18:53

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.