# Use Pandoc to convert a Markdown to a PDF and ePUB

Used: pandoc@3.6.2


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

```sh
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.

```sh
brew install mactex
```

## Generate PDF

To generate a PDF from a Markdown file, you can use the following command:

```sh
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:

```sh
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:

```makefile
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:

```sh
make all file=your-file title="Your Title"
```

And you will have both files generated.
