Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Makefile that uses Pandoc to generate HTML, PDF, DOCX, etc. from Markdown source files
# Makefile
#
# Converts Markdown to other formats (HTML, PDF, DOCX, RTF, ODT, EPUB) using Pandoc
# <http://johnmacfarlane.net/pandoc/>
#
# Run "make" (or "make all") to convert to all other formats
#
# Run "make clean" to delete converted files
# Convert all files in this directory that have a .md suffix
SOURCE_DOCS := $(wildcard *.md)
EXPORTED_DOCS=\
$(SOURCE_DOCS:.md=.html) \
$(SOURCE_DOCS:.md=.pdf) \
$(SOURCE_DOCS:.md=.docx) \
$(SOURCE_DOCS:.md=.rtf) \
$(SOURCE_DOCS:.md=.odt) \
$(SOURCE_DOCS:.md=.epub)
RM=/bin/rm
PANDOC=/usr/local/bin/pandoc
PANDOC_OPTIONS=--smart --standalone
PANDOC_HTML_OPTIONS=--to html5
PANDOC_PDF_OPTIONS=
PANDOC_DOCX_OPTIONS=
PANDOC_RTF_OPTIONS=
PANDOC_ODT_OPTIONS=
PANDOC_EPUB_OPTIONS=--to epub3
# Pattern-matching Rules
%.html : %.md
$(PANDOC) $(PANDOC_OPTIONS) $(PANDOC_HTML_OPTIONS) -o $@ $<
%.pdf : %.md
$(PANDOC) $(PANDOC_OPTIONS) $(PANDOC_PDF_OPTIONS) -o $@ $<
%.docx : %.md
$(PANDOC) $(PANDOC_OPTIONS) $(PANDOC_DOCX_OPTIONS) -o $@ $<
%.rtf : %.md
$(PANDOC) $(PANDOC_OPTIONS) $(PANDOC_RTF_OPTIONS) -o $@ $<
%.odt : %.md
$(PANDOC) $(PANDOC_OPTIONS) $(PANDOC_ODT_OPTIONS) -o $@ $<
%.epub : %.md
$(PANDOC) $(PANDOC_OPTIONS) $(PANDOC_EPUB_OPTIONS) -o $@ $<
# Targets and dependencies
.PHONY: all clean
all : $(EXPORTED_DOCS)
clean:
- $(RM) $(EXPORTED_DOCS)
@sstevens2

This comment has been minimized.

Copy link

sstevens2 commented Jan 23, 2017

Really like this makefile and would like to use it. What license is it under?

@elizabethmcd

This comment has been minimized.

Copy link

elizabethmcd commented Mar 7, 2017

I also really like this! Is this under an open source license?

@aWeinzierl

This comment has been minimized.

Copy link

aWeinzierl commented Apr 23, 2018

hmm?

@floriandierickx

This comment has been minimized.

Copy link

floriandierickx commented May 28, 2018

Thanks, i have been using this for some time now, works really nice! In the meantime I was wondering if somebody could help out to make the makefile convert only a specific '.md'-file in a folder by typing something like make name-of-the-article. Is this possible?

@llarsson

This comment has been minimized.

Copy link

llarsson commented Jan 11, 2019

I am sure that you, @floriandierickx, have already figured this out in the mean time, but for others with the same request:

make understands the %.pdf : %.md as a target itself ("generate whatever.pdf from whatever.md") , so if you have foo.md and want foo.pdf, all you'd have to type is make foo.pdf and make will understand what to do (check if the PDF is fresh enough, and if not, generate it).

It just won't show up in your tab completions in your shell, since whoever made the bash autocompletions didn't anticipate this need.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.