Blog
How to Convert Markdown to PDF Without Losing Layout
Convert Markdown to PDF with headings, tables, code and math intact. What breaks in the conversion, how to fix page breaks, and a no-install method that works.
Markdown is designed for screens: a text flow that reflows to any width. PDF is designed for pages: fixed dimensions, fixed breaks, fixed fonts. Converting between them is easy; converting well is where most tools quietly fall apart — code blocks split mid-line, tables run off the page, and the first heading lands half-way down page one.
This guide covers the three realistic routes and the specific settings that keep a document readable on paper.
Three ways to convert Markdown to PDF
| Route | Install | Handles code/tables/math | Effort | Best for |
|---|---|---|---|---|
| Browser converter | None | Yes | Low | One-off documents, private files |
| Pandoc + LaTeX | Pandoc + TeX distribution | Yes, with the right engine | High | Automated, repeatable builds |
| VS Code / editor export | Editor extension | Depends on extension | Medium | Developers already in the editor |
1. A browser-based converter (no install)
Open the Markdown to PDF converter, paste the Markdown or drop the .md file, check the preview, and print to PDF. The document is rendered in your browser — including tables, code blocks with syntax highlighting, and maths — and the PDF is produced by the browser’s own print pipeline.
Because nothing is uploaded, this is the sane choice for internal docs, contracts or anything you would not paste into a random web service.
Practical settings for the print dialog:
- Destination: Save as PDF.
- Paper size: A4 or Letter to match the document’s intended audience.
- Margins: Default is usually right; narrow margins for wide tables.
- Headers and footers: Off, unless you want a URL printed on every page.
- Background graphics: On, if your document uses table header shading or highlighted code blocks.
2. Pandoc (the repeatable route)
pandoc README.md -o README.pdf --pdf-engine=xelatex -V geometry:margin=2.5cm
Pandoc converts Markdown to LaTeX and then to PDF. It gives you real control — page geometry, fonts, a table of contents, numbering — at the cost of a TeX installation and a non-trivial template when you want anything beyond default styling.
Use it when the PDF is a build artefact that must be regenerated identically, for example in CI.
3. Editor export
VS Code’s Markdown PDF extension (and equivalents in Obsidian) render through a headless browser, so the result looks like the preview. Convenient if you already have the file open; less convenient when you need a repeatable, scriptable pipeline.
What breaks, and how to fix it
Code blocks split across pages
A long code block will be cut at a page boundary, leaving a line of code split in two. Fixes:
- Add a page-break hint before the block: in HTML output,
<div style="break-before: page"></div>. - Keep code lines under ~80 characters so the block is shorter overall.
- Reduce the code font size slightly in your print CSS, which fits more lines per page.
Tables run off the right edge
Markdown tables do not wrap; wide tables simply overflow. Either narrow the content (fewer columns, shorter labels), or switch the table to stacked rows for print. If a table must stay wide, use landscape orientation for that section.
Images disappear or render at full resolution
Relative image paths break as soon as the Markdown file is viewed outside its folder. Use absolute URLs, or embed images, before converting. Oversized screenshots also push everything after them to the next page; set a max width in CSS.
The document starts with a blank half-page
Usually a top margin plus an <h1> with a large margin above it. Set h1 { margin-top: 0 } in your print styles, or remove the top margin.
Headings get orphaned at the bottom of a page
Add break-after: avoid to heading rules in print CSS. Browsers honour this in their print engine, and it is the single highest-value print rule.
Fonts change in the PDF
If the document relies on a font that exists only in the editor, the PDF falls back to a system font. Browser conversion keeps whatever font the page loaded — which is why it tends to look closest to the preview.
A print stylesheet that behaves
If you are converting with your own toolchain and can inject CSS, this is a reasonable starting point:
@page {
size: A4;
margin: 18mm 16mm;
}
body {
font-size: 11pt;
line-height: 1.55;
}
h1, h2, h3 {
break-after: avoid;
}
pre, table, blockquote {
break-inside: avoid;
}
pre {
white-space: pre-wrap;
word-break: break-word;
font-size: 9pt;
}
img {
max-width: 100%;
}
The two break-* rules are what separate a professional PDF from a raw browser print. break-inside: avoid on pre and table prevents the worst splits; white-space: pre-wrap on code stops lines being clipped.
Markdown features and their PDF behaviour
| Markdown feature | In a well-made PDF | Watch out for |
|---|---|---|
| Headings | Numbered or styled | Orphaned headings at page bottom |
| Tables | Rendered with borders | Overflow on wide tables |
| Code fences | Monospace, highlighted | Long lines clipped, blocks split |
Math ($…$) |
Typeset if KaTeX/MathJax is loaded | Raw $ in the PDF if maths is not rendered |
| Mermaid diagrams | Rendered as images | Blank boxes if the diagram library did not load |
| Task lists | Checkboxes | Checkboxes lost in some engines |
| Links | Clickable | Non-obvious that text is a link |
Maths and diagrams are the two features that most often produce a blank space rather than an error: the converter prints before the renderer finished. If your first attempt has empty boxes, re-render and print again once the preview is fully painted.
A short checklist before you export
- Preview the rendered document, not the Markdown source.
- Check the widest table — if it overflows in preview, it will overflow in print.
- Confirm code lines are under ~80 characters.
- Make sure images load (absolute URLs or embedded data).
- Print one page to check margins and font size before generating the whole file.
- Name the output meaningfully —
project-plan-2026-11.pdfbeatsdocument.pdf.
Frequently asked questions
Can I convert Markdown to PDF on a phone?
Yes. A browser converter works on iOS and Android, and the print dialog offers “Save as PDF” or “Print to PDF” on both.
Does the PDF keep clickable links?
Browser print pipelines preserve links. Pandoc depends on the LaTeX engine and options used.
Can I convert back from PDF to Markdown?
Yes, with limits. The PDF to Markdown converter rebuilds headings, lists and paragraphs from the PDF’s text layer; scanned PDFs without a text layer cannot be converted without OCR.
Why is my PDF missing the table of contents?
A table of contents is not Markdown syntax. It has to be generated — Pandoc’s --toc, or an inserted list of links — before conversion. A viewer that shows an outline sidebar is showing navigation, not a document section.