Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
mkdocs-cm-page-to-pdf
Advanced tools
Generate a PDF file for each MkDocs page, with the possibility of selecting individual pages for export
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.
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
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 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 datetitle
: document titleurl
: document locationpageNumber
: current page numbertotalPages
: total pages in the documentfooterTemplate
(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. Disregarded when include
is set to True
.include
(bool) : Manually define files to export. Defaults to False
.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
---
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
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.
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;
}
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.
FAQs
Generate a PDF file for each MkDocs page, with the possibility of selecting individual pages for export
We found that mkdocs-cm-page-to-pdf demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.