DEV Community

Fernando B πŸš€
Fernando B πŸš€

Posted on • Edited on

Exporting a markdown book to PDF with Pandoc

I have been wanting to write a book for some time now. I've accumulated lots of experience both in my career in automation and controls, and on my own I have learned tons in software engineering, game dev, etc, the list is huge.

All this stuff rattling in my head needs to come out at some point, at my job and for my personal projects I tend to document very well and that has pushed me to get better at writing.

I have decided to write maybe a few books in markdown, well we'll see how the first one goes.

Setup

The markdown editor that you use makes no difference, I use Typora. If you are not sure vscode has pretty good support for markdown files, and most text editors do. @awwsmm did a great write up on markdown editors state-of-markdown-editors-2019

In addition to that, I use Pandoc, you'll need LaTeX to print PDFs, make sure to follow the instructions.

Exporting to PDF

I structured my chapters in directories, like shown below:

β”œβ”€β”€β”€title.txt
β”œβ”€β”€β”€Chapter1
β”œβ”€β”€β”€Chapter2
└───images
Enter fullscreen mode Exit fullscreen mode

The title is like a header for pandoc:

--------
title: Book Example
author: Fernando B.
rights: Nah
language: en-US
--------
Enter fullscreen mode Exit fullscreen mode

Pandoc Resources

The below command will add table of contents, output to book.pdf, get title info from title.txt and grab three markdown files.

pandoc --toc -o book.pdf title.txt .\Chapter1\Scene1.md .\Chapter1\Scene2.md .\Chapter2\Scene1.md
Enter fullscreen mode Exit fullscreen mode

Going beyond the command line

As you can imagine as your book grows, things will get harder to compile. I couldn't find a library or an easy parameter that takes a list of md files in a directory so I wrote a python script export_book.py. For now the script needs to be in the book root directory, but in the future I will probably expand on it.

Full Repo

Other items to think about

I need to experiment with css for formatting the book a bit better and adding header and footer, and so on, but this is a good start for anyone trying to accomplish the same or even writing academic papers as pandoc can output to different formats.

Top comments (2)

Collapse
 
learnbyexample profile image
Sundeep • Edited

pandoc is quite good and has plenty of options to convert markdown to pdf (and other formats) It has its own version of markdown with plenty of extensions and supports Github style markdown as well

I personally use Github one as it would make it easier to open source my books later on. I wrote a post with my current setup:

There's also a template I saw on Github, yet to try though:

GitHub logo Wandmalfarbe / pandoc-latex-template

A pandoc LaTeX template to convert markdown files to PDF or LaTeX.

Eisvogel

Build Status

A clean pandoc LaTeX template to convert your markdown files to PDF or LaTeX. It is designed for lecture notes and exercises with a focus on computer science. The template is compatible with pandoc 2.

Preview

A custom title page A basic example page
A custom title page A basic example page

Installation

  1. Install pandoc from pandoc.org/. You also need to install LaTeX.

  2. Download the latest version of the Eisvogel template from the release page.

  3. Extract the downloaded ZIP archive and open the folder.

  4. Move the template eisvogel.tex to your pandoc templates folder and rename the file to eisvogel.latex. The location of the templates folder depends on your operating system:

    • Unix, Linux, macOS: $XDG_DATA_HOME/pandoc/templates or ~/.pandoc/templates/
    • Windows XP: C:\Documents And Settings\USERNAME\Application Data\pandoc\templates
    • Windows Vista or later: C:\Users\USERNAME\AppData\Roaming\pandoc\templates

    If there are no folders called templates or pandoc you need to create them and put the template eisvogel.latex inside.

Usage

  1. Open the terminal and navigate…

Collapse
 
thefern profile image
Fernando B πŸš€

Thanks for the link to your post, information looks great! I think I am also going to stick with github style.