How to convert MD (markdown) file to PDF using Pandoc on macOS Ventura 13

Introduction

If you are reading this article, you may be already familiar with what MD (Markdown) document can do. One of the use cases would be converting the Markdown document into PDF file.

There are several ways you can convert the Markdown document into PDF file. In this article, we will be focusing on using Pandoc.

What is Pandoc?

Pandoc is a free and open-source document converter that can convert between many different markup formats, including Markdown, HTML, LaTeX, and Microsoft Word. It can also be used to create PDFs. Pandoc is a powerful tool that can be used for a variety of purposes, including:

  • Converting blog posts from Markdown to HTML for publication on a website.
  • Creating a presentation from a LaTeX document.
  • Exporting a research paper from Word to PDF.
  • Generating a book from a series of Markdown files.
  • Converting a website from one markup format to another.

Pandoc is also a popular tool for writing academic papers. It can be used to create a document in Markdown, which is a lightweight markup language that is easy to write and read. Pandoc can then be used to convert the Markdown document to LaTeX, which is a more powerful markup language that is better suited for typesetting mathematical equations and other complex content.

Pandoc is a versatile and powerful tool that can be used for a variety of purposes. It is a valuable asset for anyone who needs to convert documents between different markup formats.

Here are some additional details about Pandoc:

  • Pandoc is written in Haskell, a functional programming language.
  • Pandoc is available for Linux, macOS, and Windows.
  • Pandoc can be used as a command-line tool or as a library in other programs.
  • Pandoc supports over 60 input and output formats.
  • Pandoc is free and open-source software, released under the GPL license.

If you are looking for a versatile document converter, Pandoc is a great option. It is powerful, flexible, and easy to use.

In this article, we will focus mainly on converting the Markdown document into PDF document.

MD to PDF Conversion Requirements

To convert the Markdown document into PDF document, you need to set up the following:

  • Homebrew
  • Pandoc (version 3.13 or higher)
  • MacTex (needed to support the conversion to the PDF file)

Homebrew

Pandoc and MacTex will be installed using the Homebrew (brew install). If you do not have the Homebrew installed on your system, refer to How to setup Homebrew (brew install) on macOS 12 Monterey. (Article was written to target on macOS 12 Monterey, however, it should work on macOS 13 Ventura)

Pandoc Installation

To quickly check if you already have Pandoc in your system or to check the Pandoc version in your system, type the below command in the terminal.

pandoc - v

If you are getting the below message, you do not have the Pandoc installed in your system.

zsh: command not found: pandoc

Type the following command in the terminal.

brew install pandoc

Wait for the installation to complete and follow the instructions during the installation.

If you encounter a warning message like below;

Warning: You are using macOS 13.

We do not provide support for this pre-release version.

Try re-installing your Homebrew

https://brew.sh/

MacTex Installation

You will also need to install the MacTex. Use the below command to install the MacTex.

brew install --cask mactex

Note:

If installing MacTex via brew install does not work, you can download the package directly from the website.

Go to https://www.tug.org/mactex/mactex-download.html

And download the MacTeX.pkg.

Double-click the MacTex.pkg then follow the instructions.

Creating MD (Markdown) file

To create a basic MD (Markdown) file, you can follow the basic syntax in How to make MD (markdown) document.

Applying Document Decoration

Although you can convert the basic MD file to PDF file. However, you can also configure your MD to have additional decorations such as Table of Content (TOC), custom header, footer, page number, watermark, code formatting and more.

YAML Frontmatter Header Options

You can use the following example YAML Header options.

---
title: [replace with the document title]
subtitle: [replace with the document version]
 
header-includes:
  - \usepackage{sectsty}
  - \sectionfont{\clearpage}
 
  - \usepackage{titling}
    
   
  - \usepackage[
        scale=0.5,
        text=Watermark  
    ]
    {draftwatermark}
 
  - \usepackage{fancyhdr}
  - \pagestyle{fancy}
  - \fancyhf{}
  - \lhead{\leftmark}
  - \lfoot{\fontsize{6}{12} \selectfont (C) 2023 Tech-Cookbook.com All rights reserved. \\ This is a next line foot note.}
  - \rfoot{\thepage}
 
  
numbersections: true
colorlinks: true
linkcolor: black
fontfamily: arev
geometry: "left=0.75in,right=0.75in,top=1in,bottom=1in"
fontsize: 14pt
output: pdf_document
---


For applying Watermark, by interesting the following lines in the header-includes will include the watermarking in the document.
  - \usepackage[
        scale=0.5,
        text=Watermark  
    ]
    {draftwatermark}

For applying the header and footer, insert the following lines in the header-includes.

  - \usepackage{fancyhdr}
  - \pagestyle{fancy}
  - \fancyhf{}
  - \lhead{\leftmark}
  - \lfoot{\fontsize{6}{12} \selectfont (C) 2023 Tech-Cookbook.com All rights reserved. \\ This is a next line foot note.}
  - \rfoot{\thepage}

You can also apply other settings such as 

  • Adding the number in each section (use numbersections: true)
  • Adding color to the links (use colorlinks: true)
  • Make link color to black (use linkcolor: black)

And so much more settings. You can visit the https://pandoc.org/chunkedhtml-demo/6.2-variables.html

Convert from MD (Markdown) to PDF

Converting from MD to PDF is simple and easy. Use the following command in the terminal.

Command:

pandoc <source-md-file>.md -o <outputpdf-file>.pdf

Sample Command:

pandoc foo.md -o foo.pdf

Apply Style to your Code Blocks

To make your code blocks stand out from the documentation, you can apply the code highlight style. To apply the highlight style, make sure your code block syntax is applied properly.

For example, if you have a code block in the JSON format, wrap the codes starting with ``` and followed by json. It will look like ```json

Then, add your code inside then close with the ```.

The code should look like below:

```json
{
  "name": "John",
  "age": 30,
  "car": null
}
```

Other than json, a lot of language highlights are supported. To see the supported language highlights, use pandoc --list-highlight-languages

To apply the code-highlight in the PDF document, add the below argument.

Command:

pandoc –highlight-style <style name> <source-md-file>.md -o <outputpdf-file>.pdf

Sample Command:

pandoc –highlight-style <style name> <source-md-file>.md -o <outputpdf-file>.pdf

Use pandoc --list-highlight-styles to see the available styles.

Also you can refer to https://ycl6.github.io/Compare-pandoc-styles/ to see how each style will look like.

Wrap Up

Pandoc is a very powerful tool. It can do so many customizations in your documentation. This article focuses on the options commonly use and useful to have when converting it to PDF.

Latest Posts

Feel free to share this post!

Scroll to Top