Skip to main content

Mkdocs Page to PDF

An MkDocs plugin to generate a PDF file for each MkDocs page using Playwright (Chromium headless) and add a download button.

How to Use

1. Install the Package

Install the plugin using pip:

pip install mkdocs-page-pdf

Then, install Playwright browsers:

playwright install

2. Enable the Plugin

Add the plugin to your mkdocs.yml:

plugins:
  # - ...
  - page-to-pdf  # Should be last to ensure all content is rendered

Options

To customize the plugin, use the following options in your mkdocs.yml:

plugins:
  # - ...
  - page-to-pdf:
      # Options here

General Options

  • disable (bool): Disable PDF rendering. Useful for quickly disabling the plugin without removing its configuration. Default: False.
  • disableOnServe (bool): Disable PDF rendering when using mkdocs serve. Default: False.
  • concurrency (int): Maximum number of pages rendered to PDF at the same time. Higher values speed up the build but use more memory (each page is a Chromium tab). Use 1 for sequential rendering. Default: min(4, number of CPUs).

PDF Rendering Options

The following options are based on Playwright's PDF generation capabilities:

  • scale (float): Scale of the webpage rendering. Default: 1.0.
  • displayHeaderFooter (bool): Display header and footer. Default: False.
  • headerTemplate (str): HTML template for the print header. Use the following classes:
    • date: Formatted print date
    • title: Document title
    • url: Document location
    • pageNumber: Current page number
    • totalPages: Total pages in the document
  • footerTemplate (str): HTML template for the print footer. Same classes as headerTemplate.
  • printBackground (bool): Print background graphics. Default: False.
  • landscape (bool): Use landscape orientation. Default: False.
  • pageRanges (str): Page ranges to print (e.g., '1-5, 8, 11-13'). Default: "" (all pages).
  • format (str): Paper format. Default: "A4".
  • margin (dict): Paper margins. Default:
    margin:
      top: "20px"
      right: "20px"
      bottom: "20px"
      left: "20px"
    
  • pageLoadOptions (dict): Page load options (see Playwright's page.goto).
    • timeout (int): Maximum navigation time in milliseconds. Default: 30000.
    • waitUntil (str): When to consider navigation successful. Default: "load".
  • exclude (list): List of glob patterns to exclude from PDF generation.
  • downloadLink (str): Custom HTML for the PDF download link. Default: SVG icon for PDF.

Troubleshooting

Running in a Docker Container (CI/CD)

If you encounter issues running Playwright in Docker (e.g., Browser closed unexpectedly), you may need to install additional system dependencies for Chromium.

Use a ready-to-use Docker image with all dependencies pre-installed:

Manual Setup

If you prefer to set up your own image, refer to:

Out of Memory in CI

If the build crashes or gets killed on a CI runner with limited memory, lower the concurrency option. You can use MkDocs' !ENV tag to set it only in CI:

plugins:
  - page-to-pdf:
      concurrency: !ENV [PDF_CONCURRENCY, 4]

Then set PDF_CONCURRENCY=1 (or 2) in your CI environment.

Slow Build on Serve

To speed up mkdocs serve, use the --dirtyreload flag to rebuild only modified files:

mkdocs serve --dirtyreload

Alternatively, use the disableOnServe option to disable PDF generation during development.

Blank Page at the End

Due to a Chromium bug, a blank page may appear at the end of the PDF. To fix this, add the following CSS to your extra.css:

body {
    contain: strict;
}

Special Thanks

This plugin was inspired by:

Unlike these plugins, mkdocs-page-pdf uses Playwright (Chromium headless) for rendering, ensuring the PDF output closely matches what you see in your browser.

Metadata

Release files for mkdocs-page-pdf 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mkdocs-page-pdf 0.5.0
File Size Uploaded Provenance Actions
mkdocs_page_pdf-0.5.0.tar.gz 6.6 kB Without provenance Details for mkdocs_page_pdf-0.5.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mkdocs-page-pdf 0.5.0
File Interpreter ABI Platform Actions
mkdocs_page_pdf-0.5.0-py3-none-any.whl Python 3 none any Details for mkdocs_page_pdf-0.5.0-py3-none-any.whl

Total release size: 13.7 kB

Release files / mkdocs_page_pdf-0.5.0.tar.gz

Download URL mkdocs_page_pdf-0.5.0.tar.gz
Size 6.6 kB
Tags Source
SHA-256 checksum
How to use checksums
c9cf97a891a7c449a8544128ff5aafafee0e17b1acdfd9e47c81fafc72462b82
BLAKE2b-256 checksum
How to use checksums
bfb85b806ad8861bf623f6105fec68b575222f9d3162882868eeb7392dbb2f4d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.10

Release files / mkdocs_page_pdf-0.5.0-py3-none-any.whl

Download URL mkdocs_page_pdf-0.5.0-py3-none-any.whl
Size 7.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c4ce1341ba31d7c0d67ade707f2d75437090a278aff269115c22e2f2fa5d6fe6
BLAKE2b-256 checksum
How to use checksums
389e727c4dced30433f284dfb0f8fe7f8205646b8af5bdb885b975b27acba519
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.10

Security for mkdocs-page-pdf 0.5.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page