Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -69,7 +69,24 @@ def generate_tex(file_path):
|
|
| 69 |
|
| 70 |
#tex_code = load_txt("latex_code.tex")
|
| 71 |
return result.text
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
def generate_pdf_png(latex_code):
|
| 74 |
## output latex_code.pdf
|
| 75 |
# remove
|
|
@@ -80,15 +97,7 @@ def generate_pdf_png(latex_code):
|
|
| 80 |
print("file saved")
|
| 81 |
# Compile with pdflatex
|
| 82 |
#os.system("pdflatex latex_code.tex")
|
| 83 |
-
|
| 84 |
-
try:
|
| 85 |
-
# Set a timeout (in seconds)
|
| 86 |
-
result = subprocess.run(['pdflatex'],input=b'latex_code.tex\n', timeout=20,
|
| 87 |
-
stdout=subprocess.PIPE,
|
| 88 |
-
stderr=subprocess.PIPE)
|
| 89 |
-
print(result.stdout.decode())
|
| 90 |
-
except subprocess.TimeoutExpired:
|
| 91 |
-
print("Command timed out")
|
| 92 |
# Convert PDF to image using pdf2image (needs poppler installed)
|
| 93 |
pdf_file = "latex_code.pdf"
|
| 94 |
print("already have a pdf")
|
|
|
|
| 69 |
|
| 70 |
#tex_code = load_txt("latex_code.tex")
|
| 71 |
return result.text
|
| 72 |
+
def transform_to_pdf(tex_content):
|
| 73 |
+
#Read the .tex file
|
| 74 |
+
# API endpoint for LaTeX Online
|
| 75 |
+
url = "https://www.latexonline.cc/compile"
|
| 76 |
+
|
| 77 |
+
# Send the LaTeX file as text data
|
| 78 |
+
response = requests.post(url, data={"text": tex_content, "compiler": "pdflatex"})
|
| 79 |
+
|
| 80 |
+
# Check if the request was successful
|
| 81 |
+
if response.status_code == 200 and response.headers.get("Content-Type") == "application/pdf":
|
| 82 |
+
# Save the returned PDF file
|
| 83 |
+
pdf_filename = "latex_code.pdf"
|
| 84 |
+
with open(pdf_filename, "wb") as pdf_file:
|
| 85 |
+
pdf_file.write(response.content)
|
| 86 |
+
print(f"✅ PDF saved as {pdf_filename}")
|
| 87 |
+
else:
|
| 88 |
+
print(f"❌ Error: Could not generate PDF (Status Code: {response.status_code})")
|
| 89 |
+
|
| 90 |
def generate_pdf_png(latex_code):
|
| 91 |
## output latex_code.pdf
|
| 92 |
# remove
|
|
|
|
| 97 |
print("file saved")
|
| 98 |
# Compile with pdflatex
|
| 99 |
#os.system("pdflatex latex_code.tex")
|
| 100 |
+
transform_to_pdf(latex_code)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
# Convert PDF to image using pdf2image (needs poppler installed)
|
| 102 |
pdf_file = "latex_code.pdf"
|
| 103 |
print("already have a pdf")
|