Files
faster-whisper-n-ionet-llm/services/convertMdToPdf.py
swrneko 83e19c471a add changes in pdf logic
changes:
   1. Remove `/` file (just missed click when create it);
   2. Add filename input for pdf;
   3. Now pdf file saves.
2025-09-09 18:24:18 +03:00

30 lines
1.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import re
from pylatexenc.latex2text import LatexNodes2Text
from markdown_pdf import MarkdownPdf
from markdown_pdf import Section
class ConvertMdToPdf:
# Конвертирует md в pdf
def convertLatexToText(self, text:str):
# Обрабатываем только математические выражения
text = re.sub(
r'\$\$(.*?)\$\$|\$(.*?)\$',
self.replace_math,
text,
flags=re.DOTALL
)
pdf = MarkdownPdf(toc_level=0, optimize=True)
pdf.add_section(Section(text))
return pdf, text
def replace_math(self, match):
math_content = match.group(1) or match.group(2) # $$...$$ или $...$
try:
# Преобразуем только математическое выражение
converted = LatexNodes2Text().latex_to_text(math_content)
return converted
except:
return math_content # В случае ошибки оставляем как есть