# domwalters/mkdocs-to-pdf architecture

> Architecture diagram of the GitHub repository domwalters/mkdocs-to-pdf, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/domwalters/mkdocs-to-pdf
- Repository: https://github.com/domwalters/mkdocs-to-pdf

## Overview

##### Project Type & Purpose
- This is an open-source MkDocs plugin (Python) whose sole purpose is to hook into the MkDocs build lifecycle, convert the rendered HTML documentation into a paginated PDF (with cover, TOC, styles, etc.), and emit that PDF artifact.
- It provides multiple “drivers” (Headless Chrome, RelaxedJS) and uses Jinja2 templates, SCSS styles, and link-transform preprocessors to produce high-quality PDF outputs.

A. MkDocs Core (external)
- The standard MkDocs build engine that loads plugins and themes.
B. mkdocs-to-pdf Plugin
- plugin.py: registers event hooks with MkDocs.
- options.py: reads plugin configuration.
C. Preprocessor Layer
- preprocessor/links: transforms internal/external links for PDF.
D. Generator Layer
- generator.py: orchestrates PDF creation.
- cover.py: generates optional cover page.
- toc.py: builds PDF table of contents.
E. Driver Layer (strategy pattern)
- headless_chrome.py
- relaxedjs.py
- event_hook.py
(Each implements a common interface to render HTML → PDF.)
F. Template & Style Engine
- templates/: Jinja2 templates (cover.html, back_cover, filters).
- styles/: SCSS partials for fonts, heading, paging, TOC.
- themes/: CSS/JS to polyfill Material theme features.
G. Utility Modules
- utils/: helpers for images, iframes, layout, emoji, soup traversal.
H. Output
- PDF file(s) emitted to MkDocs “site” directory.
I. Docker Support (optional build/runtime environments)
- docker/mkdocs-with-pdf: container definitions for Alpine, Debian, tiny.
J. CLI / Entry Point
- mkdocs plugin entry: invoked by “mkdocs build” or “mkdocs serve.”

1. User runs “mkdocs build” with the plugin enabled.
2. MkDocs Core loads mkdocs-to-pdf plugin (plugin.py).
3. During the page events (on_page_markdown → on_page_content),
the plugin’s preprocessor/links transforms links.
4. After MkDocs finishes rendering HTML, on_post_build triggers generator.generate().
5. generator:
a. Calls cover.py to render front cover via Jinja2 templates + SCSS compiled CSS.
b. Uses driver (headless_chrome or relaxedjs) to convert HTML pages + styles → PDF.
c. Uses toc.py to insert a PDF TOC based on headings.
6. Utility modules supply image sizing, layout grid, and media‐embedding heuristics to both templates and the driver engine.
7. Driver calls external binary/process (Chrome headless or Approx. PhantomJS) or Docker container if configured.
8. Final PDF file is placed under /site or a user-defined output directory.

##### Architectural Patterns & Principles
- Plugin/Event‐hook pattern (registering to MkDocs lifecycle hooks).
- Strategy pattern for interchangeable PDF drivers.
- Template‐driven code generation (Jinja2, SCSS).
- Separation of concerns:
- Preprocessing (link rewriting)
- Presentation (templates + styles)
- Rendering (drivers)
- Highly pluggable and extensible: new drivers or templates can be dropped into the respective directories.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External System
    MkDocsCore(("MkDocs Core")):::external

    %% Plugin Layer
    subgraph "mkdocs-to-pdf Plugin"
        plugin["plugin.py"]:::python
        options["options.py"]:::python
    end

    %% Preprocessor Layer
    subgraph "Preprocessor Layer"
        preprocessor["links/transform.py & util.py"]:::python
    end

    %% Generator Layer
    subgraph "Generator Layer"
        generator["generator.py"]:::python
        cover["cover.py"]:::python
        toc["toc.py"]:::python
    end

    %% Driver Layer
    subgraph "Driver Layer"
        eventHook["event_hook.py"]:::python
        headlessChrome["headless_chrome.py"]:::python
        relaxedJS["relaxedjs.py"]:::python
    end

    %% Template & Style Engine
    subgraph "Template & Style Engine"
        templates["templates/*.html.j2 & filters & template.py"]:::template
        styles["styles/*.scss"]:::template
        themes["themes/*"]:::template
    end

    %% Utility Modules
    subgraph "Utility Modules"
        utils["utils/*.py"]:::python
    end

    %% Docker Support
    docker["docker/mkdocs-with-pdf/"]:::external

    %% Output Artifact
    PDF(("PDF Artifact")):::artifact

    %% Data Flow
    MkDocsCore -->|"loads plugin"| plugin
    plugin -->|"reads config"| options
    plugin -->|"on_page_* hooks"| preprocessor
    plugin -->|"on_post_build"| generator

    preprocessor -->|"transforms links"| generator

    generator -->|"generate cover"| cover
    generator -->|"build TOC"| toc
    generator -->|"use drivers"| eventHook
    generator -->|"use drivers"| headlessChrome
    generator -->|"use drivers"| relaxedJS
    generator -->|"apply templates/styles"| templates
    generator -->|"apply templates/styles"| styles
    generator -->|"apply templates/styles"| themes

    utils --> generator
    utils --> templates

    headlessChrome -->|"runs headless Chrome"| PDF
    relaxedJS -->|"runs RelaxedJS"| PDF
    eventHook -->|"driver interface"| PDF

    docker -->|"optional runtime"| headlessChrome
    docker -->|"optional runtime"| relaxedJS

    %% Legend (as a subgraph)
    subgraph "Legend"
        A["Rectangle: Code Component"]:::legendBox
        B(["Cylinder: Artifact"]):::legendArtifact
        C(("Cloud: External System")):::legendCloud
    end

    %% Click Events
    click plugin "https://github.com/domwalters/mkdocs-to-pdf/blob/develop/src/mkdocs_to_pdf/plugin.py"
    click options "https://github.com/domwalters/mkdocs-to-pdf/blob/develop/src/mkdocs_to_pdf/options.py"
    click preprocessor "https://github.com/domwalters/mkdocs-to-pdf/blob/develop/src/mkdocs_to_pdf/preprocessor/links/transform.py"
    click preprocessor "https://github.com/domwalters/mkdocs-to-pdf/blob/develop/src/mkdocs_to_pdf/preprocessor/links/util.py"
    click generator "https://github.com/domwalters/mkdocs-to-pdf/blob/develop/src/mkdocs_to_pdf/generator.py"
    click cover "https://github.com/domwalters/mkdocs-to-pdf/blob/develop/src/mkdocs_to_pdf/cover.py"
    click toc "https://github.com/domwalters/mkdocs-to-pdf/blob/develop/src/mkdocs_to_pdf/toc.py"
    click eventHook "https://github.com/domwalters/mkdocs-to-pdf/blob/develop/src/mkdocs_to_pdf/drivers/event_hook.py"
    click headlessChrome "https://github.com/domwalters/mkdocs-to-pdf/blob/develop/src/mkdocs_to_pdf/drivers/headless_chrome.py"
    click relaxedJS "https://github.com/domwalters/mkdocs-to-pdf/blob/develop/src/mkdocs_to_pdf/drivers/relaxedjs.py"
    click templates "https://github.com/domwalters/mkdocs-to-pdf/blob/develop/src/mkdocs_to_pdf/templates/default_cover.html.j2"
    click templates "https://github.com/domwalters/mkdocs-to-pdf/blob/develop/src/mkdocs_to_pdf/templates/default_back_cover.html.j2"
    click templates "https://github.com/domwalters/mkdocs-to-pdf/tree/develop/src/mkdocs_to_pdf/templates/filters"
    click templates "https://github.com/domwalters/mkdocs-to-pdf/blob/develop/src/mkdocs_to_pdf/templates/template.py"
    click styles "https://github.com/domwalters/mkdocs-to-pdf/blob/develop/src/mkdocs_to_pdf/styles/_fonts.scss"
    click styles "https://github.com/domwalters/mkdocs-to-pdf/blob/develop/src/mkdocs_to_pdf/styles/_heading.scss"
    click styles "https://github.com/domwalters/mkdocs-to-pdf/blob/develop/src/mkdocs_to_pdf/styles/_paging.scss"
    click styles "https://github.com/domwalters/mkdocs-to-pdf/blob/develop/src/mkdocs_to_pdf/styles/_toc.scss"
    click styles "https://github.com/domwalters/mkdocs-to-pdf/blob/develop/src/mkdocs_to_pdf/styles/cover.scss"
    click styles "https://github.com/domwalters/mkdocs-to-pdf/blob/develop/src/mkdocs_to_pdf/styles/report-print.scss"
    click themes "https://github.com/domwalters/mkdocs-to-pdf/blob/develop/src/mkdocs_to_pdf/themes/material-polyfills.css"
    click themes "https://github.com/domwalters/mkdocs-to-pdf/blob/develop/src/mkdocs_to_pdf/themes/material-polyfills.js"
    click themes "https://github.com/domwalters/mkdocs-to-pdf/blob/develop/src/mkdocs_to_pdf/themes/material-polyfills.css.map"
    click themes "https://github.com/domwalters/mkdocs-to-pdf/blob/develop/src/mkdocs_to_pdf/themes/material-polyfills.js.map"
    click themes "https://github.com/domwalters/mkdocs-to-pdf/blob/develop/src/mkdocs_to_pdf/themes/material.py"
    click themes "https://github.com/domwalters/mkdocs-to-pdf/blob/develop/src/mkdocs_to_pdf/themes/material.scss"
    click themes "https://github.com/domwalters/mkdocs-to-pdf/blob/develop/src/mkdocs_to_pdf/themes/generic.py"
    click themes "https://github.com/domwalters/mkdocs-to-pdf/blob/develop/src/mkdocs_to_pdf/themes/mkdocs.py"
    click themes "https://github.com/domwalters/mkdocs-to-pdf/blob/develop/src/mkdocs_to_pdf/themes/mkdocs.scss"
    click utils "https://github.com/domwalters/mkdocs-to-pdf/blob/develop/src/mkdocs_to_pdf/utils/emoji_util.py"
    click utils "https://github.com/domwalters/mkdocs-to-pdf/blob/develop/src/mkdocs_to_pdf/utils/iframe_util.py"
    click utils "https://github.com/domwalters/mkdocs-to-pdf/blob/develop/src/mkdocs_to_pdf/utils/image_util.py"
    click utils "https://github.com/domwalters/mkdocs-to-pdf/blob/develop/src/mkdocs_to_pdf/utils/layout_util.py"
    click utils "https://github.com/domwalters/mkdocs-to-pdf/blob/develop/src/mkdocs_to_pdf/utils/section.py"
    click utils "https://github.com/domwalters/mkdocs-to-pdf/blob/develop/src/mkdocs_to_pdf/utils/soup_util.py"
    click utils "https://github.com/domwalters/mkdocs-to-pdf/blob/develop/src/mkdocs_to_pdf/utils/tabbed_set_util.py"
    click docker "https://github.com/domwalters/mkdocs-to-pdf/tree/develop/docker/mkdocs-with-pdf/"

    %% Styles
    classDef python fill:#e0ffe0,stroke:#006600,color:#003300
    classDef external fill:#e0f0ff,stroke:#004080,color:#003366
    classDef template fill:#fff4e0,stroke:#e68000,color:#b35000
    classDef artifact fill:#f0f0f0,stroke:#999999,color:#333333,shape:cylinder
    classDef legendBox fill:#ffffff,stroke:#000000,color:#000000
    classDef legendArtifact fill:#ffffff,stroke:#999999,color:#333333,shape:cylinder
    classDef legendCloud fill:#ffffff,stroke:#004080,color:#003366,shape:cloud
```
