A Python package for converting Markdown content or files to PDF (Playwright) or DOCX (Pandoc).
Project description
Mark2pdf
A Python package for converting Markdown content into PDF and DOCX documents using Playwright and Pandoc.
📦 Installation
pip install mark2pdf
External Dependencies
This package depends on two external tools that must be installed manually:
1. Pandoc (for DOCX conversion)
- Required for DOCX output.
- Install from: https://pandoc.org/installing.html
- Ensure
pandocis available in your system'sPATH.
2. Playwright Chromium (for PDF rendering)
- Required for PDF output.
- After installing
mark2pdf, run:
playwright install chromium
This downloads the headless browser needed for PDF generation.
Basic Usage (from Markdown File)
Here’s an example that reads from a input.md file and writes out both PDF and DOCX:
# run_conversion_locally.py
import asyncio
import os
import subprocess
import sys
from markdown_converter import convert_markdown, REFERENCE_DOCX_PATH_ENV_VAR, PANDOC_PATH_ENV_VAR
INPUT_MD_FILE = r"C:\Users\Mokksh\OneDrive\Desktop\Study\os_md.md"
PDF_OUTPUT_FILE = "output_pac.pdf"
DOCX_OUTPUT_FILE = "output_pac.docx"
CUSTOM_PDF_CSS = """
/* Custom CSS for test script output */
body { font-family: 'Roboto', sans-serif; line-height: 1.5; }
h1, h2 { color: #0056b3; border-bottom: 1px solid #0056b3; padding-bottom: 0.2em; }
code { color: darkgreen; background-color: #e8f5e9; padding: 0.1em 0.3em; border-radius: 3px; }
pre { background-color: #f0f4f7; padding: 1em; border-left: 4px solid #0056b3; }
@page { margin: 2cm; }
"""
async def main():
with open(INPUT_MD_FILE, "r", encoding="utf-8") as f:
markdown_content = f.read()
pdf_bytes = await convert_markdown(markdown_content, 'pdf', custom_css=CUSTOM_PDF_CSS)
with open(PDF_OUTPUT_FILE, "wb") as f:
f.write(pdf_bytes)
print(f"PDF saved to '{PDF_OUTPUT_FILE}'")
reference_docx = os.environ.get(REFERENCE_DOCX_PATH_ENV_VAR)
docx_bytes = await convert_markdown(markdown_content, 'docx', reference_docx=reference_docx)
with open(DOCX_OUTPUT_FILE, "wb") as f:
f.write(docx_bytes)
print(f"DOCX saved to '{DOCX_OUTPUT_FILE}'")
if __name__ == "__main__":
def check_pandoc():
pandoc_executable_path = os.environ.get(PANDOC_PATH_ENV_VAR, "pandoc")
pandoc_cmd = [pandoc_executable_path, "--version"]
subprocess.run(pandoc_cmd, check=True, capture_output=True, text=True, timeout=10)
check_pandoc()
asyncio.run(main())
📁 Sample input.md
Make sure you create a file named input.md in the same directory.
✅ Environment Variable (Optional)
To use a custom reference DOCX file for styling:
export MARKDOWN_CONVERTER_REFERENCE_DOCX=/path/to/your/reference.docx
📄 License
This project is licensed under the MIT License. See the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
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 mark_2_pdf-0.1.1.tar.gz.
File metadata
- Download URL: mark_2_pdf-0.1.1.tar.gz
- Upload date:
- Size: 13.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
533a342b852a5e7af382e1fb2db6efff9d6f5b768318b30d7df9e4c55c87aa6d
|
|
| MD5 |
dc191f2ec7f76dac9696af6611c2cd1d
|
|
| BLAKE2b-256 |
72241f1e8501fe8cd362000b24d56d503b7979dd9d5e7d91b5ddd2c13e9ea779
|
File details
Details for the file mark_2_pdf-0.1.1-py3-none-any.whl.
File metadata
- Download URL: mark_2_pdf-0.1.1-py3-none-any.whl
- Upload date:
- Size: 13.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
257bd06fdbe1cbaedba138c769c45ee300fc5dabd843aa8ea0ed7bb86a2e8a0f
|
|
| MD5 |
a69d9de09798491add858c6e5831f517
|
|
| BLAKE2b-256 |
8a19b843b606b0838bfa7f33acd480b6bcdc0ac5cb22c894be206e74e1fdf394
|