MkDocs plugin to convert SVG files to PNG images during build
Project description
mkdocs-svg-to-png
An MkDocs plugin to convert SVG files to PNG images.
This plugin finds SVG code blocks and image references and converts them to PNG images during the MkDocs build process. This is useful for formats that don't support SVG directly, like PDF, or for ensuring consistent image rendering across different environments.
Requirements
This plugin requires CairoSVG and its dependencies. Please ensure you have them installed:
pip install cairosvg
CairoSVG itself has some system-level dependencies (like cairo, pango, gdk-pixbuf). On Debian/Ubuntu, you can install them using:
sudo apt-get update
sudo apt-get install libcairo2-dev libffi-dev libgdk-pixbuf2.0-dev libpango1.0-dev
For other operating systems, please refer to the CairoSVG documentation.
Setup
Install the plugin using pip:
pip install mkdocs-svg-to-png
Activate the plugin in mkdocs.yml:
plugins:
- search
- svg-to-png
Note: If you have no
pluginsentry in your config file yet, you'll likely also want to add thesearchplugin. MkDocs enables it by default if there is nopluginsentry set, but now you have to enable it explicitly.
Options
You can customize the plugin's behavior in mkdocs.yml:
plugins:
- svg-to-png:
output_dir: "assets/images"
image_format: "png"
dpi: 300
quality: 95
background_color: "transparent"
cache_enabled: true
cache_dir: ".svg_cache"
preserve_original: false
error_on_fail: false
log_level: "INFO"
cleanup_generated_images: false
enabled_if_env: null
temp_dir: null
output_dir:- Defaults to
assets/images. - The directory where generated PNG images will be saved, relative to your
docsdirectory.
- Defaults to
image_format:- Defaults to
png. - The output format for the generated images. Currently, only
pngis supported.
- Defaults to
dpi:- Defaults to
300. - The Dots Per Inch (DPI) for rendering SVG to PNG. Higher values result in larger, higher-resolution images.
- Defaults to
quality:- Defaults to
95. - The quality of the output PNG image, a value between 0 (lowest) and 100 (highest). Only applicable for PNG output.
- Defaults to
background_color:- Defaults to
transparent. - The background color for the generated PNG images. Can be a color name (e.g.,
white), a hex code (e.g.,#FFFFFF), ortransparent.
- Defaults to
cache_enabled:- Defaults to
true. - If
true, generated images will be cached to avoid re-rendering on subsequent builds if the SVG content hasn't changed.
- Defaults to
cache_dir:- Defaults to
.svg_cache. - The directory where cached images are stored, relative to the project root.
- Defaults to
preserve_original:- Defaults to
false. - If
true, the original SVG code block or image reference will be preserved in the Markdown output, in addition to the generated PNG image.
- Defaults to
error_on_fail:- Defaults to
false. - If
true, the MkDocs build will fail if any SVG to PNG conversion encounters an error. Iffalse, errors will be logged, and the original SVG content will be kept.
- Defaults to
log_level:- Defaults to
INFO. - Sets the logging level for the plugin. Can be
DEBUG,INFO,WARNING, orERROR.
- Defaults to
cleanup_generated_images:- Defaults to
false. - If
true, generated PNG images will be removed from theoutput_dirafter the MkDocs build is complete. This is useful for CI/CD pipelines.
- Defaults to
enabled_if_env:- Defaults to
null. - If set to an environment variable name, the plugin will only be enabled if that environment variable is set to a non-empty value. Useful for conditional builds (e.g., only enable for PDF builds).
- Defaults to
temp_dir:- Defaults to
null. - Specifies a directory for temporary files created during the conversion process. If
null, the system's default temporary directory will be used.
- Defaults to
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mkdocs_svg_to_png-0.0.5.tar.gz.
File metadata
- Download URL: mkdocs_svg_to_png-0.0.5.tar.gz
- Upload date:
- Size: 966.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c5d0f7bd4c92b0469e6f2c175cf73f8afae438bfadd37c26ca1f2932fb63fb0d
|
|
| MD5 |
bf799e29ad1c6b4a62749144a17165a8
|
|
| BLAKE2b-256 |
80fec2e95018bf371c3f56417d44a77a697e414e5e4c1d6beb424537e220a156
|
File details
Details for the file mkdocs_svg_to_png-0.0.5-py3-none-any.whl.
File metadata
- Download URL: mkdocs_svg_to_png-0.0.5-py3-none-any.whl
- Upload date:
- Size: 24.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cff22a954ca8d0cca339414a4a7ac06a4be18a99d4aa1b5fadbb561ed22ce11e
|
|
| MD5 |
e905d419ecac3cba9559b43b5b23d439
|
|
| BLAKE2b-256 |
3941425c563785a7195ba08ffc4f72217d0c9b5add029bae90f9b00d38fc252f
|