\usepackage{php}
Compile LaTeX to PDF from PHP in a single HTTP request. Use the built-in cURL extension for plain PHP or the Http::post facade in Laravel — no SDK installation required.
\section{Quick Start}
The example below uses PHP's built-in curl_init to compile a minimal LaTeX document and write the resulting PDF to disk. No Composer packages needed.
<?php
$apiKey = getenv('FORMATEX_API_KEY');
$apiUrl = 'https://api.formatex.io/v1';
$latexSource = <<<'LATEX'
\documentclass{article}
\usepackage{amsmath}
\begin{document}
Hello from PHP! $E = mc^2$
\end{document}
LATEX;
$ch = curl_init($apiUrl . '/compile/sync');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . $apiKey,
'Content-Type: application/json',
],
CURLOPT_POSTFIELDS => json_encode([
'source' => $latexSource,
'engine' => 'pdflatex',
]),
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode !== 200) {
$error = json_decode($response, true);
throw new RuntimeException('Compilation failed: ' . $error['error']);
}
file_put_contents('output.pdf', $response);
echo "PDF saved to output.pdf\n";\section{Laravel}
Use Laravel's Http facade to call the FormaTeX API from a controller. The example below streams the PDF directly back to the browser as a download.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Http;
class PdfController extends Controller
{
public function generate(Request $request): Response
{
$request->validate([
'source' => 'required|string',
'engine' => 'in:pdflatex,xelatex,lualatex,latexmk',
]);
$response = Http::withHeaders([
'Authorization' => 'Bearer ' . config('services.formatex.key'),
])->post('https://api.formatex.io/v1/compile/sync', [
'source' => $request->input('source'),
'engine' => $request->input('engine', 'pdflatex'),
]);
if (!$response->successful()) {
return response()->json([
'error' => $response->json('error'),
], 500);
}
return response($response->body(), 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename=output.pdf',
]);
}
}\section{Authentication}
Store your API key in an environment variable and read it at runtime. Never hardcode keys in source files.
Plain PHP
$apiKey = getenv('FORMATEX_API_KEY');
Laravel (.env + config)
# .env
FORMATEX_API_KEY=your-api-key
# config/services.php
'formatex' => ['key' => env('FORMATEX_API_KEY')],\section{Engines}
Pass an engine field in your JSON body. Omit it to use the default pdflatex.
pdflatexFastest. Best for standard documents.xelatexUnicode and system font support.lualatexLua scripting, advanced typography.latexmkAuto-runs passes for references/TOC.See engine documentation for a full comparison.
\section{Reference}
| Field | Type | Required | Description |
|---|---|---|---|
| source | string | Yes | Full LaTeX source code as a string |
| engine | string | No | Engine to use. Default: "pdflatex". Options: pdflatex, xelatex, lualatex, latexmk |
POST https://api.formatex.io/v1/compile/sync — Returns raw PDF bytes on success (200), or JSON error on failure (422/401).
\section{Other SDKs}
\end{php}
Get your API key, run the quickstart, and have PDF output in minutes — no Composer packages required.
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.