FormaTeX

\usepackage{nodejs}

Node.js LaTeX to PDF API

Compile LaTeX to PDF from Node.js or any JavaScript runtime using the FormaTeX REST API. No SDK required — a plain fetch call is all you need.

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

\section{Installation}

No package to install

FormaTeX is a plain HTTP API. Node 18+ ships with native fetch. For older Node versions, install node-fetch.

npm install node-fetch  # or use built-in fetch (Node 18+)
# No SDK required — use the REST API directly

\section{Quick start}

Compile your first PDF

Call the /v1/compile/sync endpoint with your LaTeX source. The response is a binary PDF you can write directly to disk or stream to the client.

const API_KEY = process.env.FORMATEX_API_KEY!;
const API_URL = "https://api.formatex.io/v1";

const latexSource = `
\\documentclass{article}
\\usepackage{amsmath}
\\begin{document}
Hello from Node.js! $E = mc^2$
\\end{document}
`;

const response = await fetch(`${API_URL}/compile/sync`, {
  method: "POST",
  headers: {
    "Authorization": `Bearer ${API_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    source: latexSource,
    engine: "pdflatex",
  }),
});

if (!response.ok) {
  const error = await response.json();
  throw new Error(`Compilation failed: ${error.error}`);
}

const pdfBuffer = Buffer.from(await response.arrayBuffer());
await fs.writeFile("output.pdf", pdfBuffer);

\section{Environment variables}

Storing your API key

Never hardcode your API key. Store it in an environment variable and load it at runtime.

Shell / .env

FORMATEX_API_KEY=your-key

dotenv package

require('dotenv').config()

Read in code

process.env.FORMATEX_API_KEY

\section{Express integration}

Express route example

Expose a POST /generate-pdf endpoint that accepts LaTeX source from a request body and streams the compiled PDF directly back to the caller.

import express from "express";
import { writeFile } from "fs/promises";

const app = express();
app.use(express.json());

app.post("/generate-pdf", async (req, res) => {
  const { latexSource, engine = "pdflatex" } = req.body;

  const response = await fetch("https://api.formatex.io/v1/compile/sync", {
    method: "POST",
    headers: {
      "Authorization": `Bearer ${process.env.FORMATEX_API_KEY}`,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({ source: latexSource, engine }),
  });

  if (!response.ok) {
    const err = await response.json();
    return res.status(500).json({ error: err.error });
  }

  const pdfBuffer = Buffer.from(await response.arrayBuffer());
  res.setHeader("Content-Type", "application/pdf");
  res.setHeader("Content-Disposition", "attachment; filename=output.pdf");
  res.send(pdfBuffer);
});

app.listen(3000);

\section{Cloud storage}

Upload compiled PDFs to S3

Compile and upload to AWS S3 in a single function. The same pattern works for any S3-compatible storage — R2, GCS, DigitalOcean Spaces, and MinIO.

import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3";

const s3 = new S3Client({ region: "us-east-1" });

async function compileAndUpload(latex: string, key: string): Promise<string> {
  const response = await fetch("https://api.formatex.io/v1/compile/sync", {
    method: "POST",
    headers: {
      "Authorization": `Bearer ${process.env.FORMATEX_API_KEY}`,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({ source: latex, engine: "pdflatex" }),
  });

  if (!response.ok) throw new Error("Compilation failed");

  const pdfBuffer = Buffer.from(await response.arrayBuffer());

  await s3.send(new PutObjectCommand({
    Bucket: "my-bucket",
    Key: key,
    Body: pdfBuffer,
    ContentType: "application/pdf",
  }));

  return `https://my-bucket.s3.amazonaws.com/${key}`;
}

\section{Engine selection}

Choose your LaTeX engine

Pass an optional engine field in the request body. Defaults to pdflatex when omitted.

pdflatexxelatexlualatex

\section{API reference}

Endpoint reference

FieldTypeRequiredDescription
sourcestringYesFull LaTeX document source
enginestringNopdflatex (default), xelatex, or lualatex
POST https://api.formatex.io/v1/compile/sync→ binary PDF (200) or JSON error (4xx/5xx)

\section{Other integrations}

FormaTeX from any language

FormaTeX is a plain HTTP API. Copy-paste examples for every major language.

Ready to compile LaTeX from Node.js?

Free tier — 15 API compilations per month. No credit card 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