Guide

How to Convert Markdown to PDF with Pandoc

Updated

Run pandoc input.md -o output.pdf. Pandoc sees the .pdf extension and typesets the document through LaTeX (pdflatex by default), so a TeX distribution has to be installed. Add --pdf-engine=xelatex if the text contains anything beyond basic Latin characters, and -V geometry:margin=1in for normal margins. The rest of this page covers the options worth knowing and the errors you're likely to hit, each tested on Pandoc 3.11.

Markdown to PDF with Pandoc, step by step

  1. 1

    Install Pandoc and a PDF engine

    Install Pandoc, plus a TeX distribution (TeX Live, MacTeX or MiKTeX) for the default LaTeX route. Or skip both installs and use the pandoc/latex Docker image.

  2. 2

    Run the basic command

    pandoc input.md -o output.pdf. Pandoc picks PDF output from the .pdf extension.

  3. 3

    Switch engine for non-Latin text

    Add --pdf-engine=xelatex if the document contains symbols, accented scripts or anything beyond basic Latin characters.

  4. 4

    Set margins and links

    Add -V geometry:margin=1in for normal margins and -V colorlinks=true so links stand out.

  5. 5

    Save the options in a defaults file

    Put the flags in a YAML defaults file and run pandoc input.md -o output.pdf --defaults=defaults.yaml, so every document converts the same way.

No TeX installed? Use Docker

A full TeX distribution runs to several gigabytes. If you only need PDFs occasionally, the official image bundles Pandoc with a minimal LaTeX setup. Run it from the folder that holds your Markdown:

docker run --rm -v "$(pwd)":/data pandoc/latex input.md -o output.pdf

The image works in /data, so paths are relative to your current folder. It doesn't include your system fonts or optional engines like WeasyPrint, so mainfont and HTML engines need a custom image.

The options that matter for PDF

-o output.pdf
Output file. The .pdf extension is what makes Pandoc produce a PDF.
--pdf-engine=xelatex
Typeset with XeLaTeX: Unicode text and system fonts. lualatex works the same way.
-V geometry:margin=1in
Page margins. The LaTeX default margins are wide for most documents.
-V colorlinks=true
Coloured hyperlinks instead of plain text.
--toc
Table of contents built from your headings.
--syntax-highlighting=tango
Code highlighting style. Older Pandoc versions call this --highlight-style; 3.11 still accepts that but warns it's deprecated.
--resource-path=DIR
Where to look for images referenced with relative paths.
--defaults=FILE.yaml
Load all of the above from a file.

Pandoc's Markdown has no page-break syntax, but on the LaTeX route a line containing only \newpage passes through as raw LaTeX and starts a new page. A YAML block at the top of the file (title:, author:, date:) becomes the document's title block.

Worked example: a defaults file for client documents

Once you've settled on options, stop retyping them. Save this as defaults.yaml:

pdf-engine: xelatex
toc: true
variables:
  geometry: margin=1in
  colorlinks: true

Then convert any document with pandoc proposal.md -o proposal.pdf --defaults=defaults.yaml. You get XeLaTeX (so a ✓ or an accented client name doesn't stop the build), one-inch margins, coloured links and a table of contents. Commit the file next to your documents and everyone's PDFs come out the same.

Where this example falls short: adding mainfont: Inter to the variables works on a laptop with Inter installed and fails in CI or Docker with a fontspec error, because the font isn't there. Install the font in the build environment, or leave mainfont out of shared defaults.

Errors and what they mean

! LaTeX Error: Unicode character ✓ (U+2713) not set up for use with LaTeX.

The default pdflatex engine can't typeset that character. Rerun with --pdf-engine=xelatex (or lualatex).

[WARNING] Missing character: There is no ✓ (U+2713) in font [lmroman12-bold]…

What you get after switching to xelatex: the PDF builds, but the character is silently left out because the default Latin Modern font doesn't have it. Set -V mainfont to an installed font that does. Emoji need a font with emoji glyphs.

Package fontspec Error: The font "…" cannot be found

The mainfont you named isn't installed where Pandoc runs. That includes Docker containers, which have their own fonts, not yours.

'weasyprint' not found. Please select a different --pdf-engine or install 'weasyprint'

The engine isn't installed or isn't on PATH. You'll see the same message, with that engine's name, for any other engine you haven't installed.

[WARNING] Could not fetch resource img/logo.png: replacing image with description

The easy one to miss. Pandoc still writes the PDF and exits successfully, with the image's alt text in place of the image. Relative paths resolve from where you run the command, so run it from the Markdown file's folder or add --resource-path.

One more that doesn't produce a message: long lines in code blocks don't wrap on the LaTeX route, so they run off the right edge of the page. Break long lines in the source, or use an HTML engine and set white-space: pre-wrap on code in your CSS.

When the PDF is going to a client

Pandoc is excellent at turning Markdown into a correct document. Making it look like it came from your studio, with logo, brand colours and a proper cover, means writing a LaTeX template. Markpdf does that part: write the proposal or scope of work in Markdown and export it with your saved brand kit applied.

FAQ

Can Pandoc make a PDF without LaTeX?
Yes. Pandoc can hand off to HTML-based engines such as WeasyPrint, wkhtmltopdf or Prince, or to Typst, using --pdf-engine. Each has to be installed separately. If it isn't, Pandoc stops with "'weasyprint' not found. Please select a different --pdf-engine or install 'weasyprint'" (with the engine you asked for).
How do I change the font?
Use --pdf-engine=xelatex or lualatex and set -V mainfont="Font Name". The font has to be installed where Pandoc runs. If it isn't, the build fails with "Package fontspec Error: The font ... cannot be found". The default pdflatex engine doesn't use system fonts at all.
Why is my CSS ignored?
CSS only applies with an HTML-based engine, for example --pdf-engine=weasyprint --css=style.css. The default LaTeX route ignores CSS entirely and takes its look from LaTeX variables and templates.
How do I add a logo, header or footer?
On the LaTeX route that means a custom LaTeX template or header-includes in the YAML front matter. It's doable but fiddly, and it's the point where most people looking for a branded document stop using raw Pandoc.
How do I convert every Markdown file in a folder?
Loop over them in the shell: for f in *.md; do pandoc "$f" -o "${f%.md}.pdf"; done. Add your defaults file to the command so every file gets the same settings.

Related templates

← Back to home All templates All guides See pricing