FormaTeX

\usepackage{php}

PHP LaTeX to PDF API

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.

Also available in:Python·Node.js·Go·PHP·Ruby·Rust·cURL|Full API reference

\section{Quick Start}

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.

compile.php
<?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}

Laravel integration

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.

app/Http/Controllers/PdfController.php
<?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}

Environment variable setup

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')],
  • Generate API keys from your dashboard after signing up.
  • Keys are shown once on creation and stored as hashes — save yours immediately.

\section{Engines}

Engine selection

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}

API reference

FieldTypeRequiredDescription
sourcestringYesFull LaTeX source code as a string
enginestringNoEngine to use. Default: "pdflatex". Options: pdflatex, xelatex, lualatex, latexmk

POST https://api.formatex.io/v1/compile/syncReturns raw PDF bytes on success (200), or JSON error on failure (422/401).

\end{php}

Start compiling LaTeX from 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.

Cookie policy