An MkDocs plugin to export content pages as PDF files
The pdf-export plugin will export all markdown pages in your MkDocs repository as PDF files using WeasyPrint. The exported documents support many advanced features missing in most other PDF exports, such as a PDF Index and support for CSS paged media module.
- This package requires MkDocs version 1.0 or higher (0.17 works as well)
3.43.5 or higher
- WeasyPrint depends on cairo, Pango and GDK-PixBuf which need to be installed separately. Please follow the installation instructions for your platform carefully:
- Explicit support for your mkdocs theme is probably required. As of now, the only supported theme is mkdocs-material. A generic version will just generate the PDF files and put the download link into a
If you want to add a new theme, see adding support for new themes for more information.
Install the package with pip:
pip install mkdocs-pdf-export-plugin
Enable the plugin in your
Note: If you have no
pluginsentry in your config file yet, you’ll likely also want to add the
searchplugin. MkDocs enables it by default if there is no
pluginsentry set, but now you have to enable it explicitly.
More information about plugins in the MkDocs documentation.
When building your repository with
mkdocs build, you should now see the following message at the end of your build output:
Converting 17 files to PDF took 15.6s
site_dir you should now have a PDF file for every markdown page.
You may customize the plugin by passing options in
Setting this to
true will show all WeasyPrint debug messages during the build. Default is
This option allows you to use a different CSS media type (or a custom one like
pdf-export) for the PDF export. Default is
Setting this option will enable the build only if there is an environment variable set to 1. This is useful to disable building the PDF files during development, since it can take a long time to export all files. Default is not set.
Setting this to
true will combine all pages into a single PDF file. All download links will point to this file. Default is
This option allows you to use a different destination for the combined PDF file. Has no effect when
combined is set to
false. Default is
This option allows you to specify a custom theme handler module. This path must be relative to your project root (See example below). Default is not set.
Adjusting the output
The resulting PDF can be customized easily by adding a custom stylesheet such as the following:
size: a4 portrait;
margin: 25mm 10mm 25mm 10mm;
font-family: "Roboto","Helvetica Neue",Helvetica,Arial,sans-serif;
content: '© 2018 My Company';
content: 'Page ' counter(page);
For this to take effect, use the
extra_css directive in mkdocs.yml, as described in the MkDocs user guide.
Adding support for new themes
If you use a mkdocs theme which is currently not supported, check out the
themes/material.py file and adjust it according to your requirements. You will have to implement two methods to support a theme:
get_stylesheetshould return a CSS which gets applied to fix issues with weasyprint
modify_htmlshould add a link to the PDF download before writing it to disk
If there is no explicit support for your theme, the generic version will just add a
<link> tag in the head pointing to the generated PDF.
From reporting a bug to submitting a pull request: every contribution is appreciated and welcome. Report bugs, ask questions and request features using Github issues. If you want to contribute to the code of this project, please read the Contribution Guidelines.
Special thanks go to Stephan Hauser for the original development of this plugin.