Markdown to PDF:four ways to convert an .md file.

You have a Markdown file and need a PDF. Here are four ways to make one on any system, what each keeps, and how to get a Word file too.

macOS 14 or later. Reading is free; editing and reviews are one payment.

Quick answer

To convert Markdown to PDF, export the file from a Markdown app, run pandoc, or print the rendered file from a browser. On a Mac, DotMD Editor does it with File → Export as PDF…, and the diagrams and formulas are drawn. pandoc makes a PDF with pandoc notes.md -o notes.pdf, but it needs a LaTeX engine installed unless you choose another PDF engine.

pandoc notes.md -o notes.pdf

·

Markdown to PDF: four ways to convert an .md file

There is no single built-in command for turning an .md file into a PDF. Each of these four ways works; they differ in what they draw and what you must install.

  1. Export from a Markdown app

    An app that renders Markdown can save the rendered page as a PDF. On a Mac, open the file in DotMD and choose File → Export as PDF…. The PDF looks like the rendered page, with tables, code, Mermaid diagrams and KaTeX formulas drawn. Export is part of DotMD Editor; reading is free, and pricing has the details. The full steps are in Markdown to PDF.

  2. Use pandoc on the command line

    pandoc is a free converter. Give the output file a .pdf extension:

    pandoc notes.md -o notes.pdf

    By default pandoc uses LaTeX to make the PDF, so a LaTeX engine must be installed. The pandoc manual says it can also go through ConTeXt, roff ms or HTML. Pick one with --pdf-engine; the values it lists include pdflatex, lualatex, xelatex, tectonic, wkhtmltopdf, weasyprint and typst.

    pandoc notes.md --pdf-engine=xelatex -o notes.pdf
  3. Use Visual Studio Code

    VS Code shows a Markdown preview: press ⇧⌘V (Ctrl+Shift+V on Windows and Linux) in the editor. Its Markdown documentation does not mention PDF export, so the preview alone cannot save a PDF. An extension can add that; check an extension's page and reviews before you install one. Without an extension, use way four on the exported page, or way two.

  4. Print the rendered file from a browser

    Open the rendered file in a browser, for example the file in its GitHub repository, open the print dialog and choose Save as PDF. This needs nothing installed. GitHub draws Mermaid diagrams and math in Markdown files; check that they appear in the printout.

Which way to choose

WayWorks onTables and codeMermaid and mathNeeds installing
DotMD EditorMacYesDrawnThe app
pandocMac, Windows, LinuxYesMath with a LaTeX engine. Diagrams stay as code unless you add a filterpandoc and a PDF engine
VS CodeMac, Windows, LinuxIn the previewDepends on the extensionVS Code and an extension
Browser printAny systemYesDrawn on GitHub; check the printoutNothing

For one file on a Mac, use the app. For many files, or for a build script, use pandoc. For a quick copy of a file that is already on GitHub, print the page.

Online converters

A web converter takes your file by uploading it to someone else's server. For a public README that may not matter. For a spec, a contract or anything internal, it does. A converter on your own machine, such as the app or pandoc, keeps the file where it is.

Markdown to Word (.docx)

pandoc also writes Word files. The pandoc demos page converts a text file with this form of the command:

pandoc -s notes.md -o notes.docx

pandoc picks the output format from the .docx extension and does not go through LaTeX for this. Open the result in Word and check the tables and the images. To paste formatted text into Google Docs instead, DotMD's Copy as Rich Text copies the rendered page.

Common problems

  • Diagrams are missing. A Mermaid block is code until something draws it. Use an app or a page that draws diagrams, or check the output before you send it. See Mermaid flowcharts.
  • A wide table is cut off. A table with many columns does not fit a portrait page. Shorten the cell text, remove a column, or print in landscape. See Markdown tables.
  • A heading sits alone at the bottom of a page. Check the pages of the finished PDF and move a paragraph or a section if needed.
  • Images are missing. Images use paths relative to the .md file. pandoc looks for them in the folder where you run it by default; the --resource-path option changes that. See Markdown images.
  • Formulas show as plain text. Math needs a renderer. DotMD draws KaTeX formulas; see Math formulas.

Questions about converting Markdown to PDF

How do I convert an .md file to PDF?

Export it from a Markdown app, run pandoc with a .pdf output file, or open the rendered file in a browser and print it to PDF. The table above shows what each way keeps.

Why does pandoc say it cannot make the PDF?

By default pandoc needs a LaTeX engine to make a PDF. Install one, or choose another engine with --pdf-engine, for example weasyprint or typst, which also have to be installed.

Can I convert Markdown to Word?

Yes. With pandoc, pandoc -s notes.md -o notes.docx writes a Word file. Check tables and images in Word afterwards.

Is it safe to use an online converter?

The file is uploaded to a server you do not control. Do not use one for private or internal documents. Converting on your own machine keeps the file local.

Does VS Code export Markdown to PDF?

Its Markdown documentation describes a preview and does not mention PDF export. An extension is needed, or you can print the rendered page from a browser.

More questions