Skip to main content
Back to Projects
Open Source
npm Package
TypeScript

@amitdevx/md2pdf

Production-grade open-source Markdown to PDF rendering engine for Node.js that converts Markdown into high-fidelity PDFs using Playwright, Chromium, and the Unified processing pipeline.

Version

npm version

Downloads

npm downloads

Build Status

Build Status

License

License: MIT

Runtime

Node.js >=18.0.0

Installation

# Install globally

npm install -g @amitdevx/md2pdf

md2pdf init

# Or use locally within a project

npm install @amitdevx/md2pdf

npx md2pdf init

Practical Use Cases

README.md ➔ README.pdf

import { convert } from "@amitdevx/md2pdf";

const result = await convert({input: "./README.md", output: "./README.pdf",});

Automated Release Notes

# GitHub Actions automated generation
md2pdf CHANGELOG.md --output release.pdf

Technical Documentation

md2pdf docs/architecture.md \--output build/architecture.pdf \--toc \--paper A4

Resume Generation

md2pdf resume.md \--output resume.pdf \--margin 15mm

Rendering Pipeline

ParseTransformStringifyRenderPrint01MarkdownRaw document input02Unified (Remark)Markdown AST parsing03AST RehypeHTML AST transforms04HTML + CSSString generation05PlaywrightHeadless Chromium06PDF OutputHigh-fidelity document

Key Features

  • Native KaTeX Offline Math (mhchem)
  • Mermaid & Architecture Diagrams
  • Obsidian callouts, wiki-links, tags, embeds, highlights
  • Batch processing with single Chromium instance
  • Config file support (md2pdf.config.ts/json/yaml)
  • md2pdf doctor & init diagnostic tooling
  • GFM tables, strikethrough, task lists, footnotes
  • Table of Contents with depth control
  • Custom headers/footers with dynamic page numbers
  • --json-errors & Structured error codes for CI
  • JavaScript & TypeScript API
  • Professional Typography & Syntax highlighting (Shiki)

CLI Usage

Quick and powerful commands to generate PDFs directly from your terminal.

# Generate a PDF from a single Markdown file:

md2pdf README.md

# Specify a custom output path and generate a Table of Contents:

md2pdf input.md --output custom.pdf --toc

# Convert with custom paper size and margins:

md2pdf input.md --paper Letter --margin 15mm

# Force a page break before every H1 heading:

md2pdf input.md --h1-new-page

# Batch mode:

md2pdf "docs/*.md" --output out_dir/

# With config file:

md2pdf input.md --config md2pdf.config.json

# Obsidian vault:

md2pdf note.md --vault-root ./vault --resolve-links

# Machine-readable errors (CI):

md2pdf input.md --json-errors

# Verbose output:

md2pdf input.md --verbose

Rendered Output

Side-by-side comparison showcasing typography, syntax highlighting, and layout accuracy.

document.md
# System Architecture

The **md2pdf** engine supports native mathematics & diagrams.

```mermaid
graph TD
    AST --> Playwright
```

## Equation

$$
E = mc^2
$$
document.pdf

System Architecture

The md2pdf engine supports native mathematics & diagrams.

AST
Playwright

Equation

E = mc²

Diagnostics & Setup

Built-in tools to ensure your rendering environment is perfectly configured.

# Initialize a new environment and download dependencies automatically:

md2pdf init

# Check your system health and Playwright pipeline status:

md2pdf doctor

# Print advanced internal variables and stack traces if an error occurs:

md2pdf input.md --debug

Note: Typography uses Inter and JetBrains Mono served from Google Fonts CDN. Internet access is required during conversion for correct typography. Offline environments will fall back to system fonts.

Library Usage

Embed the rendering engine directly in your Node.js applications.

import { convert } from '@amitdevx/md2pdf';


const result = await convert({

input: 'README.md',

output: 'README.pdf',

paper: 'A4',

margin: '20mm',

toc: true

});


console.log(`Render time: ${result.renderTimeMs}ms`);

OptionTypeDescription
mathbooleanEnable KaTeX/mhchem rendering
themestringCustom CSS theme path
mermaid.themestringdefault, dark, forest, neutral
pageBreaksbooleanForce page breaks on headers
metadataobjectPDF Author, Title, Keywords
header / footerstringCustom HTML templates

Release Timeline

v0.5.4 (Current)

5x performance boost via persistent Chromium daemon, offline bundled fonts, package size reduced 80%

v0.5.3

Flawless Mermaid v10.9.1 parity, AST regex preprocessing, output overwrite protection

v0.5.2

Mermaid syntax tolerance, unescaped quotes, repaired mindmaps

v0.5.1

JSON CLI output routing, circular embed strict depth, AST regex masking, SVG viewBox scaling

v0.5.0

Batch processing globs, config files (ts/json/yaml), programmatic defineConfig API

v0.4.2

5MB size limit validation, publish-gpr CI pipeline, unsupported flag handlers

v0.4.1

Native Obsidian highlight syntax, root user sandboxing detection, KaTeX parsing fixes

v0.4.0

Obsidian callouts, wiki-links, tags, 3-stage browser fallback, zero-script npm compliance

v0.3.0

Native KaTeX rendering, mhchem chemistry, base64 offline fonts

v0.2.4

Critical security patches (XSS/SSRF), V8 OOM memory DoS protection

v0.2.2

Strict theme validation & JSON error handling

v0.2.1

Mermaid dynamic sizing & batched rendering

v0.2.0

Mermaid pipelines & Doctor CLI diagnostics

v0.1.6

Strict input validation & CI stability

v0.1.5

Friendly CLI Error UX & Footnotes restyling

v0.1.4

Auto-installer for Chromium binaries

v0.1.3

Running headers, footers & auto-pagination

v0.1.2

TOC generation & PDF metadata injection

v0.1.0

Core Playwright AST rendering engine

v0.0.2

Refined npm workflows & CI/CD

v0.0.1

Initial public release

Engineering Focus

The project demonstrates building and maintaining a production-quality open-source developer tool with emphasis on:

API Design
CLI UX
Regression Testing
Rendering Accuracy
Type Safety
Documentation
OSS Maintenance
Semantic Versioning

© 2026 Amit Divekar. All rights reserved.