3

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 "$@"
 do 
/usr/local/bin/pandoc --from=markdown --to=html "$f" --css=/Users/fooo/Documents/Code/css/markdown.css -o "$f".html 
done

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
7
  • 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
    Commented Feb 3, 2012 at 18:47
  • which pandoc? which prince? (As in what is the full path to each file)
    – Daniel
    Commented Feb 3, 2012 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
    Commented Feb 3, 2012 at 20:02
  • path to pandoc/usr/local/bin/pandoc; path to prince: /usr/local/bin/prince
    – Maiasaura
    Commented Feb 3, 2012 at 20:38
  • Sorry, one more: where does your markdown.css file live?
    – Daniel
    Commented Feb 3, 2012 at 21:21

2 Answers 2

3

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

The payload is the shell script

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

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
  • 2
    make that prince - "${f%.*}".pdf to avoid files named foo.md.pdf. Also, you'll need a full path to markdown.css Commented Feb 3, 2012 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 Commented Nov 17, 2018 at 20:33
0

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

1
  • 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
    Commented Feb 3, 2012 at 18:53

You must log in to answer this question.

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