Skip to content

About

Convert a Docusaurus project to PDF using LaTeX

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

Docusaurus2PDF

Convert Docusaurus documentation sites to high-quality PDF with full MDX support.

GitHub Release License: MIT Node.js

Features

  • Full MDX Support - Parses all Docusaurus components including DocCardList, Details, Admonitions, Tabs
  • LaTeX-based PDF Generation - Uses LaTeX (latexmk by default) for professional-quality PDF output with proper typography
  • Multilingual & i18n - Automatically reads translations from i18n/ directory and merges with fallbacks
  • Line Highlighting - Supports code block metadata {1,3-5} and comments (e.g. // highlight-next-line) using fvextra
  • Math Equations - Supports inline $...$ and block $$...$$ LaTeX math equations
  • Mermaid & PlantUML - Automatically generates high-quality diagrams with source code fallbacks if skipped or failed
  • Lettered Lists - Natively compiles a. b. c. lists using enumitem
  • Emoji Rendering - LuaLaTeX renders emojis using Twemoji Mozilla font
  • Section Filtering - Generate PDFs for specific sidebar sections only
  • Docker-based LaTeX - No local LaTeX installation required
  • Multiple Output Modes - Per-language, per-section, or single PDF

Quick Start

# Install globally from GitHub (no token required)
npm install -g github:BluPix/docusaurus2pdf

# Generate PDF from your Docusaurus site
docusaurus2pdf -i ./my-docusaurus-site -o ./pdf-output

Installation

Directly from GitHub (Recommended)

You can install the package directly from this repository. No GitHub token is required, and the package will automatically build during installation.

npm install -g github:BluPix/docusaurus2pdf

From GitHub Packages

This package is also published to the GitHub Packages registry. Before installing, configure your npm client to use the GitHub registry for the @blupix scope.

Create or edit your ~/.npmrc file (or a local .npmrc in your project) and add:

@blupix:registry=https://npm.pkg.github.com/
//npm.pkg.github.com/:_authToken=YOUR_GITHUB_PERSONAL_ACCESS_TOKEN

(Ensure your GitHub token has the read:packages permission.)

Then, install the package:

npm install -g @blupix/docusaurus2pdf

From source

git clone https://github.com/your-repo/docusaurus2pdf.git
cd docusaurus2pdf
npm install
npm run build

CLI Options

Option Alias Description Default
--input <path> -i Input Docusaurus site directory (required)
--output <path> -o Output directory for PDFs ./pdf-export
--engine <engine> -e LaTeX engine (lualatex, xelatex, pdflatex, tectonic) lualatex
--single -s Generate single PDF with all content false
--per-language -l Generate separate PDF per language true
--tex-only - Generate .tex files only, skip PDF compilation false
--compile-only - Compile existing .tex files to PDF false
--no-docker - Use local TeX installation instead of Docker false
--sections <names> - Comma-separated section names to generate (all)
--strip-numbering - Strip manual numbering from headings (e.g., "3. Title" → "Title") false
--convert-emoji - Convert emoji to text (e.g., "📷" → "[Camera]") for PDF output false
--suppress-caption-numbers - Suppress automatic figure numbering in captions false

Usage Examples

Basic Usage

# Generate PDFs per language (default behavior)
npx @blupix/docusaurus2pdf -i ./docs -o ./pdf-output

# Generate single PDF with all content
npx @blupix/docusaurus2pdf -i ./docs -o ./pdf-output --single

# Use specific LaTeX engine
npx @blupix/docusaurus2pdf -i ./docs -o ./pdf-output --engine xelatex

Diagram Support

The tool automatically converts PlantUML and Mermaid diagrams to SVG format for high-quality PDF output:

  • Mermaid: Server-side rendered using jsdom (no browser needed)
  • PlantUML: Local generation via Java JAR (if available) or TeaVM PlantUML.js (pure JavaScript fallback)

Note: All diagrams are converted to SVG format. For LaTeX compilation, use lualatex or xelatex (recommended) as they have built-in SVG support via the svg package. The pdflatex engine is not supported for documents containing diagrams.

Both diagram types use LaTeX's \includesvg command and maintain vector quality in the output PDF. No additional configuration needed - diagrams are automatically detected and converted.

Section Filtering

Generate PDFs only for specific sidebar sections:

# Single section
npx @blupix/docusaurus2pdf -i ./docs -o ./pdf-output --sections "tutorials"

# Multiple sections
npx @blupix/docusaurus2pdf -i ./docs -o ./pdf-output --sections "tutorials,guides,api"

Available section names are derived from your Docusaurus sidebar configuration (autogenerated directories or explicit categories).

TeX Output

# Generate only .tex files (for custom processing)
npx @blupix/docusaurus2pdf -i ./docs -o ./pdf-output --tex-only

# Compile existing .tex files to PDF
npx @blupix/docusaurus2pdf -i ./docs -o ./pdf-output --compile-only

Docusaurus Plugin

For automatic PDF generation during your Docusaurus build, use the plugin:

// docusaurus.config.js
module.exports = {
  // ... your config
  plugins: [
    [
      '@blupix/docusaurus2pdf/plugin',
      {
        outputDir: './pdf-output',
        single: false,
        sections: ['tutorials', 'guides'],
      },
    ],
  ],
};

Plugin Options

Option Type Description Default
outputDir string Output directory for PDFs ./pdf-export
single boolean Generate single PDF false
sections string[] Sections to include all
texOnly boolean Generate only .tex files false
stripNumbering boolean Strip manual numbering from headings false
suppressCaptionNumbers boolean Suppress figure numbering in captions false
onSuccess function Callback after generation -

MDX Component Support

Component LaTeX Output
DocCardList \begin{itemize} ... \end{itemize}
DocCard Bold text with optional description
Details tcolorbox with title
Admonitions (:::tip, etc.) Colored tcolorbox with custom icons
Tabs (<Tabs><TabItem>) Styled tcolorbox card per tab
Math Equations ($...$, $$...$$) Inline math or display math blocks
Mermaid / PlantUML PDF/EPS vector graphic (or source code fallback if skipped/failed)
Code Blocks (title, showLineNumbers, highlights) Verbatim with highlightlines (via fvextra) or lstlisting (via listings)
Emojis (LuaLaTeX) \emoji{name} with Twemoji font

Requirements

  • Node.js 18+
  • LaTeX with lualatex or xelatex (for PDF compilation with SVG support)
  • Optional: Java + PlantUML (for better PlantUML diagram quality)

LaTeX Setup

For PDF compilation with SVG diagram support, install a modern LaTeX distribution:

macOS:

brew install --cask mactex

Ubuntu/Debian:

sudo apt-get install texlive-full

Diagram Generation

Mermaid diagrams work out of the box (server-side rendering via jsdom).

PlantUML diagrams have two options:

  1. Recommended: Install Java and PlantUML for best quality:
    # macOS
    brew install plantuml
    
    # Ubuntu/Debian
    sudo apt-get install plantuml
  2. Fallback: Uses bundled TeaVM PlantUML.js (pure JavaScript, no Java needed, ~8MB)

Programmatic API

import { Renderer, SiteLoader } from '@blupix/docusaurus2pdf';

const siteLoader = new SiteLoader();
const site = await siteLoader.load('./my-docusaurus-site');

const renderer = new Renderer({
  OutputDir: './pdf-output',
  Engine: 'lualatex',
  Sections: ['tutorials', 'guides'],
});

// Generate single PDF
await renderer.renderSingle(site);

// Or per language
await renderer.renderPerLanguage(site);

// Or per section
await renderer.renderPerSection(site);

Troubleshooting

No LaTeX installation found

If you see this error, either:

  1. Install Docker and ensure it's running
  2. Install a local LaTeX distribution and use --no-docker

PlantUML diagrams not appearing

Ensure:

  • PlantUML code blocks use triple backticks with plantuml language
  • PlantUML server is accessible (uses public PlantUML server by default)

PDF compilation fails

  • Check the .log file in the output directory for LaTeX errors
  • Try --tex-only to inspect the generated .tex file
  • Ensure enough memory is available for LaTeX compilation

Origin

This project was inspired by a school assignment at VŠPJ Jihlava (2007–2008), originally assigned by Tomáš Richta — a DokuWiki-to-LaTeX converter written in C. Years later, I used that original codebase as a starting point, feeding it into LLM-based tools. What began as a simple port evolved through Go and eventually into TypeScript, driven by the complexity of real-world Docusaurus projects. While this implementation shares no code with the original, the core idea carried through from the very beginning.

License

MIT

About

Convert a Docusaurus project to PDF using LaTeX

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages