Web Development

How do I export Markdown to HTML or PDF?

Learn multiple methods to convert Markdown files to HTML and PDF formats for sharing, publishing, and distribution — with a side-by-side comparison of Pandoc, VS Code, browser print, and online converters.

By Inventive HQ Team

Understanding Markdown Export

To export Markdown, run it through a converter that parses the Markdown syntax and re-renders it as HTML (for the web) or PDF (for sharing and print). The most common paths are Pandoc on the command line (pandoc input.md -o output.html or -o output.pdf), a one-click editor extension such as VS Code's Markdown PDF, a browser's "Save as PDF" print dialog, or an online converter for occasional use. HTML conversion needs no extra dependencies; PDF conversion with Pandoc additionally requires a LaTeX distribution unless you point it at a lighter engine like weasyprint or Typst.

That is the summary an AI overview would give you. What it can't give you is the part that actually decides your choice: which tool matches your styling needs, which ones silently fail without LaTeX installed, and which command flags changed in recent Pandoc versions. This guide walks through every route, with correct, current commands you can copy.

Fortunately, converting Markdown to HTML and PDF is straightforward with numerous tools available. The choice depends on your specific needs, technical comfort level, and desired output quality. Understanding your options helps you choose the best approach for your workflow.

The conversion process is fundamentally simple: parse Markdown syntax, interpret it according to Markdown specifications, and generate HTML or PDF output. Most tools add powerful features like CSS styling, template customization, and metadata handling.

Choosing a method at a glance

Before diving into commands, here is how the main export routes compare. Pick the row that matches how much control you need and how comfortable you are on the command line.

MethodBest forProsConsNeeds LaTeX?
Pandoc (CLI)Single documents, scripted/automated conversionMost powerful; HTML + PDF; templates, TOC, metadata, custom CSSCommand-line only; PDF path needs a PDF engine installedOnly for the default PDF engine (use --pdf-engine=weasyprint/typst to avoid it)
VS Code "Markdown PDF" extensionWriters already in VS Code who want one clickZero config; exports PDF/HTML/PNG; syntax highlighting; custom CSSEditor-bound; uses bundled Chromium (larger install)No
Browser Print → Save as PDFQuick PDF with no installWorks anywhere; honours print CSS; nothing to set upLimited control of page breaks/margins; manual stepNo
Static site generators (Hugo, Jekyll, Astro)A whole collection of interlinked Markdown pagesHandles conversion, navigation, theming, and deploy togetherOverkill for one file; build setup requiredNo
Online converters (Dillinger, markdowntopdf.com)One-off conversions, no toolingInstant; nothing to installPrivacy risk for sensitive docs; little styling controlNo
Node/Python libraries (markdown-it, Python-Markdown)Embedding conversion inside an appProgrammatic; integrates into pipelinesYou write the glue code and styling yourselfNo

Which should I use? For a one-off, use your browser's Print-to-PDF or an online tool. For repeatable, styled single documents, use Pandoc. For a whole website of Markdown pages, use a static site generator. For conversion inside your own software, use a library.

The Markdown export pipeline A left-to-right flow: a Markdown file is parsed into an HTML document, which is either served on the web or rendered by a print or LaTeX engine into a PDF. A pulse travels along the path. One pipeline, two destinations: parse once, render to HTML or PDF .md file Markdown
<rect x="204" y="96" width="120" height="56" rx="8" fill="#2813e8"/>
<text x="264" y="122" font-size="14" font-weight="bold" fill="#ffffff">Parser</text>
<text x="264" y="140" font-size="11" fill="#dbeafe">Pandoc / lib</text>

<rect x="384" y="96" width="120" height="56" rx="8" fill="#1e293b"/>
<text x="444" y="122" font-size="14" font-weight="bold" fill="#ffffff">HTML</text>
<text x="444" y="140" font-size="11" fill="#cbd5e1">document</text>

<rect x="564" y="40" width="132" height="52" rx="8" fill="#0ea5e9"/>
<text x="630" y="63" font-size="13" font-weight="bold" fill="#ffffff">Web / browser</text>
<text x="630" y="80" font-size="11" fill="#e0f2fe">served as-is</text>

<rect x="564" y="152" width="132" height="52" rx="8" fill="#f59e0b"/>
<text x="630" y="175" font-size="13" font-weight="bold" fill="#ffffff">PDF</text>
<text x="630" y="192" font-size="11" fill="#fef3c7">print / LaTeX engine</text>

Converting Markdown to HTML

HTML is the native format of the web. Converting Markdown to HTML makes your content viewable in browsers and publishable on websites. Several approaches exist, from simple online tools to command-line utilities to programmatic libraries.

Online Markdown to HTML Converters

The simplest approach is using online conversion tools. No installation or technical knowledge required—paste Markdown, download HTML:

  • pandoc.org/try: Excellent online interface for the popular Pandoc converter
  • markdowntohtml.com: Simple, focused converter
  • dillinger.io: Browser-based Markdown editor with export options
  • markdown-convert.com: Quick online converter

These tools typically generate basic HTML. For production use, you might want more control over styling and structure.

If you just want to see the rendered HTML for a snippet before committing to a full toolchain, preview it right here:

Loading interactive tool...

Command-Line Tools

For developers, command-line tools offer powerful conversion with full customization. Pandoc is the gold standard:

pandoc input.md -o output.html

This produces an HTML fragment (just the converted body). Add --standalone (or -s) to get a complete document with <head> and <body>. Pandoc supports extensive options:

# Generate a complete, styled HTML document
pandoc input.md --standalone --css style.css -o output.html

# Use a custom template
pandoc input.md --template=mytemplate.html -o output.html

# Include table of contents
pandoc input.md --toc -o output.html

Other command-line tools include:

  • markdown-cli: A simple Node.js-based converter
  • marked: JavaScript markdown parser with CLI
  • commonmark: Reference implementation with various output options
  • MultiMarkdown: Extended Markdown with additional features

Installation varies by tool. Pandoc is available for Windows, macOS, and Linux. Node.js tools install via npm.

Node.js Solutions

For JavaScript developers, Node.js libraries provide programmatic conversion:

const md = require('markdown-it');
const fs = require('fs');

const markdown = md();
const mdContent = fs.readFileSync('input.md', 'utf8');
const html = markdown.render(mdContent);

fs.writeFileSync('output.html', html);

The markdown-it library is popular and extensible:

const md = require('markdown-it')({
  html: true,
  linkify: true,
  typographer: true
});

const html = md.render('# Heading\n\nParagraph');

Showdown is another option with similar capabilities:

const showdown = require('showdown');
const converter = new showdown.Converter();
const html = converter.makeHtml('# Heading');
Advertisement

Python Solutions

Python developers can use libraries like Markdown or mistune:

import markdown

with open('input.md', 'r') as f:
    content = f.read()

html = markdown.markdown(content)

with open('output.html', 'w') as f:
    f.write(html)

Or with mistune (v3 API — the old mistune.Markdown() callable was replaced):

import mistune

# Simple one-off conversion
html = mistune.html('# Heading\n\nParagraph')

# Reusable, configurable parser
convert = mistune.create_markdown(plugins=['strikethrough', 'table'])
html = convert('# Heading\n\nParagraph')

Adding Styling to HTML Output

Generated HTML is often unstyled. Adding CSS improves presentation:

External CSS

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <!-- Markdown-generated HTML here -->
</body>
</html>

Create a CSS file styling standard HTML elements:

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto;
    line-height: 1.6;
    color: #333;
    max-width: 900px;
    margin: 0 auto;
    padding: 20px;
}

h1, h2, h3 {
    color: #2c3e50;
    margin-top: 24px;
}

code {
    background-color: #f4f4f4;
    padding: 2px 6px;
    border-radius: 3px;
    font-family: monospace;
}

pre {
    background-color: #f4f4f4;
    padding: 12px;
    border-radius: 4px;
    overflow-x: auto;
}

Inline CSS

Use Pandoc with CSS for single-file output:

pandoc input.md -s --css style.css --embed-resources -o output.html

The --embed-resources flag inlines CSS and images, creating a single portable file (combine it with -s/--standalone to also get a full <head>/<body> wrapper). This replaces the older --self-contained flag, which was deprecated in Pandoc 2.19 (2022) — --self-contained still works but is now just a synonym for --embed-resources --standalone and prints a deprecation warning.

CSS Frameworks

Use CSS frameworks for professional styling:

# Using GitHub-style CSS
pandoc input.md --css https://cdn.jsdelivr.net/npm/github-markdown-css/github-markdown.css -o output.html

Popular CSS frameworks for Markdown:

  • GitHub Markdown CSS: Matches GitHub's Markdown styling
  • Tufte CSS: Elegant, readable typography
  • Bootstrap: Professional styling with customization
  • Tailwind CSS: Utility-first styling

Converting Markdown to PDF

PDF is ideal for sharing, printing, and archiving. Several approaches convert Markdown to PDF:

Online Converters

Similar to HTML converters, online tools handle Markdown-to-PDF conversion:

  • markdowntopdf.com: Simple online converter
  • dillinger.io: Built-in PDF export
  • pandoc.org/try: Generate PDF via Pandoc online

These tools are quick but offer limited customization. Avoid them for anything confidential — you are uploading the document to a third party.

VS Code (no LaTeX required)

If you already write in VS Code, the fastest route is the Markdown PDF extension by yzane. Install it, open your .md file, then run the command palette (Cmd/Ctrl+Shift+P) and choose Markdown PDF: Export (pdf). It renders the file with a bundled headless Chromium — so no LaTeX — and can also export HTML, PNG, and JPEG. It preserves syntax highlighting and accepts custom CSS via the markdown-pdf.styles setting.

Browser Print → Save as PDF (nothing to install)

The zero-dependency method: open the Markdown in any live preview (GitHub, VS Code's built-in preview pane, or a browser Markdown extension), press Cmd/Ctrl+P, and set the print destination to Save as PDF. This uses the browser's own print engine, honours @media print CSS, and works on any machine. The trade-off is coarse control over page breaks and margins compared with Pandoc or a dedicated exporter.

Pandoc for PDF

Pandoc can generate PDF directly:

pandoc input.md -o output.pdf

Important gotcha: this command is shorthand for pandoc input.md -t latex --pdf-engine=pdflatex -o output.pdf, so it requires a LaTeX distribution (TeX Live, MiKTeX, or MacTeX). Pandoc does not silently fall back to another engine — without LaTeX it fails with an error like pdflatex not found. To skip LaTeX entirely, point Pandoc at a lighter engine that you have installed:

# HTML-based engine (no LaTeX) — requires WeasyPrint installed
pandoc input.md --pdf-engine=weasyprint -o output.pdf

# Typst engine (no LaTeX) — fast, modern
pandoc input.md --pdf-engine=typst -o output.pdf

# Chromium/webkit-based engine — requires wkhtmltopdf installed
pandoc input.md --pdf-engine=wkhtmltopdf -o output.pdf

With advanced options:

# Custom title, author, date
pandoc input.md --pdf-engine=pdflatex \
  --variable title="Document Title" \
  --variable author="Author Name" \
  --variable date="January 31, 2025" \
  -o output.pdf

# Using a template
pandoc input.md --template=mytemplate.latex -o output.pdf

# With table of contents
pandoc input.md --toc -o output.pdf

Markdown to HTML to PDF

Convert Markdown to HTML first, then HTML to PDF using tools like:

  • wkhtmltopdf: Excellent HTML-to-PDF converter
  • puppeteer: Headless Chrome for PDF generation
  • weasyprint: Python-based HTML-to-PDF

With Node.js and Puppeteer:

const puppeteer = require('puppeteer');
const md = require('markdown-it');
const fs = require('fs');

async function convertMdToPdf() {
  const markdown = md();
  const mdContent = fs.readFileSync('input.md', 'utf8');
  const html = markdown.render(mdContent);

  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.setContent(html);
  await page.pdf({path: 'output.pdf'});
  await browser.close();
}

convertMdToPdf();

With Python and WeasyPrint:

from weasyprint import HTML, CSS
import markdown

# Convert Markdown to HTML
with open('input.md', 'r') as f:
    content = f.read()
html_content = markdown.markdown(content)

# Convert HTML to PDF
HTML(string=html_content).write_pdf('output.pdf')

Dedicated Markdown-to-PDF Tools

Some tools specialize in Markdown to PDF conversion:

  • Marp: Create presentations from Markdown
  • reveal.md: Web-based presentations from Markdown
  • markdown-pdf: npm package for Markdown-to-PDF
  • grip: GitHub Flavored Markdown previewer with PDF export

Using markdown-pdf:

npm install -g markdown-pdf
markdown-pdf input.md

Batch Conversion

Converting multiple files efficiently:

Bash Script for Multiple Files

#!/bin/bash

for file in *.md; do
  base="${file%.md}"
  pandoc "$file" -o "$base.html"
  pandoc "$file" -o "$base.pdf"
done

This converts all Markdown files to both HTML and PDF.

Node.js Batch Conversion

const fs = require('fs');
const path = require('path');
const md = require('markdown-it');

const markdown = md();
const dir = './markdown-files';

fs.readdirSync(dir).forEach(file => {
  if (file.endsWith('.md')) {
    const mdContent = fs.readFileSync(path.join(dir, file), 'utf8');
    const html = markdown.render(mdContent);
    const outFile = path.join('./output', file.replace('.md', '.html'));
    fs.writeFileSync(outFile, html);
  }
});

Static Site Generators

For publishing collections of Markdown files, static site generators like Jekyll, Hugo, or Gatsby convert Markdown to websites automatically.

With Jekyll:

jekyll new my-site
cd my-site
jekyll serve

These tools handle conversion, styling, and deployment automatically.

Preserving Metadata

Markdown often includes frontmatter metadata:

---
title: Document Title
author: John Doe
date: 2025-01-31
---

# Document Content

Your content here...

Pandoc preserves frontmatter:

pandoc input.md -o output.html

The title and author become document properties.

Best Practices for Export

Always test generated output. Export to HTML or PDF and verify formatting looks correct.

Use consistent styling across documents. Create CSS files or templates for consistency.

Include metadata (title, author, date) for professional documents.

Test with actual content. Placeholder text might behave differently than final content.

Consider accessibility. Ensure generated HTML is accessible to screen readers.

Version control your source Markdown, not generated output. Regenerate when needed.

Troubleshooting Common Issues

Images not displaying: Ensure image paths are correct and images are accessible.

Styling missing: Verify CSS files are accessible and linked correctly.

Unicode characters broken: Ensure UTF-8 encoding throughout the process.

PDF generation fails: Install required dependencies (LaTeX for Pandoc) or use alternative tools.

Conclusion

Converting Markdown to HTML and PDF is straightforward with numerous excellent tools. Online converters offer simplicity for quick conversions. Command-line tools like Pandoc provide powerful customization for complex documents. Programmatic approaches via Node.js or Python libraries integrate conversion into automated workflows. Choose based on your needs: simple online tools for occasional conversions, Pandoc for powerful flexible conversion, or specialized tools for specific output formats. Properly styled HTML and professional PDF documents help you share Markdown content with broader audiences.

Frequently Asked Questions

How do I convert Markdown to PDF?

The most reliable command-line method is Pandoc: run pandoc input.md -o output.pdf. Because that command defaults to a LaTeX engine, you also need a TeX distribution installed (TeX Live, MiKTeX, or MacTeX). If you do not want to install LaTeX, pass a lighter engine — pandoc input.md --pdf-engine=weasyprint -o output.pdf or --pdf-engine=typst — or simply open the file in VS Code with the Markdown PDF extension, or preview it in a browser and use Print to PDF. There is no single "best" tool; pick by how much control and styling you need.

Does Pandoc need LaTeX to make a PDF?

By default, yes. Running pandoc input.md -o output.pdf is equivalent to pandoc input.md -t latex --pdf-engine=pdflatex -o output.pdf, so without a TeX distribution the command fails with an error like "pdflatex not found". Pandoc does not silently fall back to another engine. To avoid LaTeX entirely, explicitly choose an HTML-based or Typst engine with --pdf-engine=weasyprint, --pdf-engine=wkhtmltopdf, or --pdf-engine=typst, each of which must be installed separately.

What is the easiest way to convert Markdown to HTML?

For a one-off, paste your Markdown into an online converter or an editor like Dillinger and download the HTML. For repeatable results, use Pandoc: pandoc input.md -o output.html produces an HTML fragment, and adding --standalone (or -s) wraps it in a full document with <head> and <body>. Developers embedding conversion into an app usually reach for a library such as markdown-it (JavaScript) or the Python-Markdown package instead.

How do I convert Markdown to PDF in VS Code?

Install the "Markdown PDF" extension by yzane, open your .md file, then run the command palette (Cmd/Ctrl+Shift+P) and choose "Markdown PDF: Export (pdf)". The extension renders the file with a headless Chromium and can also export HTML, PNG, and JPEG. It respects syntax highlighting and can apply custom CSS, which makes it the fastest zero-config route for people who already live in VS Code.

Can I export Markdown to PDF without installing anything?

Yes. Open the Markdown in any editor or viewer that renders a live preview — GitHub, VS Code's built-in preview, or a browser extension — then use the browser's Print dialog and choose "Save as PDF" as the destination. This uses the browser's own print engine, needs no LaTeX or command-line tools, and honours print CSS. The trade-off is less control over page breaks and margins than a dedicated tool gives you.

How do I add CSS styling to Markdown HTML output?

Pass a stylesheet to Pandoc and produce a standalone document: pandoc input.md -s --css style.css -o output.html links the CSS, while pandoc input.md -s --embed-resources --css style.css -o output.html inlines it so the HTML is a single portable file. The older --self-contained flag does the same as --embed-resources --standalone but was deprecated in Pandoc 2.19 (2022). Popular ready-made stylesheets include GitHub Markdown CSS and Tufte CSS.

How can I batch convert many Markdown files at once?

Loop over the files in a shell: for f in *.md; do pandoc "$f" -o "${f%.md}.html"; done converts every Markdown file in a folder to HTML (swap the extension for .pdf to produce PDFs). For a whole site of interlinked pages, a static site generator such as Hugo, Jekyll, or Astro is a better fit — it handles conversion, navigation, and templating together.

Which is better for Markdown export: Pandoc or a static site generator?

Use Pandoc for individual documents — reports, articles, or single pages you want as standalone HTML or PDF. Use a static site generator (Hugo, Jekyll, Astro, Gatsby) when you have a collection of Markdown files that should become an interlinked website with shared navigation, themes, and a build/deploy pipeline. Pandoc is the surgical single-file tool; SSGs are the publishing platform.

markdownhtmlpdfexportcontent-creation