How to Convert Markdown to PDF Without Losing Formatting (2026)
You wrote a clean Markdown doc. You hit “export to PDF.” Suddenly the code blocks wrap wrong, tables overflow the page, and half a paragraph is sliced in two.
This guide shows how to convert Markdown to PDF without losing formatting — including code, tables, images, links, and page breaks — using a free online converter, VS Code, and Pandoc.
Why Markdown → PDF Goes Wrong
Markdown is plain text; PDF is a fixed page. Most formatting bugs come from four gaps:
| Problem | What you see in the PDF | Cause |
|---|---|---|
| Code blocks cut off | Lines truncated at page edges | No page-break control |
| Tables clipped | Columns missing on the right | Page width too narrow |
| Fonts change | Serif body, odd mono sizes | Missing font mapping |
| Blank last page | Empty final page | Trailing newlines / margins |
Fix these and your PDF looks like the preview.
Method 1: Free Online Converter (Fastest)
Best when you want a one-click PDF with no install.
- Open the Markdown → PDF converter
- Paste your Markdown (or upload a
.mdfile) - Pick a template (Classic, Modern, Academic…)
- Click Download PDF
What stays intact:
- Headings and spacing
- Bold, italic,
inline code - Fenced code blocks with syntax highlighting
- Tables, lists, blockquotes
- Images and links
Tip: Use the template picker before exporting. Academic templates add tighter margins and better heading hierarchy for print.
Method 2: VS Code (Best for Docs in a Repo)
If the Markdown lives next to your code:
- Install Markdown PDF (or Markdown All in One + print)
- Open the
.mdfile Cmd/Ctrl+Shift+P→ Markdown PDF: Export (pdf)- Save next to the source file
VS Code preview is close to GitHub-flavored Markdown, so README screenshots match what you already reviewed.
Method 3: Pandoc (Best for Batch / CI)
For many files or automated builds:
pandoc README.md -o README.pdf --pdf-engine=xelatex
Useful flags:
# Keep code line breaks
pandoc doc.md -o doc.pdf --wrap=pre --highlight-style=tango
# Custom page size + margins
pandoc doc.md -o doc.pdf -V geometry:margin=1in -V papersize:a4
Pandoc is powerful but needs a LaTeX engine installed. For occasional exports, the online converter is simpler.
Keep These 6 Formatting Details Intact
1. Code blocks (especially long lines)
Prefer fenced blocks with a language tag:
```python
def hello():
print("Hello, PDF!")
```
- Use a language tag so highlighters work
- Avoid one-line dumps longer than ~80 characters when you can
- If a line must stay long, make sure the exporter wraps or scrolls — it should not clip
2. Tables
Keep headers short. Wide tables are the #1 reason columns disappear in PDF:
| Format | Best for | Editable? |
|--------|-----------------|-----------|
| Markdown | Writing | Yes |
| PDF | Sharing/print | No |
| DOCX | Review cycles | Yes |
If a table has many columns, split it or drop optional columns before export.
3. Page breaks (where it matters)
Most exporters respect simple HTML in Markdown:
<div style="page-break-after: always;"></div>
Use it before major chapters so a heading never lands alone at the bottom of a page.
4. Images
- Use relative paths (
./images/chart.png) or full HTTPS URLs - Prefer PNG/JPEG under ~1–2 MB
- Add alt text — it also becomes the PDF’s accessibility label
5. Links
In print, long URLs look noisy. Prefer descriptive link text:
[Markdown syntax guide](/blog/markdown-syntax-guide)
6. Headings hierarchy
Stick to one # for the title, then ## / ###. Skipping levels breaks PDF bookmarks and table-of-contents tools.
Checklist Before You Export
- One
#title only - Code blocks use fences + language
- Tables fit a portrait page (~6–8 columns max)
- Images load in preview
- No accidental HTML in the body
- Preview matches what you want to print
FAQ
How do I convert Markdown to PDF for free?
Paste your .md into an online Markdown-to-PDF tool and download. No signup, no watermark — everything runs in your browser.
Why are my code blocks cut off in the PDF?
Long lines plus no page-break handling. Use fenced blocks, wrap long lines, and export with a tool that splits oversized blocks instead of slicing mid-line.
Can I keep syntax highlighting in the PDF?
Yes. Use a converter that renders highlighted HTML before PDF output (most online tools and the VS Code Markdown PDF extension do this).
Markdown to PDF or Markdown to Word — which one?
Choose PDF for final share/print (layout locked). Choose Word when someone still needs to edit. See Markdown vs HTML vs LaTeX if you are still picking a source format.
How do I convert Markdown to PDF on Mac / Windows / Linux?
Same steps on every OS: use an online converter in the browser, or VS Code / Pandoc if you prefer a local toolchain.
Next steps
- Export a sample: Markdown → PDF
- Need editing later? Markdown → Word
- Learn the syntax details: Markdown syntax guide
- Quick lookup: Markdown cheat sheet