Markdown to PDF in MacOSX

I tend to use markdown almost unconsciously when taking notes or expressing thoughts or ideas in a text editor. But on several occasions I’ve had the need to share what I’ve written with a non technical person and handing off markdown to them seems a bit so-so. So after a few of these incidences I settled the matter and decided to investigate if there was an easy way to just generate a pdf file from markdown from the command-line in MacOSX. A quick tour on google and you’ll find this excellent gist that describes more or less how to get it working. The problem was that everything didn’t quite work for me (I later found out that a fix was written in the comment but I didn’t know at the time). These are the two things that I had to do differently:

  1. When it says that you should do brew tap phinze/cask I simply ignored this step and went directly to brew cask install mactex
  2. When it’s time to create a symlink the gist instructs us to do sudo ln -s /usr/texbin/pdflatex /usr/local/bin/ but instead I had to do ln -s /Library/TeX/Root/bin/x86_64-darwin/pdflatex /usr/local/bin/pdflatex

Here are the instructions in its entirety:

$ brew update
$ brew install pandoc
$ brew install ghc cabal-install
$ cabal install pandoc
$ brew cask install mactex
$ ln -s /Library/TeX/Root/bin/x86_64-darwin/pdflatex /usr/local/bin/pdflatex

Now you should be able to run pandoc from the command-line and generate pdfs like this:

$ pandoc -o out.pdf osx-pdf-from-markdown.markdown

Great! But I’m not using pandoc regularly so in order to make it a little easier to remember I wrote a small bash script that I’ve named md2pdf:

#!/bin/bash
if [ -z "${1}" ]; then 
	echo "You need to specify the markdown file"
	exit 0	
fi

# If second argument is undefined then name the output file the same as the input file 
# expect that use a "pdf" file extension
if [ -z "${2}" ]; then 
	# Get the filename (and path) without the extension
	filename_without_ext=`rev <<< "${1}" | cut -d"." -f2- | rev`
    pdf_filename="${filename_without_ext}.pdf"
else 
	pdf_filename="${2}"
fi

pandoc -o "${pdf_filename}" "${1}"

After some chmod +x md2pdf and having copied it to /usr/local/bin this allows me to simply write:

$ md2pdf some.md

and it'll generate a file called some.pdf. It's also possible to specify another directory or filename if you like:

$ md2pdf some.md ~/pdfs/important.pdf

That's it!

81 thoughts on “Markdown to PDF in MacOSX

  1. You don’t need lines 3 and 4; those relate to the Haskell/Cabal method of installing pandoc and are redundant since brew pandoc works fine.

    Thanks for the instructions!

  2. Thanky ou for this article, very useful script! But I think it suffices to install pandoc via brew. Also, an important option to add to the pandoc command in case you write anything possibly containing special characters: `–pdf-engine=xelatex`

  3. I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article.

  4. I’m amazed, I must say. Seldom do I encounter a blog that’s both educative and amusing, and without a doubt,
    you have hit the nail on the head. The problem is an issue that
    too few people are speaking intelligently about. Now i’m very happy that I came across this during my hunt for something relating to this.

  5. Hello this is somewhat of off topic but I was wanting
    to know if blogs use WYSIWYG editors or if you
    have to manually code with HTML. I’m starting a blog soon but have no
    coding expertise so I wanted to get advice from someone with experience.
    Any help would be greatly appreciated!

  6. If you are going for finest contents like me, only pay a quick visit this website everyday since it provides quality
    contents, thanks

  7. Attractive section of content. I just stumbled upon your weblog and in accession capital to assert that I get in fact enjoyed
    account your blog posts. Any way I’ll be subscribing to your
    feeds and even I achievement you access consistently fast.

  8. Hello! I know this is kind of off topic but I was wondering if you knew where I could locate a captcha plugin for my comment form?

    I’m using the same blog platform as yours and I’m
    having trouble finding one? Thanks a lot!

  9. Undeniably believe that which you said. Your favorite reason appeared to be at the net the simplest factor to take
    into account of. I say to you, I definitely get annoyed at the
    same time as folks think about worries that they just do not realize about.
    You controlled to hit the nail upon the top and outlined out the entire thing without having side effect , people could take a signal.
    Will likely be back to get more. Thank you

  10. My brother suggested I might like this blog.
    He was entirely right. This publish actually made my day.
    You can not consider just how so much time I had spent for this information! Thanks!

  11. I do not know if it’s just me or if perhaps everybody else experiencing
    problems with your site. It appears as if some of the written text
    on your content are running off the screen. Can someone else please comment and let me know if this
    is happening to them too? This may be a problem with my web
    browser because I’ve had this happen previously.

    Many thanks

  12. I think that is one of the such a lot important info for
    me. And i’m glad studying your article. But should remark on few
    common things, The web site taste is perfect, the articles is in point of fact
    nice : D. Excellent process, cheers

  13. I loved as much as you will receive carried out
    right here. The sketch is attractive, your authored subject
    matter stylish. nonetheless, you command get bought an impatience over that you wish be delivering the following.
    unwell unquestionably come more formerly again as exactly the same
    nearly a lot often inside case you shield this increase.

Leave a Reply to Clitoral Suction G-Spot Vibrating Egg Cancel reply

Your email address will not be published. Required fields are marked *