Convert Markdown to PDF using Docker, Pandoc and Asciidoctor

1 minute read

Today we had to deliver a .pdf file to a customer containing documentation about a system. The documentation was written in Markdown and needed to be converted to PDF. After some thoughts we did not find some straightforward way to do it, so we need it to convert Markdown to Asciidoctor using Pandoc first and then from Asciidoctor to PDF. In addition to that, we love Docker, so we used Docker containers to achieve that. The containers used:

We prepared quickly a small Bash script, which is the following (Listing of convertMdToPdf.sh):

#!/bin/bash

set -eux

INPUT_MD_FILE=bob.md
OUT_ADOC_FILE=tmpOut.adoc 
OUT_TMP_PDF_FILE=tmpOut.pdf
OUT_PDF_FILE=documentation.pdf

echo "Clear temporary files (if exist)"

# Clear any temporary files
(rm -f "${OUT_ADOC_FILE}" "${OUT_PDF_FILE}" "${OUT_TMP_PDF_FILE}" || true)

# Convert Markdown to Asciidoctor
docker run --rm -v "$(pwd)":/data pandoc/core  -f markdown -t asciidoc -i "${INPUT_MD_FILE}" -o "${OUT_ADOC_FILE}"

# Convert Asciidoctor to PDF
docker run --rm -v $(pwd):/documents/ asciidoctor/docker-asciidoctor asciidoctor-pdf "${OUT_ADOC_FILE}"

# Rename the output PDF to a more proper one
mv "${OUT_TMP_PDF_FILE}"  "${OUT_PDF_FILE}"

This is it! Very simple and straightforward! I hope this article helps you and you will not devote much time to do the same. Have a nice day from a stormy Greece!

Leave a Comment