Convert Markdown to HTML & PDF (and Back): Complete Tutorial (2026)
A hands-on tutorial for converting between Markdown, HTML, and PDF — clean sanitized HTML for any CMS, watermark-free PDFs, and HTML-to-Markdown for rescuing rich text. With a CI-ready API.
Conversion is where Markdown earns its keep: write once in plain text, publish anywhere as HTML or PDF — and pull messy rich text back into clean Markdown when you need to. This tutorial covers all three directions with the convert tool group: Markdown → HTML, Markdown → PDF, and HTML → Markdown.
You'll learn which converter fits each job, the gotchas that trip people up, and how to automate the same conversions with an API.
The three conversions
| Direction | Tool | Typical job |
|---|---|---|
| Markdown → HTML | Markdown to HTML | Paste into a CMS, email, web page |
| Markdown → PDF | Markdown to PDF | Reports, resumes, handouts |
| HTML → Markdown | HTML to Markdown | Rescue content from rich text |
Part 1: Markdown → HTML
The Markdown to HTML converter turns your document into clean, sanitized, standalone HTML — a self-contained .html file with embedded styles you can paste into any CMS or open in a browser.
Why "sanitized" matters
The output strips unsafe constructs (<script>, inline event handlers, javascript: URLs), so it's safe to paste into a CMS or inject into a page. This is the same sanitization the render API applies — essential when the source includes user- or AI-generated content (see safe HTML for AI apps).
Workflow
- Draft in the editor.
- Convert with Markdown to HTML.
- Paste into your CMS as HTML, or use the standalone file directly.
For WordPress specifically, paste the HTML into a Custom HTML block — see the WordPress Markdown guide. For the full conversion theory (parsers, GFM, sanitization), read the Markdown to HTML guide.
Part 2: Markdown → PDF
The Markdown to PDF tool uses your browser's native print engine — which means:
- No upload — your document never leaves your machine.
- No watermark — unlike many online converters.
- Selectable text and crisp fonts — not a flattened image.
- Sharp math and diagrams — KaTeX and Mermaid render at vector quality.
Getting print right
PDF output is really "print to PDF," so print CSS rules apply:
- Use page breaks deliberately (the tool handles
##section breaks well). - Keep tables narrow enough for the page width.
- Preview before exporting — long code lines may overflow; wrap or shorten them.
The Markdown to PDF guide covers print CSS and page-break control in depth.
Part 3: HTML → Markdown (the rescue direction)
Got content trapped in rich text — a webpage, a Word doc pasted as HTML, an email? The HTML to Markdown converter extracts clean GitHub Flavored Markdown, preserving:
- Tables (as pipe tables)
- Code blocks with language hints
- Links, images, and task lists
- Headings and lists
It strips <script>, <style>, and presentational cruft automatically. This is the fastest way to migrate legacy content into a Markdown-based system.
Cleanup after conversion
HTML→Markdown output is usually good but benefits from a pass through the formatter (normalize bullets, whitespace) and the link checker (catch links that broke in translation).
A round-trip example
Migrating a blog post from an old CMS to a Markdown repo:
- Copy the post's HTML → HTML to Markdown.
- Clean with the formatter.
- Fix relative links flagged by the link checker.
- Commit the
.mdto your repo (docs-as-code). - Publish back out as HTML or PDF.
Markdown becomes your durable source of truth; HTML and PDF are disposable outputs.
Automating with the API
For bulk or pipeline conversion:
| Direction | Endpoint |
|---|---|
| Markdown → HTML | /api/v1/render |
| HTML → Markdown | /api/v1/convert/html-to-markdown |
| Markdown → plain text | /api/v1/convert/markdown-to-text |
curl -sf -X POST https://trymarkdownviewer.com/api/v1/render \
-H "Authorization: Bearer $MDV_API_KEY" \
-H "Content-Type: application/json" \
-d '{"markdown": "# Hello\n\nWorld."}'
# → { "data": { "html": "<h1 id=\"hello\">Hello</h1>...", "characters": 14 } }
PDF is browser-side by design (the print engine lives in the browser), so PDF export stays a client-side step.
Common conversion mistakes
1. Pasting unsanitized HTML into a CMS. Use the sanitized HTML output, especially for UGC/AI content.
2. Expecting PDF to upload your file. It doesn't — it prints locally, which is the privacy win.
3. Losing code language hints in HTML→MD. The converter preserves them; verify with the code block extractor.
4. Overflowing code in PDF. Long lines clip on the page — wrap them before exporting.
5. Broken relative links after migration. Always re-check with the link checker.
Related articles
- Markdown to HTML guide — conversion internals
- Markdown to PDF guide — print CSS & page breaks
- HTML to Markdown guide — rescuing rich text
- Edit & preview tutorial — the writing step
- Safe Markdown → HTML for AI apps
Quick reference
| Need | Tool / endpoint |
|---|---|
| Markdown → HTML | tool · /render |
| Markdown → PDF | tool (browser print) |
| HTML → Markdown | tool · /convert/html-to-markdown |
| Clean up after | formatter + link checker |
Keep Markdown as your source and treat HTML/PDF as outputs you regenerate on demand. With the convert tools (and the API for scale), you write once and publish everywhere — and you can always pull rich text back into clean Markdown.
Written by Markdown Viewer Team. Found this useful? Try the editor →
Keep reading
Hand-picked articles related to this one.
Convert HTML, Word exports, and CMS content to clean GitHub Flavored Markdown — browser tools, CLI, API pipelines, and cleanup workflows for migrations and AI output.
Convert Markdown to clean HTML for CMS, email, and static sites — browser tools, CLI, JavaScript libraries, static generators, and API pipelines compared with code examples.
A tutorial on bridging Markdown to and from other markup formats — Atlassian Confluence/Jira wiki markup, forum BBCode, AsciiDoc, and reStructuredText — with migration tips and gotchas.