{"id":9929,"library":"md2pdf","title":"md2pdf: Markdown to PDF Converter","description":"md2pdf is a Python library and CLI tool for converting Markdown files to PDF, offering features like custom styling, Jinja templating, and support for various Markdown extensions. It is currently at version 3.1.1 and is actively maintained, receiving regular updates for new features, bug fixes, and security enhancements.","status":"active","version":"3.1.1","language":"en","source_language":"en","source_url":"https://github.com/jmaupetit/md2pdf","tags":["markdown","pdf","conversion","cli","documentation"],"install":[{"cmd":"pip install md2pdf","lang":"bash","label":"Basic installation"},{"cmd":"pip install \"md2pdf[cli]\"","lang":"bash","label":"With CLI tools"}],"dependencies":[{"reason":"Provides command-line interface functionality (e.g., Typer, watchdog).","package":"cli","optional":true},{"reason":"Provides extra dependencies for LaTeX specific processing (less common).","package":"latex","optional":true}],"imports":[{"symbol":"md2pdf","correct":"from md2pdf.core import md2pdf"}],"quickstart":{"code":"from md2pdf.core import md2pdf\nimport os\n\nmarkdown_content = \"\"\"\n# My Awesome Document\n\nHello, **md2pdf**!\n\nThis is an example of converting Markdown to PDF using the library's API.\n\n- Item 1\n- Item 2\n\n```python\nprint(\"Code blocks are also supported\")\n```\n\"\"\"\n\noutput_filename = \"my_document.pdf\"\n\ntry:\n    md2pdf(\n        md_content=markdown_content,\n        dest=output_filename,\n        css_file=None, # Path to a CSS file for custom styling, or None\n        base_url=None, # Base URL for relative paths in markdown\n        md_extensions=['extra', 'fenced_code', 'tables'] # Common Markdown extensions\n    )\n    print(f\"Successfully created PDF: {output_filename}\")\nexcept Exception as e:\n    print(f\"Error converting markdown to PDF: {e}\")\n    # For WeasyPrint related errors, check system dependencies (see 'Problems')\n","lang":"python","description":"This quickstart demonstrates how to use the `md2pdf` function from the core API to convert a Markdown string directly into a PDF file. You can specify Markdown content, output destination, optional CSS for styling, and desired Markdown extensions."},"warnings":[{"fix":"Review the updated CLI usage by running `md2pdf --help`. For example, `md2pdf --input README.md --output README.pdf` replaces `md2pdf README.md README.pdf`.","message":"The command-line interface (CLI) underwent a significant breaking change in `v3.0.0`. It switched to Typer and moved from positional arguments to exclusively using options.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Ensure your Python environment is 3.10 or newer. Upgrade Python or use a virtual environment with a compatible version.","message":"`md2pdf` dropped support for Python versions older than 3.10 starting from `v2.0.0`. Attempts to install or run on older Python interpreters will fail.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"If you intend to use the `md2pdf` command in your terminal, always install with `pip install \"md2pdf[cli]\"`.","message":"Running the `md2pdf` command-line tool requires installing the `cli` extra (`pip install \"md2pdf[cli]\"`). A basic `pip install md2pdf` will only install the library API, not the executable CLI.","severity":"gotcha","affected_versions":">=3.0.0"},{"fix":"If you don't use Jinja templating, ensure your Markdown content does not contain Jinja-like syntax that could conflict with templating rules. Pass a `jinja_template=None` if you are using the API and want to disable Jinja processing for some reason (though `md2pdf` processes `md_content` as a template directly).","message":"Since `v2.0.0`, all input Markdown content is automatically treated as a Jinja2 template. This means any Jinja-like syntax (e.g., `{{ variable }}` or `{% for item in list %}`) will be processed, potentially leading to errors or unexpected output if not intended.","severity":"gotcha","affected_versions":">=2.0.0"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Install `md2pdf` with the CLI extras: `pip install \"md2pdf[cli]`.","cause":"The `md2pdf` CLI executable was not installed because the `cli` extra was omitted during installation.","error":"md2pdf: command not found"},{"fix":"Install the library using pip: `pip install md2pdf` (or `pip install \"md2pdf[cli]` for CLI).","cause":"The `md2pdf` library is not installed in the active Python environment, or the environment is not correctly activated.","error":"ModuleNotFoundError: No module named 'md2pdf'"},{"fix":"Update your CLI command to use options: `md2pdf --input README.md --output output.pdf`.","cause":"This error typically occurs when using pre-v3.0.0 CLI syntax (positional arguments) on `md2pdf` versions 3.0.0 or later, where the CLI switched to Typer and uses only options.","error":"md2pdf: error: unrecognized arguments: README.md output.pdf"},{"fix":"Install the necessary system dependencies for WeasyPrint. For Debian/Ubuntu: `sudo apt-get install python3-dev libffi-dev libxml2-dev libxslt1-dev libjpeg-dev libpng-dev zlib1g-dev libpango1.0-0 libcairo2 libgdk-pixbuf2.0-0`. For macOS: `brew install pango cairo gdk-pixbuf`.","cause":"WeasyPrint, a core dependency of md2pdf, relies on external system libraries like Pango, Cairo, and GDK-Pixbuf. These need to be installed at the operating system level.","error":"md2pdf.exceptions.ConversionError: WeasyPrint exited with error code 1. Make sure all external dependencies are installed.\n\nMore info: https://doc.weasyprint.org/en/stable/install.html"}]}