Skip to content

Instantly share code, notes, and snippets.

@georgiana-gligor
Last active December 11, 2024 03:48
Show Gist options
  • Save georgiana-gligor/fd247d02f8a44ce745db to your computer and use it in GitHub Desktop.
Save georgiana-gligor/fd247d02f8a44ce745db to your computer and use it in GitHub Desktop.
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
@alpaltunel
Copy link

For those who found this document on 2024 please use this:

sudo ln -s /usr/local/texlive/2023/bin/universal-darwin/pdflatex /usr/local/bin

then pandoc -o out.pdf readme.md will work

@georgiana-gligor
Copy link
Author

thanks @alpaltunel for the update! so happy to see my old scribble still useful

@anthonydahanne
Copy link

For those who found this document on late 2024 please use this:

sudo ln -s /usr/local/texlive/2024/bin/universal-darwin/pdflatex /usr/local/bin

then pandoc -o out.pdf readme.md will work

@magicliang
Copy link

I believe some of the example is out-of-date, for example:

brew tap phinze/cask
Error: phinze/cask was moved. Tap homebrew/cask instead.

there are big changes in cask/macOS ecosystem, maybe we should review old instructions.

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