\ai{pdf-generation}
Use GPT-4, Claude, or any LLM to generate LaTeX source, then compile it to a professional PDF with the FormaTeX API. Two API calls — unlimited document types, pixel-perfect output.
\section{The Pattern}
The pattern is simple: prompt an LLM to produce raw LaTeX source, then POST that source to the FormaTeX compile endpoint. The LLM handles document structure and content; FormaTeX handles the full TeX Live compilation pipeline. You get production-quality PDF output without managing a LaTeX installation.
LLM writes LaTeX
Modern LLMs produce syntactically correct LaTeX reliably — especially with a focused system prompt. GPT-4o, Claude Opus, and Gemini Pro all work well.
FormaTeX compiles
POST the LaTeX string to https://api.formatex.io/v1/compile/sync. Choose pdflatex, xelatex, or lualatex. Receive raw PDF bytes in the response.
Save or stream the PDF
Write the response bytes to a file, stream them to a browser download, or upload to cloud storage — whatever your workflow requires.
\section{LaTeX vs HTML}
| Criterion | LaTeX + FormaTeX | HTML-to-PDF |
|---|---|---|
| Mathematics | Native equation rendering with amsmath | MathJax workaround, inconsistent fonts |
| Typography | Knuth–Plass line breaking, professional kerning | Browser-quality, variable rendering |
| Page layout | Precise margins, headers, footers, multi-column | CSS-approximated, print quirks |
| Cross-references | \ref, \cite, \label — automatic numbering | Manual or fragile JS-based |
| Long documents | Chapter, section, TOC, bibliography | Difficult to manage at scale |
| LLM reliability | LLMs produce valid LaTeX more consistently | LLMs produce inconsistent inline CSS |
\section{Python Example}
Complete, runnable example using the OpenAI Python SDK and the requests library. Call generate_pdf() with any natural-language prompt to receive a .pdf file on disk.
import openai
import requests
openai_client = openai.OpenAI(api_key="your-openai-key")
FORMATEX_URL = "https://api.formatex.io/v1/compile/sync"
FORMATEX_API_KEY = "your-formatex-key"
SYSTEM_PROMPT = """You are a LaTeX expert. When asked to create a document,
respond ONLY with valid LaTeX source code. No markdown fences, no explanation.
Always include \\documentclass, a complete preamble, and
\\begin{document}...\\end{document}. Use standard packages only."""
def generate_latex(prompt: str) -> str:
"""Ask GPT-4 to write a complete LaTeX document."""
response = openai_client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": prompt},
],
temperature=0.2, # Low temperature for deterministic LaTeX output
)
return response.choices[0].message.content
def compile_to_pdf(latex_source: str, engine: str = "pdflatex") -> bytes:
"""Compile LaTeX source with FormaTeX and return PDF bytes."""
response = requests.post(
FORMATEX_URL,
headers={
"Authorization": f"Bearer {FORMATEX_API_KEY}",
"Content-Type": "application/json",
},
json={"source": latex_source, "engine": engine},
timeout=30,
)
response.raise_for_status()
return response.content
def generate_pdf(prompt: str, output_path: str, engine: str = "pdflatex") -> None:
"""End-to-end: prompt → LaTeX → PDF saved to disk."""
print(f"Generating LaTeX for: {prompt[:60]}...")
latex_source = generate_latex(prompt)
print("Compiling PDF with FormaTeX...")
pdf_bytes = compile_to_pdf(latex_source, engine)
with open(output_path, "wb") as f:
f.write(pdf_bytes)
print(f"PDF saved to {output_path} ({len(pdf_bytes):,} bytes)")
# Example usage
generate_pdf(
prompt=(
"Write a 3-page LaTeX technical report on transformer neural networks. "
"Include an abstract, introduction, the scaled dot-product attention "
"equation, a comparison table of model sizes, and a conclusion."
),
output_path="transformer_report.pdf",
engine="pdflatex",
)\section{Prompt Tips}
Enforce raw LaTeX in the system prompt
Include "respond ONLY with valid LaTeX source code — no markdown fences, no explanation" in the system message. Without this, models often wrap output in ```latex blocks that must be stripped.
Specify the document class explicitly
Tell the model which documentclass to use: "Use \documentclass{article}" or "\documentclass[12pt,a4paper]{report}". This prevents the model from choosing an inappropriate class.
Keep temperature low
Set temperature to 0.1–0.3 for LaTeX generation. High temperature increases the chance of hallucinated commands or missing braces that break compilation.
List the packages you need
Explicitly tell the model which packages to include: amsmath, graphicx, booktabs, hyperref. Models sometimes import nonexistent packages when left to choose freely.
Validate before compiling
Strip markdown fences with a simple regex before POSTing. Check that the output contains \documentclass and \begin{document} — if not, re-prompt rather than wasting a compile request.
Use xelatex for Unicode content
If the document may contain non-ASCII characters (names, accents, CJK), set engine to xelatex and instruct the model to use \usepackage{fontspec} instead of inputenc.
\section{Related}
\end{pdf-generation}
Get a FormaTeX API key, copy the Python example above, and ship AI-generated professional PDFs in under ten minutes.
One quick thing
We track anonymous usage — page views, feature usage, compilation events — to understand what works and what doesn't. No ads, no personal data, no third-party sharing.