Skip to main content

Generate a PDF file for each MkDocs page

Project description

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.

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.

Recommended Docker Image

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:

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mkdocs_page_pdf-0.4.0.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mkdocs_page_pdf-0.4.0-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file mkdocs_page_pdf-0.4.0.tar.gz.

File metadata

  • Download URL: mkdocs_page_pdf-0.4.0.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.10

File hashes

Hashes for mkdocs_page_pdf-0.4.0.tar.gz
Algorithm Hash digest
SHA256 279a2a766ac6956c26e2d97f21be09348a29bcfed826f79164fe822998b52653
MD5 cd289a370d2da44948beba3862a3a275
BLAKE2b-256 6603bb706f3bbffd72bcae2cf1c5bcd75d53eb31fea14bad926ed8a8fc5f3978

See more details on using hashes here.

File details

Details for the file mkdocs_page_pdf-0.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for mkdocs_page_pdf-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6182b32b166760add4eacdcef78b3b14bf5a1722606e14147bd68a71bb4b9829
MD5 507a97277b7f0cb43a727c59a4c11af2
BLAKE2b-256 2a7891256c100571c8bcb778e758bac42f8ffe3030914394cfaef99152bd6abd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page