mkdocs-pdf-export-plugin

repository·master·Indexed 43 days ago

https://github.com/zhaoterryy/mkdocs-pdf-export-plugin

An MkDocs plugin that exports markdown content pages into PDF files using the WeasyPrint engine. It supports advanced CSS paged media features for custom headers, footers, and indices. Features include the ability to combine all pages into a single PDF, conditional execution via environment variables, and custom theme handlers for non-material themes.

Tokens
1.8K
Snippets
8
Records
13
Agent score
51%

What's inside mkdocs-pdf-export-plugin

  1. Customize PDF output with CSS

    master

    The exported PDF supports the CSS paged media module. You can adjust margins, fonts, headers, and footers by adding a custom stylesheet to your MkDocs project.

    To apply these styles, use the extra_css directive in your mkdocs.yml.

    Example CSS for @page customization:

    @page {
        size: a4 portrait;
        margin: 25mm 10mm 25mm 10mm;
        counter-increment: page;
        font-family: "Roboto","Helvetica Neue",Helvetica,Arial,sans-serif;
        @top-left {
            content: '© 2018 My Company';
        }
        @top-center {
            content: string(chapter);
        }
        @top-right {
            content: 'Page ' counter(page);
        }
    }
  2. Install and enable the mkdocs-pdf-export-plugin

    master

    To use the plugin, install it via pip and then register it in your mkdocs.yml configuration file.

    Requirements:

    • MkDocs >= 1.0
    • Python >= 3.4
    • WeasyPrint >= 44

    Note on the search plugin: If you define a plugins section in your mkdocs.yml, MkDocs will no longer enable the default search plugin automatically. You should explicitly include search in your list if you want to keep search functionality enabled.

    pip install mkdocs-pdf-export-plugin
    plugins:
        - search
        - pdf-export
  3. Add support for new MkDocs themes

    master

    If you are using a theme that is not explicitly supported, you can implement a custom theme handler. This allows you to fix WeasyPrint rendering issues and control how the PDF download link is integrated into your site.

    1. Create a Python module (e.g., theme-handler.py) in your project root.
    2. Implement the following two methods:
      • get_stylesheet(): Returns a CSS string that is applied to fix specific rendering issues for WeasyPrint.
      • modify_html(): Logic to add a link to the PDF download within your theme's HTML.
    3. Register the handler in mkdocs.yml using the theme_handler_path option.

    Example Project Structure:

    project-root
    ├── theme-handler.py
    ├── docs
    ├── mkdocs.yml
    └── site
    plugins:
        - pdf-export:
            theme_handler_path: theme-handler.py
  4. Install and configure the mkdocs-pdf-export-plugin

    master

    The mkdocs-pdf-export-plugin exports your MkDocs markdown pages as PDF files using WeasyPrint.

    Prerequisites

    1. MkDocs: version 1.0 or higher (0.17 also works).
    2. Python: version 3.5 or higher.
    3. System Dependencies: WeasyPrint requires cairo, Pango, and GDK-PixBuf to be installed on your operating system. Follow the WeasyPrint installation guide for your specific platform (Linux, macOS, or Windows).
    4. Theme Support: Currently, mkdocs-material is explicitly supported. For other themes, a generic version will be used that adds a <link> tag for the PDF download.

    Installation

    Install the plugin via pip:

    pip install mkdocs-pdf-export-plugin

    Configuration

    Enable the plugin in your mkdocs.yml. Note that if you define a plugins section, you must explicitly include the search plugin if you want to keep MkDocs' default search functionality.

    plugins:
        - search
        - pdf-export
  5. Configure pdf-export plugin options

    master

    You can customize the behavior of the pdf-export plugin in your mkdocs.yml file using the following options:

    OptionTypeDefaultDescription
    verbosebooleanfalseIf true, shows all WeasyPrint debug messages during the build.
    media_typestringprintSets the CSS media type used for export (e.g., print or a custom type like pdf-export).
    enabled_if_envstringnot setIf set, the plugin only runs if this environment variable is set to 1. Useful for skipping slow PDF builds during local development.
    combinedbooleanfalseIf true, combines all pages into a single PDF file instead of individual files.
    combined_output_pathstringpdf/combined.pdfThe destination path for the combined PDF. Only used if combined is true.
    theme_handler_pathstringnot setPath to a custom theme handler module, relative to your project root.
    plugins:
        - pdf-export:
            verbose: true
            media_type: print
            enabled_if_env: ENABLE_PDF_EXPORT
            combined: true
            combined_output_path: my_docs.pdf
            theme_handler_path: theme-handler.py