markdown2pdf converts Markdown to PDF using a lexical analyzer and PDF rendering engine. The library tokenizes Markdown into semantic elements, applies styling rules from TOML configuration, and generates styled PDF output.
Both binary and library are provided. The binary offers CLI conversion from files, URLs, or strings. The library enables programmatic PDF generation with full control over styling and fonts. Configuration can be loaded at runtime or embedded at compile time for containerized deployments.
Built in Rust for performance and memory safety. Handles standard Markdown syntax including headings, lists, code blocks, links, tables, and images. Supports multiple input sources and outputs to files or bytes for in-memory processing.
Install binary
Homebrew
Cargo
Install the binary globally using cargo:
For the latest git version:
Prebuilt binaries
Prebuilt versions are available in our GitHub releases:
| File | Platform | Checksum |
|---|---|---|
| markdown2pdf-aarch64-apple-darwin.tar.xz | Apple Silicon macOS | checksum |
| markdown2pdf-x86_64-apple-darwin.tar.xz | Intel macOS | checksum |
| markdown2pdf-x86_64-pc-windows-msvc.zip | x64 Windows | checksum |
| markdown2pdf-aarch64-unknown-linux-gnu.tar.xz | ARM64 Linux | checksum |
| markdown2pdf-x86_64-unknown-linux-gnu.tar.xz | x64 Linux | checksum |
Install as library
Add to your project:
Or add to your Cargo.toml:
= "0.2.0"
Feature Flags
The library provides optional feature flags to control dependencies:
- Default (no features): Core PDF generation from files and strings. No network dependencies.
fetch: Enables URL fetching support (requires one of the TLS features below).native-tls: Enables URL fetching with native TLS/OpenSSL (recommended for most users).rustls-tls: Enables URL fetching with pure-Rust TLS implementation (useful for static linking or avoiding OpenSSL).
# Minimal installation (no network dependencies)
= "0.2.0"
# With URL fetching support (native TLS)
= { = "0.2.0", = ["native-tls"] }
# With URL fetching support (rustls)
= { = "0.2.0", = ["rustls-tls"] }
Note: Binary installations via cargo or prebuilt downloads do not include URL fetching by default. To build the binary with URL support:
# Install with URL fetching support
# Or build from source
Usage
The tool accepts file paths (-p), string content (-s), or URLs (-u) as input. Output path is specified with -o. Input precedence: path > url > string. Defaults to 'output.pdf'.
Convert a Markdown file:
Convert string content:
Convert from URL (requires native-tls or rustls-tls feature):
Use --verbose for detailed output, --quiet for CI/CD pipelines, or --dry-run to validate syntax without generating PDF.
Fonts
The font system supports three modes:
- Built-in fonts: Helvetica, Times, Courier (fastest, no file I/O)
- System fonts: Searches standard OS font directories
- File paths: Load directly from a TTF/OTF file
# Use built-in font (fastest)
# Use system font
# Use specific font file
Font subsetting is enabled by default, reducing PDF size by embedding only the glyphs used in the document. A Unicode document with Arial Unicode MS produces ~45KB instead of 23MB.
Performance is ~20ms for standard documents with built-in fonts.
Library Usage
Two main functions: parse_into_file() saves PDF to disk, parse_into_bytes() returns bytes for web services. Both parse Markdown, apply styling, and render output.
Configuration uses ConfigSource: Default for built-in styling, File("path") for runtime loading, or Embedded(content) for compile-time embedding.
use ;
// Default styling
parse_into_file?;
// File-based configuration
parse_into_file?;
// Embedded configuration
const CONFIG: &str = include_str!;
parse_into_file?;
Font configuration uses FontConfig for programmatic control:
use ;
let font_config = new
.with_default_font
.with_code_font;
parse_into_file?;
Logging
The library uses the log crate. No output by default. Enable with any log-compatible backend (e.g., env_logger) and set RUST_LOG=markdown2pdf=info or debug for diagnostics.
Configuration
TOML configuration customizes fonts, colors, spacing, and visual properties. Three loading methods: default styles, runtime files, or compile-time embedding.
For binary usage, create a config file at ~/markdown2pdfrc.toml and copy the example configuration from markdown2pdfrc.example.toml. For library usage with embedded config, create your configuration file and embed it using include_str!().
Contributing
For information regarding contributions, please refer to CONTRIBUTING.md file.