Instantly share code, notes, and snippets.

Embed
What would you like to do?
Markdown source for the "Create PDF files from Markdown sources in OSX" article

Create PDF files from Markdown sources in OSX

When Markdown appeared more than 10 years ago, it aimed to make it easier to express ideas in an easy-to-write plain text format. It offers a simple syntax that takes the writer focus away from the formatting, thus giving her time to focus on the actual content.

The market abunds of editors to be used for help with markdown. After a few attempts, I settled to Sublime and its browser preview plugin, which work great for me and have a small memory footprint to accomplish that. To pass the results around to other people, less technical, a markdown file and a bunch of images is not the best approach, so converting it to a more robust format like PDF seems like a much better choice.

Pandoc is the swiss-army knife of converting documents between various formats. While being able to deal with heavy-weight formats like docx and epub, we will need it for the more lightweight markdown. To be able to generate PDF files, we need LaTeX. On OSX, the solution of choice is usually MacTeX.

Setup Pandoc for document conversion

Choose the easy brew way:

$ brew update
$ brew install pandoc

If the above solution is not working, the alternative is to take the cabal route:

$ brew install ghc cabal-install
$ cabal update
$ cabal install pandoc

For those interested, the install page covers each aspect in more depth.

Setup Mactex for PDF generation

$ brew tap phinze/cask
$ brew install brew-cask
$ brew cask install mactex

Generating PDF files

We will use the current article as the example we want to export as PDF. The markdown source is available as a gist.

When trying the output command from the pandoc documentation, we notice there is a problem, as it can't find pdflatex.

$ pandoc -o out.pdf osx-pdf-from-markdown.markdown
pandoc: pdflatex not found. pdflatex is needed for pdf output.

Let's check it's been properly installed and symlink it correctly.

$ ls -lsa /usr/texbin/pdflatex
8 lrwxr-xr-x  1 g  wheel  6 Apr 23 14:09 /usr/texbin/pdflatex -> pdftex

$ sudo ln -s /usr/texbin/pdflatex /usr/local/bin/

Re-running the output command works correctly and gives us a shiny new PDF file.

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

This comment has been minimized.

Show comment
Hide comment
@brianjking

brianjking Jan 8, 2016

Thanks @georgiana-gligor!

  • Any idea if this will work with the BasicTex release?
  • Secondly, any idea how to get PDFs to generate with images that are included in the markdown file?

brianjking commented Jan 8, 2016

Thanks @georgiana-gligor!

  • Any idea if this will work with the BasicTex release?
  • Secondly, any idea how to get PDFs to generate with images that are included in the markdown file?
@jpsecher

This comment has been minimized.

Show comment
Hide comment
@jpsecher

jpsecher Sep 24, 2016

I had to do

ln -s /usr/local/texlive/2016/bin/x86_64-darwin/pdflatex /usr/local/bin/

jpsecher commented Sep 24, 2016

I had to do

ln -s /usr/local/texlive/2016/bin/x86_64-darwin/pdflatex /usr/local/bin/
@tleyden

This comment has been minimized.

Show comment
Hide comment
@tleyden

tleyden Nov 5, 2016

Ditto on ln -s /usr/local/texlive/2016/bin/x86_64-darwin/pdflatex /usr/local/bin/

tleyden commented Nov 5, 2016

Ditto on ln -s /usr/local/texlive/2016/bin/x86_64-darwin/pdflatex /usr/local/bin/

@driade

This comment has been minimized.

Show comment
Hide comment
@driade

driade May 13, 2017

Thanks, it helped

ln -s /usr/local/texlive/2016/bin/x86_64-darwin/pdflatex /usr/local/bin/

driade commented May 13, 2017

Thanks, it helped

ln -s /usr/local/texlive/2016/bin/x86_64-darwin/pdflatex /usr/local/bin/

@coffeejoshua

This comment has been minimized.

Show comment
Hide comment
@coffeejoshua

coffeejoshua Sep 27, 2017

Thanks!

For some reason mine was not in the path and moved to /Library/TeX/Root/bin/x86_64-darwin/pdflatex.

ln -s /Library/TeX/Root/bin/x86_64-darwin/pdflatex /usr/local/bin/

coffeejoshua commented Sep 27, 2017

Thanks!

For some reason mine was not in the path and moved to /Library/TeX/Root/bin/x86_64-darwin/pdflatex.

ln -s /Library/TeX/Root/bin/x86_64-darwin/pdflatex /usr/local/bin/

@illern

This comment has been minimized.

Show comment
Hide comment
@illern

illern Oct 9, 2017

Nice, thanks. The only thing was the path for pdflatex.
ln -s /usr/local/texlive/2017/bin/x86_64-darwin/pdflatex /usr/local/bin/pdflatex

illern commented Oct 9, 2017

Nice, thanks. The only thing was the path for pdflatex.
ln -s /usr/local/texlive/2017/bin/x86_64-darwin/pdflatex /usr/local/bin/pdflatex

@dhoffi

This comment has been minimized.

Show comment
Hide comment
@dhoffi

dhoffi Oct 23, 2017

maybe also worth a try:

#!/bin/bash

if (( $# != 2 ))
then
  echo "Dependencies: brew install markdown htmldoc"
  echo "Usage: md2pdf <input.md> <output.pdf>"
  exit 1
fi
markdown $1 | htmldoc --cont --headfootsize 8.0 --linkcolor blue --linkstyle plain --format pdf14 - > $2

dhoffi commented Oct 23, 2017

maybe also worth a try:

#!/bin/bash

if (( $# != 2 ))
then
  echo "Dependencies: brew install markdown htmldoc"
  echo "Usage: md2pdf <input.md> <output.pdf>"
  exit 1
fi
markdown $1 | htmldoc --cont --headfootsize 8.0 --linkcolor blue --linkstyle plain --format pdf14 - > $2
@arctouch-brunopinheiro

This comment has been minimized.

Show comment
Hide comment
@arctouch-brunopinheiro

arctouch-brunopinheiro commented Mar 16, 2018

It does work with BasicTex

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment