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

pandoc file.md -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 = http://johnmacfarlane.net/pandoc/

Prince = http://www.princexml.com/

Update #2 for clarity

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

pandoc --from=markdown --to=html test.md --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
  • 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.

  • 2
    make that prince - "${f%.*}".pdf to avoid files named foo.md.pdf. Also, you'll need a full path to markdown.css Feb 3 '12 at 21:19
  • Perfect! Just what I've been looking for. I've just added below the command that works for me using pandoc 2.4 and which removes the .md from the pdf file name: for f in "$@" do pandoc -f markdown_mmd --wrap=preserve -V geometry:a4paper -o "${f/.md/}".pdf "$f" done Nov 17 '18 at 20:33

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

  • 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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .