Markdown to PDF in VS Code: Extensions and Settings
VS Code can’t export Markdown to PDF on its own. How to do it with the Markdown PDF and Markdown Preview Enhanced extensions, with margins and page numbers.
Updated · 5 min read
VS Code previews Markdown out of the box, but it has no built-in way to save that preview as a PDF. You need an extension. Two are widely used, and both print the document with a Chromium browser behind the scenes.
Method 1: the Markdown PDF extension
Markdown PDF (by yzane) is the simplest route.
- Open the Extensions view, search for Markdown PDF and install it.
- Open your
.mdfile. - Press F1 (or Ctrl/⌘+Shift+P), type
exportand choose markdown-pdf: Export (pdf). The same command is in the editor’s right-click menu.
The PDF is saved next to the Markdown file. The first export takes longer because the extension downloads its own copy of Chromium.
Settings worth changing
Add these to your settings.json (F1 → Preferences: Open User Settings (JSON)):
{
"markdown-pdf.format": "A4",
"markdown-pdf.orientation": "portrait",
"markdown-pdf.margin.top": "2cm",
"markdown-pdf.margin.bottom": "2cm",
"markdown-pdf.margin.left": "2cm",
"markdown-pdf.margin.right": "2cm",
"markdown-pdf.displayHeaderFooter": true,
"markdown-pdf.footerTemplate": "<div style=\"font-size: 9px; margin: 0 auto;\"><span class='pageNumber'></span> / <span class='totalPages'></span></div>",
"markdown-pdf.styles": ["markdown-pdf.css"],
"markdown-pdf.outputDirectory": "output",
"markdown-pdf.convertOnSave": false
}
| Setting | What it does |
|---|---|
markdown-pdf.format, markdown-pdf.orientation |
Paper size and orientation |
markdown-pdf.margin.* |
Page margins |
markdown-pdf.displayHeaderFooter |
Turns the header and footer on or off |
markdown-pdf.headerTemplate, markdown-pdf.footerTemplate |
HTML for the header and footer; pageNumber and totalPages spans are filled in for you |
markdown-pdf.styles |
Your own CSS files, applied to the PDF |
markdown-pdf.outputDirectory |
Where exported files go |
markdown-pdf.convertOnSave |
Re-exports every time you save the file |
markdown-pdf.executablePath |
Use an installed Chrome instead of the downloaded Chromium |
The extension renders Mermaid diagrams from mermaid code blocks and LaTeX math with KaTeX.
Method 2: Markdown Preview Enhanced
Markdown Preview Enhanced replaces the preview pane and can export from it. It uses the Chrome browser already on your computer, so Chrome must be installed.
- Install the extension and open its preview for your file.
- Right-click inside the preview and choose Chrome (Puppeteer), then PDF.
Page settings go in front matter at the top of the Markdown file:
---
puppeteer:
landscape: false
format: "A4"
---
To export automatically on every save, add:
---
export_on_save:
puppeteer: true
---
Troubleshooting
- The first export hangs or fails. The Chromium download was probably blocked. Point
markdown-pdf.executablePathat your installed Chrome and try again. - No page numbers. Check that
markdown-pdf.displayHeaderFooteristrueand the bottom margin is large enough to show the footer. - The PDF doesn’t look like the preview. The preview uses your editor theme; the PDF uses the extension’s own stylesheet. Add your CSS through
markdown-pdf.styles. - Images are missing. Use paths relative to the Markdown file, or full URLs.
Without installing an extension
If you only need a PDF now and then, or you’re on a machine where you can’t install extensions, copy the Markdown from VS Code and paste it into the online Markdown to PDF converter. It runs in the browser, keeps the file on your computer, and has page numbers, a table of contents, themes, math and Mermaid built in — no settings.json required.
For builds and scripts, the terminal inside VS Code can run Pandoc, or you can convert Markdown to PDF from Python.
Try it on your own document
Free, private and right in your browser — no signup.