Skip to content
All posts
4 min read
markdown
tutorial
convert
html
pdf

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

DirectionToolTypical job
Markdown → HTMLMarkdown to HTMLPaste into a CMS, email, web page
Markdown → PDFMarkdown to PDFReports, resumes, handouts
HTML → MarkdownHTML to MarkdownRescue 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

  1. Draft in the editor.
  2. Convert with Markdown to HTML.
  3. 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:

  1. Copy the post's HTML → HTML to Markdown.
  2. Clean with the formatter.
  3. Fix relative links flagged by the link checker.
  4. Commit the .md to your repo (docs-as-code).
  5. 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:

DirectionEndpoint
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.

Quick reference

NeedTool / endpoint
Markdown → HTMLtool · /render
Markdown → PDFtool (browser print)
HTML → Markdowntool · /convert/html-to-markdown
Clean up afterformatter + 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.