How to Convert Markdown to PDF in VS Code
Export Markdown to PDF in VS Code with Markdown PDF or Markdown Preview Enhanced, or use your browser’s print dialog when you need selectable text.
VS Code’s built-in Markdown preview has no PDF export command. To turn your .md file into a PDF, you can use Markdown PDF in the editor, export from the preview with Markdown Preview Enhanced, or open the file in a browser converter.
A. Export from the editor with Markdown PDF
- In VS Code’s Extensions view, search for
yzane.markdown-pdfand install Markdown PDF. - Open your
.mdfile. PressF1orCtrl+Shift+P(⌘⇧Pon Mac) to open the Command Palette. - Type
exportand choosemarkdown-pdf: Export (pdf). You can also right-click inside the editor for the same export options, includinghtml,png,jpeg, andall.
The extension renders with Chromium through Puppeteer. It uses the browser set in markdown-pdf.executablePath. With that setting unset, it looks for an installed copy of Chrome, Edge, or Chromium. If it finds none, it downloads Chromium on first use. To disable the download, set markdown-pdf.chromium.autoDownload to false.
The default paper size is A4. For a Letter handout with equal margins, add these entries to settings.json.
{
"markdown-pdf.format": "Letter",
"markdown-pdf.margin.top": "1.5cm",
"markdown-pdf.margin.bottom": "1.5cm",
"markdown-pdf.margin.left": "1.5cm",
"markdown-pdf.margin.right": "1.5cm"
}
Use markdown-pdf.displayHeaderFooter to show headers and footers. Set their contents with markdown-pdf.headerTemplate and markdown-pdf.footerTemplate. Use markdown-pdf.outputDirectory for the destination folder and markdown-pdf.styles for custom CSS. Recent releases sanitize raw HTML by default.
B. Export from Markdown Preview Enhanced
- Install Chrome and the Markdown Preview Enhanced extension (
shd101wyy.markdown-preview-enhanced). - Open the Markdown file and run
Markdown Preview Enhanced: Open Preview. For a side preview, pressCtrl+K V(⌘K Von Mac). - Right-click inside the preview, choose
Chrome (Puppeteer), then select the PDF export.
To set the paper size and orientation for this document, put this YAML front matter at the top of the Markdown file.
---
puppeteer:
landscape: false
format: "A4"
---
C. Convert in the browser without an extension
Open ConverterTo’s Markdown to PDF, choose your file, and select A4 or Letter. The conversion runs in your browser. Choose the output according to what readers need.
- Create image-based PDF produces a download with page breaks between lines. Each page is an image, so readers cannot select the text.
- Print to PDF (selectable text) opens the browser’s print dialog. Save as PDF there if readers need to copy a passage.
Which method fits your document?
| Feature | Markdown PDF | Markdown Preview Enhanced | ConverterTo |
|---|---|---|---|
| Setup | Extension and Chromium-based browser | Extension and Chrome | Open the tool in a browser |
| Selectable text | Usually | Usually | Print to PDF |
| Custom CSS | markdown-pdf.styles | Preview styles | Built-in styling |
| Offline after setup | Usually, with local assets | Usually, with local assets | Needs a connection to load the tool and remote images |
| Relative images | Usually, if files are present | Usually, if files are present | Use absolute image URLs |
| Best for | Reusing export settings | Exporting the preview you work in | A one-off PDF |
Troubleshooting
- Chromium download blocked by a proxy? Set VS Code’s
http.proxyor pointmarkdown-pdf.executablePathto an installed browser. - Missing image? If the file links to
images/chart.png, check the path from the Markdown file’s folder. The browser tool cannot resolve sibling files from a single uploaded document. Use an absolute image URL there. Remote image hosts receive the request and can see your IP address. - Chinese, Japanese, or Korean characters appear as boxes? Check that the computer doing the export has a font installed that covers those characters.
- A section needs a new page? With Markdown PDF, put
<div class="page"></div>on its own line before the heading.
Frequently asked questions
Can I export every time I save? Enable markdown-pdf.convertOnSave for Markdown PDF and restart VS Code. For Markdown Preview Enhanced, add export_on_save: { puppeteer: true } as a top-level entry in the document’s YAML front matter.
What if readers need a web page or an editable copy? Use Markdown to HTML for a web page or Markdown to Word for an editable document.