Marcio Cunha

How to generate PDF from Markdown on Linux with Pandoc

Learn how to automate the conversion of technical documentation in Markdown into professional PDF reports on Linux using Pandoc and WeasyPrint.

Marcio Cunha•4 min
Also available in:PortuguêsEspañol
Summary
  • Pandoc acts as a universal markup converter that translates plain text files into dozens of final formats.
  • WeasyPrint processes advanced CSS style sheets to generate printed page layouts in PDF with high visual fidelity.
  • Automating technical documentation eliminates manual labor and standardizes report delivery for engineering teams.
  • HTML and CSS templates allow you to customize margins, headers, footers, and institutional colors in generated documents.
  • Continuous integration pipelines can trigger PDF compilation whenever updates occur in repository files.

Why convert Markdown documentation to PDF on Linux

Keeping technical documentation up to date takes time, but distributing it to clients and managers often requires the PDF format. Markdown has become the industry standard for writing plain text due to its simplicity, but it lacks native pagination features. When you need to deliver elegant reports without abandoning the terminal-based workflow, automated conversion tools save the day. In practice, this means turning plain text files into polished corporate documents with just a single command.

Many teams try to use visual editors for this task, which breaks version control traceability. Keeping documentation in code repositories and compiling final artifacts in an automated way ensures that the printed document accurately reflects the current state of software. For those working with process automation, such as efficient file management via the command line, integrating this routine into the Linux environment is a natural and highly productive path.

Understanding the role of Pandoc in text conversion

Pandoc is often called the Swiss Army knife of document conversion. It reads a file in an input format and translates it into practically any other format imaginable. Under the hood, it analyzes the logical structure of your text, identifying headers, lists, and code blocks, and then reassembles them into the target structure.

Although Pandoc can generate PDFs directly using LaTeX, configuring the LaTeX typesetting engine on Linux distributions can be a complex and heavy task. This is where the combination with modern HTML and CSS engines comes in. By instructing Pandoc to convert Markdown to intermediate HTML, we pave the way to use web-based visual tools to render the final PDF with total control over design.

Setting up the environment with Pandoc and WeasyPrint

To start generating styled documents on Linux, we need to install the essential tools on the operating system. The process is straightforward and can be performed using your favorite distribution's package manager, such as APT on Ubuntu or Debian. Before proceeding, it is worth remembering that keeping the system updated helps avoid graphic dependency conflicts.

Open your terminal and run the commands below to install Pandoc and WeasyPrint, which will be our CSS-based rendering engine to generate static web pages converted into PDF with perfect pagination support.

sudo apt update
sudo apt install pandoc python3-weasyprint -y

With the packages installed, the environment is ready to receive your text files and transform them into professional reports. If you need to handle complex media formatting routines in the terminal, it is worth checking complementary tips on multimedia file processing on Linux to speed up your daily workflow.

Writing the first document and applying CSS styles

The great differential of using WeasyPrint alongside Pandoc is the ability to style the document using standard web CSS style sheets. This means you can define custom fonts, exact margins for A4 printing, controlled page breaks, and dynamic headers with automatic page numbers.

Create a file named document.md with your Markdown content and a style.css file to define the visual appearance. Then, use the Pandoc command directing the PDF engine to WeasyPrint. This way, the tool reads the Markdown, applies the HTML template, and renders the PDF with the exact typography chosen.

pandoc document.md --pdf-engine=weasyprint -c style.css -o report.pdf

This modular approach completely separates content from visual presentation. If your company's visual identity changes, you don't need to rewrite a single line of technical documentation; just update the CSS file and recompile the project files.

Automating compilation with Shell scripts

Running long commands manually in the terminal every time a Markdown file changes quickly gets tiring. The best practice in software engineering is to encapsulate these repetitive tasks in small executable scripts or automation files, allowing you to compile dozens of reports in seconds.

You can create a simple Bash script that searches for all Markdown files in a folder and generates the respective styled PDFs. This routine can be integrated into continuous integration tools to run automatically whenever there is a new commit in the company's documentation repository.

#!/bin/bash
for file in *.md;
  do
    output="${file%.md}.pdf"
    pandoc "$file" --pdf-engine=weasyprint -o "$output"
    echo "Generated: $output"
done

With this script running, any member of the technical team can generate professional-grade printed documentation just by running a command in the terminal, without needing to master complex and proprietary desktop publishing tools. To explore these architectural concepts further in production, see our related technical analysis on How to test an NTC thermistor with a bench multimeter.

Final considerations

Automating PDF generation from Markdown files on Linux transforms how technology teams handle documentation and reports. Combining Pandoc with WeasyPrint offers the perfect balance between the simplicity of plain text writing and the visual sophistication demanded by clients and managers. By integrating this routine into your daily development workflow, you gain speed, ensure consistency, and eliminate proprietary software dependencies in your workspace.

Adopting a code-based workflow to generate visual artifacts reinforces a clean and reproducible engineering culture. Whether documenting APIs, creating internal manuals, or generating complex invoices and technical reports, mastering these tools opens a new range of possibilities to optimize your time and elevate the quality of your team's deliverables.