Generate a PDF file for each MkDocs page, with the possibility of selecting individual pages for export
Project description
CM MkDocs Plugins - 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.
This was forked from the great work done by https://github.com/brospars/mkdocs-page-pdf and changed to allow individual PDF export instead of excluding paths from export, due to the need to speed up the PDF generation process for a vast number of files and an issue with the glob pattern used for the exclude parameter, since the plugin implementation of PurePath.match doesn't match recursively so we have to specify all possible paths to exclude, significantly slowing down the process. Parsing the FrontMatter YAML to search for a specific key:value speeds the process immensely.
How to use
Install the package with pip:
pip install mkdocs-cm-page-to-pdf
Enable the plugin in your mkdocs.yml:
plugins:
# - ...
- mkdocs-cm-page-to-pdf # should be last
Options
To set different options use the following syntax.
plugins:
# - ...
- mkdocs-cm-page-to-pdf:
# Options here
disable
(bool): Disable pdf rendering useful to quickly disable it without removing the plugin config. Defaults toFalse
.disableOnServe
(bool): Disable pdf rendering when usingmkdocs serve
. Defaults toFalse
.
The following options are directly induced from pyppeteer options :
scale
(float): Scale of the webpage rendering, defaults to1.0
.displayHeaderFooter
(bool): Display header and footer. Defaults toFalse
.headerTemplate
(str): HTML template for the print header. Should be valid HTML markup with following classes.date
: formatted print datetitle
: document titleurl
: document locationpageNumber
: current page numbertotalPages
: total pages in the document
footerTemplate
(str): HTML template for the print footer. Should be valid HTML markup with the same classes asheaderTemplate
.printBackground
(bool): Print background graphics. Defaults toFalse
.landscape
(bool): Paper orientation. Defaults toFalse
.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 toA4
.margin
(dict): Paper margins.top
(str): Top margin, accepts values labeled with units, defaults to20px
.right
(str): Right margin, accepts values labeled with units, defaults to20px
.bottom
(str): Bottom margin, accepts values labeled with units, defaults to20px
.left
(str): Left margin, accepts values labeled with units, defaults to20px
.
pageLoadOptions
(dict): Page load options (see this).timeout
(int): Maximum time in milliseconds, defaults to30000
.waitUntil
(str): When to consider navigation succeeded, defaults toload
.
exclude
(list) : List of glob pattern to exclude. Disregarded wheninclude
is set toTrue
.include
(bool) : Manually define files to export. Defaults toFalse
.
If using the include
option, insert a pdfexport: true
key in the FrontMatter YAML of any Markdown file you wish to export to PDF. Example:
---
pdfexport: true
---
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 libraries used by Chrome Headless.
Related issue: https://github.com/pyppeteer/pyppeteer/issues/194
See this article and this guide
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 adding the following to you extra.css :
body {
contain: strict;
}
Special thanks
The original version of this plugin (here: https://github.com/brospars/mkdocs-page-pdf) 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
Built Distribution
File details
Details for the file mkdocs_cm_page_to_pdf-0.1.5.tar.gz
.
File metadata
- Download URL: mkdocs_cm_page_to_pdf-0.1.5.tar.gz
- Upload date:
- Size: 6.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 22023d75c0d2eb88c68ee43a844d16d9b7471f953885e9738f94bce7bbc5d27e |
|
MD5 | c1f711417c09e3937e78c9e8a071dc7c |
|
BLAKE2b-256 | edb5aa8cccc4aa9283e3b6666775dfb5d93815099b4f17b0466bb88b377d1ee4 |
File details
Details for the file mkdocs_cm_page_to_pdf-0.1.5-py3-none-any.whl
.
File metadata
- Download URL: mkdocs_cm_page_to_pdf-0.1.5-py3-none-any.whl
- Upload date:
- Size: 9.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0ac98cc18f7cccb96f7167f0e19e2a0804a3e7b573bc0822b2567465a503475d |
|
MD5 | fb2aec0e7a9fd9bf8ff71cd6096fdf7b |
|
BLAKE2b-256 | 796ad44c7802f6bd96022a0f4637bf10d2f1b7dd85d1878937e5f20eee6c1c3f |