DEV Community

David Carr
David Carr

Posted on • Originally published at dcblog.dev on

3 3

Generate PDF and Epub files using Pandoc

I write my books using Markdown. Using a tool called Pandoc you can convert Markdown files into PDF's and Epub files. Lets take a look at the commands.

Install Pandoc with Homebrew

brew install pandoc
Enter fullscreen mode Exit fullscreen mode

Converte a .md file to .pdf aka generate a PDF

pandoc demo.md --pdf-engine=xelatex -o demo.pdf
Enter fullscreen mode Exit fullscreen mode

Note when generating a PDF the option --pdf-engine is required.

The syntax is pandoc followed by the source file add any options with the -- flag. Set the output location and filename with the -o flag.

if you encoutner this error:

pandoc: /Library/TeX/texbin/pdflatex: createProcess: posix_spawnp: illegal operation (Inappropriate ioctl for device)

It means xelatex is not installed on the machine.

To install xelatex:

brew tap homebrew/cask
brew install basictex
eval "$(/usr/libexec/path_helper)"
sudo tlmgr update --self
sudo tlmgr install texliveonfly
sudo tlmgr install xelatex
sudo tlmgr install adjustbox
sudo tlmgr install tcolorbox
sudo tlmgr install collectbox
sudo tlmgr install ucs
sudo tlmgr install environ
sudo tlmgr install trimspaces
sudo tlmgr install titling
sudo tlmgr install enumitem
sudo tlmgr install rsfs
Enter fullscreen mode Exit fullscreen mode

Running the command again:

pandoc demo.md --pdf-engine=xelatex -o demo.pdf
Enter fullscreen mode Exit fullscreen mode

You may see:

[WARNING] Missing character: There is no (U+251C) (U+251C) in font [lmmono10-regular]:!

You can either install a font that supports the symbols, often caused by emoji's for example

Once the above has been corrected you will be able to generate PDF from Markdown files using:

pandoc demo.md --pdf-engine=xelatex -o demo.pdf
Enter fullscreen mode Exit fullscreen mode

To add a table of contents use the option --toc in the command

Make PDF:

pandoc demo.md --pdf-engine=xelatex --toc -o demo.pdf
Enter fullscreen mode Exit fullscreen mode

Make Epub:

pandoc demo.md --toc -o demo.epub
Enter fullscreen mode Exit fullscreen mode

Front Matter

When working with PDF/Markdown you can specify YAML tags in the markdown to set the book title, author and event the cover image with working with EPUB

This should be at the top of the file, it will not be printed.

---
title: Demo Book
creator:
- role: author
  text: David Carr
cover-image: cover.jpg
---
Enter fullscreen mode Exit fullscreen mode

Write a book

Here is a basic markdown file; this has chapters designated by # (h1) sub headings can be added by using ## (h2)

---
title: Demo Book
creator:
- role: author
  text: David Carr
cover-image: cover.jpg
---

# Chapter 1

An example chapter.....

# Chapter 2

This is super basic

## Sub chapter that belongs to chapter 2

>Markdown is awesome!

# Chapter 3
Enter fullscreen mode Exit fullscreen mode

Convert this to a PDF or Epub file to produce:

ebook

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more