How toUse Pandoc to convert a Markdown to a PDF and ePUB
If you ever need to convert a Markdown file to a PDF and ePUB, Pandoc is a great CLI tool to help you do it.
Installation
First you will have to install it, on Mac you can do it with Brew
brew install pandoc
To generate a PDF, you will also need to install mactext
, also with Brew, which has the engines used by Pandoc to generate PDFs.
brew install mactex
Generate PDF
To generate a PDF from a Markdown file, you can use the following command:
pandoc file.md -f markdown -t pdf -s -o file.pdf \
--metadata title="Title of the Document" \
--metadata author="Author" \
--metadata language="en" \
--metadata rights="Any copyright message" \
--metadata date="`date +'%Y-%m-%d'`" \
--toc
This will take the file.md
and generate a file.pdf
with the metadata and a table of contents.
Generate ePUB
To generate an ePUB from a Markdown file, you can use the following command:
pandoc file.md -f markdown -t epub -s -o file.epub \
--metadata title="Title of the Document" \
--metadata author="Author" \
--metadata language="en" \
--metadata rights="Any copyright message" \
--metadata date="`date +'%Y-%m-%d'`" \
--epub-title-page=false \
--toc
This will take the file.md
and generate a file.epub
with the metadata and a table of contents, you can then open this ePUB file on any reader, like Apple Books.
Use Makefile to automate
You can also create a Makefile
to automate the process, here is an example:
all: pdf epub
pdf:
pandoc $(file).md -f markdown -t pdf -s -o $(file).pdf \
--metadata title="$(title)" \
--metadata author="Your Name" \
--metadata language="en" \
--metadata rights="@2025 by Your Name, All Rights Reserved" \
--metadata date="`date +'%Y-%m-%d'`" \
--toc
epub:
pandoc $(file).md -f markdown -t epub -s -o $(file).epub \
--metadata title="$(title)" \
--metadata author="Your Name" \
--metadata language="en" \
--metadata rights="@2025 by Your Name, All Rights Reserved" \
--metadata date="`date +'%Y-%m-%d'`" \
--epub-title-page=false \
--toc
Now you can run this command to generate both PDF and ePUB:
make all file=your-file title="Your Title"
And you will have both files generated.