python conversion from markdown to pdf

This article is converted by SimpRead, original address blog.csdn.net

Table of Contents

Method 1: Using WeasyPrint

You need to install WeasyPrint and markdown packages:

pip install weasyprint markdown
import markdown
from weasyprint import HTML, CSS

# Read Markdown file
with open('example.md', 'r', encoding='utf-8') as f:
    text = f.read()
    html = markdown.markdown(text, output_format='html5')

# Convert HTML to PDF
HTML(string=html).write_pdf("output_weasyprint.pdf")

Method 2: Using pdfkit and wkhtmltopdf

You need to install pdfkit and markdown packages:

pip install pdfkit markdown
apt install wkhtmltopdf
import pdfkit
import markdown

# Read Markdown file
with open('example.md', 'r', encoding='utf-8') as f:
    text = f.read()
    html = markdown.markdown(text)

# Add Chinese font style to HTML
html_with_style = f"""
<html>
<head>
    <meta charset="UTF-8">
    <style>
        body {{
            font-family: 'SimHei', 'Microsoft YaHei', sans-serif;
            font-size: 12pt;
        }}
    </style>
</head>
<body>
{html}
</body>
</html>
"""

# Use extra options to enable local file access (for loading fonts)
pdfkit.from_string(html_with_style, 'output_pdfkit.pdf', options={'enable-local-file-access': None})

Method 3: Using md2pdf

pip install md2pdf weasyprint
from md2pdf.core import md2pdf

def convert_md_to_pdf(input_md_file_path, output_pdf_file_path):
    """
    Converts a Markdown file to a PDF file with Chinese font support.
    """
    with open(input_md_file_path, 'r', encoding='utf-8') as f:
        text = f.read()

    # Convert and apply style
    md2pdf(output_pdf_file_path, md_content=text)

Method 4: Using pandoc and wkhtmltopdf

pip install pypandoc
apt install pandoc wkhtmltopdf
import pypandoc

def md_to_pdf_pandoc(input_md, output_pdf):
    try:
        output = pypandoc.convert_file(
            input_md, 'pdf', outputfile=output_pdf,
            extra_args=['--pdf-engine=wkhtmltopdf']  # Use xelatex instead of pdflatex
        )
        print(f"PDF generated: {output_pdf}")
    except Exception as e:
        print("Conversion failed:", str(e))

Does not support formula rendering.

Install Tex components

sudo apt update
sudo apt install texlive-latex-base texlive-fonts-recommended texlive-fonts-extra texlive-latex-extra texlive-xetex  texlive-full

Use xelatex engine, download template https://github.com/Wandmalfarbe/pandoc-latex-template?tab=readme-ov-file, place it into /root/.local/share/pandoc/templates

import pypandoc

def md_to_pdf_pandoc(input_md, output_pdf):
    try:
        # Use custom LaTeX template or inline style
        output = pypandoc.convert_file(
            input_md, 'pdf',
            outputfile=output_pdf,
            extra_args=[
                '--pdf-engine=xelatex',
                '-V', 'documentclass=ctexart',  # Use ctex package for Chinese support
                '-V', 'CJKmainfont=SimHei',     # Set Chinese font
                '-V', 'monofont=DejaVu Sans Mono',  # Set monospaced font to support code blocks
                '-V', 'geometry:margin=1in',    # Set page margins
                '--template=eisvogel',  # Use custom template
                '--listings',
                '--highlight-style=pygments'    # Use syntax highlighting style (with background)
            ]
        )
        print(f"PDF generated: {output_pdf}")
    except Exception as e:
        print("Conversion failed:", str(e))

# Call function
md_to_pdf_pandoc('example.md', 'out.pdf')   # from https://michael.blog.csdn.net/

Effect Comparison

Original MDWeasyPrintpdfkitmd2pdfpandocpandoc+tex
Code blocks, formulas, tables do not work