LaTeX PDF Generator
Generate professional PDF documents by writing a Python script that constructs LaTeX source strings and compiles them to PDF using Tectonic. No TeX distribution installation required — Tectonic auto-downloads packages on first use.
Approach
- Python script builds LaTeX content as a string using f-strings/concatenation
- Write the string to a
.texfile - Compile with
tectonic <file>.tex→ produces.pdfin the same directory
This approach is preferred over HTML-to-PDF or JS-based PDF libraries because LaTeX produces typographically superior output with precise control over layout, tables, math, headers/footers, and page breaks.
When NOT to use LaTeX:
- Presentations / slide decks — LaTeX produces ugly, hard-to-brand slides. Use the
image-from-htmlskill instead (HTML/CSS slides → Chrome headless → PNG → PDF). HTML gives pixel-perfect brand control that LaTeX can't match. - Branded marketing materials — posters, social graphics, flyers. Use
image-from-htmlfor exact brand guideline matching.
When to use LaTeX:
- Reports, invoices, CVs, academic papers, data-heavy documents with tables and statistics — anything that benefits from precise typographic control over text-heavy content.
Prerequisites
- Python 3 (standard library only — no pip packages needed)
- Tectonic:
brew install tectonic(macOS) orcargo install tectonic
Quick Start
import subprocess
from pathlib import Path
def generate_report(data: dict, output_path: Path):
tex = build_latex(data)
tex_path = output_path.with_suffix(".tex")
tex_path.write_text(tex)
result = subprocess.run(
["tectonic", str(tex_path)],
capture_output=True, text=True,
)
if result.returncode != 0:
raise RuntimeError(f"tectonic failed:\n{result.stderr}")
Essential LaTeX Helpers
Always include these in the Python script:
def esc(text: str) -> str:
"""Escape LaTeX special characters."""
for old, new in [
("\\", "\\textbackslash{}"), ("&", "\\&"), ("%", "\\%"),
("$", "\\$"), ("#", "\\#"), ("_", "\\_"),
("{", "\\{"), ("}", "\\}"),
("~", "\\textasciitilde{}"), ("^", "\\textasciicircum{}"),
]:
text = text.replace(old, new)
return text
def pct(v: float, decimals: int = 1) -> str:
"""Format a 0-1 value as a LaTeX percentage."""
return f"{v * 100:.{decimals}f}\\%"
Document Structure
Key Patterns
Compile with Tectonic
result = subprocess.run(
["tectonic", str(tex_path)],
capture_output=True, text=True,
)
if result.returncode != 0:
print(result.stderr)
sys.exit(1)
Tectonic downloads LaTeX packages automatically on first run. No tlmgr or TeX Live needed. It produces the PDF in the same directory as the .tex file.