Spaces:
Running
Running
Commit ·
8f970d0
1
Parent(s): 4b022cd
working PDF compilation API endpoint
Browse files- frontend/export_utils.py +17 -5
frontend/export_utils.py
CHANGED
|
@@ -61,11 +61,23 @@ def generate_latex(markdown_text: str) -> str:
|
|
| 61 |
def compile_pdf(tex_content: str) -> bytes:
|
| 62 |
"""Compiles a complete LaTeX string to a PDF using public latexonline.cc API."""
|
| 63 |
try:
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
if r.status_code == 200:
|
| 70 |
return r.content
|
| 71 |
else:
|
|
|
|
| 61 |
def compile_pdf(tex_content: str) -> bytes:
|
| 62 |
"""Compiles a complete LaTeX string to a PDF using public latexonline.cc API."""
|
| 63 |
try:
|
| 64 |
+
import urllib.parse
|
| 65 |
+
|
| 66 |
+
encoded_tex = urllib.parse.quote(tex_content)
|
| 67 |
+
url = f"https://latexonline.cc/compile?text={encoded_tex}"
|
| 68 |
+
|
| 69 |
+
# If the encoded URL is massive, the server might reject the GET request.
|
| 70 |
+
# Fallback to the POST /data endpoint with a multipart file upload.
|
| 71 |
+
if len(url) > 8000:
|
| 72 |
+
logger.info("URL too long, falling back to POST /data multipart")
|
| 73 |
+
r = requests.post(
|
| 74 |
+
"https://latexonline.cc/data?command=pdflatex",
|
| 75 |
+
files={"file": ("document.tex", tex_content)},
|
| 76 |
+
timeout=45
|
| 77 |
+
)
|
| 78 |
+
else:
|
| 79 |
+
r = requests.get(url, timeout=45)
|
| 80 |
+
|
| 81 |
if r.status_code == 200:
|
| 82 |
return r.content
|
| 83 |
else:
|