cloudairambo / utils /pandoc_utils.py
rihancloud's picture
Deploy clean FastAPI backend without node_modules
99fdb5f
Raw History Blame Contribute Delete
730 Bytes
import subprocess
import os
import uuid
from core.config import settings
def convert_pandoc(input_path: str, target_ext: str):
target_ext = target_ext.lower().lstrip(".")
output_name = f"{uuid.uuid4()}.{target_ext}"
output_path = os.path.join(settings.OUTPUT_DIR, output_name)
cmd = ["pandoc", input_path, "-o", output_path]
# ✅ CRITICAL LINE (UNICODE SAFE)
if target_ext == "pdf":
cmd.extend(["--pdf-engine=xelatex"])
proc = subprocess.run(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
if proc.returncode != 0:
stderr = proc.stderr.decode(errors="ignore")
raise RuntimeError("Pandoc failed: " + stderr)
return output_path