MD to PDF

How to convert Markdown to PDF
every method, tested

To convert Markdown to PDF, choose by situation. For a one-off document, paste it into a browser converter and download the file. For anything scripted or repeated, use Pandoc on the command line. If you already work in VS Code or Obsidian, their export works when it works — the catch with each is covered below. Every command on this page was run end to end to produce a PDF.

Which way to convert Markdown to PDF

MethodBest for
A browser converterOne document, converted by hand, with nothing to install.
Pandoc, on the command lineAnything automated, repeated, or part of a build.
PythonGenerating PDFs from inside a Python program.
VS Code, with an extensionPeople who already write in VS Code, on a machine with open internet access.
Obsidian's built-in exportA note that only ever lives in Obsidian.
Printing a rendered previewA quick copy when the formatting does not much matter.

A browser converter

Paste the Markdown into a page, check the preview and download the PDF. No install, no account, and the rendering engine is the browser you already have open.

  1. Open the converter and paste your Markdown, or drag the .md file onto the page
  2. Check the preview — it is laid out at the real page size with the real margins, so a line that wraps there wraps in the PDF
  3. Set the typeface, page size and margins if you want, then download the PDF

What to watch for

  • Images referenced by relative path, like ./images/a.png, have nothing to resolve against once the text leaves its folder. Use absolute URLs or data URIs
  • It is not scriptable — for a build step, use Pandoc instead

Tested: This site's own converter, which every other claim on this site is measured against.

Pandoc, on the command line

The most capable option and the right one for a pipeline. Pandoc does not produce PDFs on its own: it hands the document to a PDF engine, and which engine you choose changes the result more than any flag does.

  1. Install Pandoc from pandoc.org or your package manager
  2. Install a PDF engine. Typst is a single small binary; a LaTeX engine is the default but needs a TeX distribution
  3. Run the conversion, naming the engine explicitly so the result does not depend on what happens to be installed
pandoc notes.md -o notes.pdf --pdf-engine=typst
The lightest setup: Typst as the engine, no TeX distribution needed.
pandoc notes.md -o notes.pdf --pdf-engine=tectonic --toc -V geometry:margin=2cm
Through LaTeX, with a table of contents and set margins. Tectonic fetches the TeX packages it needs on demand.

What to watch for

  • Long code lines are clipped at the page edge rather than wrapped, through both engines — in testing, 99 characters of a 400-character line survived through Typst and 92 through LaTeX, and the rest was silently lost
  • Mermaid diagrams are not rendered without a separate filter
  • Plain pandoc with no --pdf-engine defaults to pdflatex, and stops with "pdflatex not found" unless a full TeX distribution is installed. Naming the engine avoids depending on what happens to be on the machine
  • Its own syntax — definition lists, x^2^ superscript — will not carry over if the same file is later converted with anything else

Tested: Run end to end on macOS with pandoc 3.12.1, Typst 0.15.1 and Tectonic 0.17.0. Both commands produced a PDF from the same test document; the long-line and Mermaid results above come from that run.

Python

When the conversion is one step in a larger script, a library avoids shelling out. The markdown-pdf package has no system dependencies to install, which is the usual stumbling block with Python PDF libraries.

  1. Install the package with pip
  2. Add the Markdown as a section and save the file
pip install markdown-pdf
Install the package.
from markdown_pdf import MarkdownPdf, Section

pdf = MarkdownPdf(toc_level=2)
with open("notes.md", encoding="utf-8") as f:
    pdf.add_section(Section(f.read()))
pdf.save("notes.pdf")
Convert a file, with headings turned into a table of contents up to level 2.

What to watch for

  • Neither Mermaid nor LaTeX maths is rendered
  • Styling is controlled through CSS passed to each section rather than through visible settings

Tested: Run end to end with markdown-pdf 1.13.3 on Python 3.14, exactly as shown. The Mermaid and maths results above come from that run.

VS Code, with an extension

VS Code has no PDF export of its own, so this needs an extension. The popular ones produce print-accurate output by driving a headless copy of Chromium, which is also where they tend to fail.

  1. Install a Markdown-to-PDF extension from the Extensions view
  2. Open the .md file, then run the extension's export command from the Command Palette
  3. On first run, expect it to download its own copy of Chromium before anything is produced

What to watch for

  • The Chromium download is a few hundred megabytes from a Google host, which is exactly what a corporate proxy or a locked-down network blocks — and the error rarely says so
  • In Remote-SSH, Codespaces or a devcontainer there is often no GUI to run a browser in at all
  • Mermaid and LaTeX usually need extra configuration before they render

Tested: Not run from here: an extension cannot be driven headlessly from this test setup. The steps describe the documented flow, and the Chromium behaviour is what these extensions state in their own setup notes.

Obsidian's built-in export

Obsidian can export the open note to PDF directly. It is the quickest route for a note already in the vault, with the trade-off that the result goes through Obsidian's own renderer rather than one designed for paper.

  1. Open the note
  2. Run Export to PDF from the Command Palette, or from the note's More options menu
  3. Choose page size and margins in the export dialog, then save

What to watch for

  • Themes and CSS snippets can change how the export looks, so the PDF depends on how your vault happens to be set up
  • Wikilinks resolve against the vault, so they cannot become working links in a file that has left it
  • Transclusions and Dataview queries depend on the vault too — check what they actually produced in the exported file

Tested: Not run from here: Obsidian is a desktop app that cannot be driven headlessly from this setup. The steps describe its documented export flow.

Open the Markdown somewhere that renders it — a README on GitHub, or an editor's preview pane — then print the page and choose Save as PDF. It needs nothing at all, which is its whole appeal.

  1. Open the rendered Markdown in a browser or preview pane
  2. Print it, and choose Save as PDF as the destination
  3. Turn off the browser's headers and footers in the print dialog, or every page carries the date and URL

What to watch for

  • You are printing a web page, so the site's own layout, navigation and margins come along with the content
  • There is no control over page breaks, so headings and tables land wherever the page happens to break

Tested: A browser's own print-to-PDF, which needs no install to check.

When the PDF comes out wrong

Images are missing from the PDF
Almost always relative paths, like ./images/a.png, which only mean something next to the original file. Replace them with absolute URLs, or embed the images as data URIs so nothing has to be fetched.
Diagram labels are cut off mid-word
The diagram's boxes were sized before the web font finished loading, so the real text was drawn into boxes measured for a different font. A converter has to load fonts before drawing, which not every one does.
Prices turned into italic maths
Two dollar signs on one line open and close inline maths in many renderers, so the text between two prices gets typeset as a formula with its spaces removed. Escape the dollar signs, or use a converter that only treats $ as maths when it hugs its content.
A wide table runs off the page
Nothing makes fourteen columns fit on A4 portrait. Switch to landscape, reduce the columns, or split the table in two.
Long code lines disappear past the page edge
Some engines clip rather than wrap. Check the end of your longest line in the finished PDF — if it is missing, the engine clipped it, and the content is gone rather than merely hidden.

Going further

For a construct-by-construct breakdown of what survives conversion — measured by converting each one and inspecting the file — see which Markdown features survive PDF export. To convert a document now, there is a free markdown to pdf converter on this site, and a page specifically for turning a .md file to PDF.