File size: 649 Bytes
8557bbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import os
import subprocess
import tempfile


def compile_tikz_to_pdf(tikz_code):
    temp_dir = tempfile.mkdtemp()

    tex_path = os.path.join(temp_dir, "output.tex")
    pdf_path = os.path.join(temp_dir, "output.pdf")

    with open(tex_path, "w") as f:
        f.write(tikz_code)

    try:
        subprocess.run(
            ["pdflatex", "-interaction=nonstopmode", tex_path],
            cwd=temp_dir,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            check=True,
        )
        return pdf_path
    except subprocess.CalledProcessError as e:
        print("PDF compilation failed:", e)
        return None