Markdown to PDF Magic: Create a One-Command Converter for macOS
Learn how to streamline your documentation workflow by building a custom script that transforms Markdown files into professionally formatted PDFs with a single command. This step-by-step guide covers everything from installing prerequisites to creating and using your own mdtopdf tool on macOS.
How to Convert Markdown to PDF with a Custom Script
As a developer, I often find myself writing documentation in Markdown. While Markdown is great for version control and easy editing, sometimes I need to share my docs as PDFs. For example, I may want to read and mark up a document on my e-ink reader or iPad. Today, I'm going to show you how to create a simple script that converts Markdown files to PDFs with just one command.
Prerequisites
Before we dive into the script, make sure you have the following installed on your macOS system:
Homebrew: If you don't have it, install it with:
1/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"MacTeX: Install it using Homebrew:
1brew install --cask mactexPandoc: Install it using Homebrew:
1brew install pandoc
After installing MacTeX, you need to add it to your PATH. Add this line to your ~/.zshrc
file:
1export PATH="/usr/local/texlive/2024/bin/universal-darwin:$PATH"
Make sure to replace "2024" with the actual year of your TeX Live installation if it's different.
Creating the Script
Now, let's create our mdtopdf
script. Here's the complete script:
1#!/bin/bash23# Check if a filename was provided4if [ $# -eq 0 ]; then5 echo "❌ Error: No filename provided."6 echo "ℹ️ Usage: mdtopdf <filename.md>"7 exit 18fi910# Get the input filename11input_file="$1"1213# Check if the input file exists14if [ ! -f "$input_file" ]; then15 echo "❌ Error: File '$input_file' not found."16 exit 117fi1819# Check if the input file has a .md extension20if [[ "$input_file" != *.md ]]; then21 echo "❌ Error: Input file must have a .md extension."22 exit 123fi2425# Generate the output filename by replacing .md with .pdf26output_file="${input_file%.md}.pdf"2728# Run pandoc with xelatex and custom margin settings29pandoc "$input_file" -o "$output_file" \30 --pdf-engine=xelatex \31 -V geometry:"top=2cm, bottom=2cm, left=2cm, right=2cm" \32 -V papersize=a43334# Check if pandoc was successful35if [ $? -eq 0 ]; then36 echo "✅ Conversion successful. Output saved as '$output_file'"37else38 echo "❌ Error: Conversion failed."39 exit 140fi
To set up this script:
Create a new file named
mdtopdf
(without any extension) in a text editor.Copy and paste the above script into the file.
Save the file and make it executable:
1chmod +x mdtopdfMove the script to a directory in your PATH, for example:
1sudo mv mdtopdf /usr/local/bin/
Using the Script
Now you can use the script from anywhere in your terminal by running:
1mdtopdf your_markdown_file.md
The script will create a PDF with the same name as your Markdown file, but with a .pdf extension.
What the Script Does
- It checks if you've provided a filename as an argument.
- It verifies that the input file exists and has a .md extension.
- It generates the output filename by replacing the .md extension with .pdf.
- It runs pandoc with the xelatex PDF engine and some custom settings:
- It sets all margins (top, bottom, left, right) to 2cm.
- It sets the paper size to A4.
- It checks if the conversion was successful and provides appropriate output.
The script also includes some error handling and uses emojis in the output messages to make them more visually appealing and easier to read at a glance.
Conclusion
With this script, converting your Markdown files to PDFs becomes a breeze. No more fiddling with pandoc options every time you need a PDF - just run mdtopdf
and you're done! Feel free to modify the script to suit your needs, such as changing the margin sizes or paper format.
Happy converting!