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 pyppeteer (chrome headless) and add a download button.

How to use

Install the package with pip:

pip install mkdocs-page-pdf

Enable the plugin in your mkdocs.yml:

plugins:
  # - ...
    - page-to-pdf # should be last

Options

To set different options use the following syntax.

plugins:
  # - ...
    - page-to-pdf :
        # Options here
  • disable (bool): Disable pdf rendering useful to quickly disable it without removing the plugin config. Defaults to False.
  • disableOnServe (bool): Disable pdf rendering when using mkdocs serve. Defaults to False.

The following options are directly induced from pyppeteer options :

  • scale (float): Scale of the webpage rendering, defaults to 1.0.
  • displayHeaderFooter (bool): Display header and footer. Defaults to False.
  • headerTemplate (str): HTML template for the print header. Should be valid HTML markup with 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. Should be valid HTML markup with the same classes as headerTemplate.
  • printBackground (bool): Print background graphics. Defaults toFalse.
  • landscape (bool): Paper orientation. Defaults to False.
  • pageRanges (string): Paper ranges to print, e.g., '1-5,8,11-13'. Defaults to empty string, which means all pages.
  • format (str): Paper format. Defaults to A4.
  • margin (dict): Paper margins.
    • top (str): Top margin, accepts values labeled with units, defaults to 20px.
    • right (str): Right margin, accepts values labeled with units, defaults to 20px.
    • bottom (str): Bottom margin, accepts values labeled with units, defaults to 20px.
    • left (str): Left margin, accepts values labeled with units, defaults to 20px.
  • pageLoadOptions (dict): Page load options (see this).
    • timeout (int): Maximum time in milliseconds, defaults to 30000.
    • waitUntil (str): When to consider navigation succeeded, defaults to load.
  • exclude (list) : List of glob pattern to exclude
  • downloadLink (str): html string to personalize pdf download link, defaults to (pdf svg icon)

Troubleshooting

Running in a docker container (ci/cd)

Depending on what image you are using you may encounter some issue running pyppeteer : Browser closed unexpectedly This is due to some missing shared librairies used by Chrome Headless.

Related issue : https://github.com/pyppeteer/pyppeteer/issues/194
See this article and this guide
Ready-to-use docker image : https://github.com/brospars/docker-mkdocs

Slow build on serve

You can use disable or disableOnServe options to disable the pdf rendering (entirely or on serve) but you can also use the mkdocs serve --dirtyreload flag to only rebuild modified files on the fly.

Blank page at the end

Due to a chrome bug a blank page can appear at the end of the PDF you can remove it by addin the following to you extra.css :

body {
    contain: strict;
}

Special thanks

This plugin was inspired by mkdocs-with-pdf and mkdocs-pdf-export-plugin but without using weasyprint and instead pyppeteer (chrome headless) to have a render closer to what you have in your chrome 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.2.0.tar.gz (5.9 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.2.0-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for mkdocs_page_pdf-0.2.0.tar.gz
Algorithm Hash digest
SHA256 9cf83c4b7f4b5af03805498befc34f972fafd40eac204ac9df7df5ea1d63f726
MD5 57ee9f51ff4c693774faf68ac2684c14
BLAKE2b-256 c1ca3008f7ab5036cd2acd0a0c1fdb5f942da38754f677f477e6aafba9b594ee

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mkdocs_page_pdf-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 eafb67d5990023cbd6580e86cee59cffe5919a3af567df8f01edfe67a721c19c
MD5 d720b714f284a1c7fd1ccb9f12076c4c
BLAKE2b-256 59fa81b839cb5a28c2c48b0dfb760fa05e0a6ce5254fc8413a8502ddaed9802f

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