\usepackage{nodejs}

واجهة Node.js لتحويل LaTeX إلى PDF

جمّع LaTeX إلى PDF من Node.js أو أي بيئة تشغيل JavaScript باستخدام FormaTeX REST API. لا حاجة إلى SDK، فاستدعاء fetch بسيط يكفي.

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

\section{Installation}

لا توجد حزمة للتثبيت

FormaTeX واجهة HTTP بسيطة. يأتي Node 18+ مع fetch أصلية. للإصدارات الأقدم من Node، ثبّت node-fetch .

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

\section{بداية سريعة}

جمّع أول ملف PDF لك

استدعِ نقطة النهاية /api/v1/compile مع شيفرة LaTeX المصدرية. الاستجابة ملف PDF ثنائي يمكنك كتابته مباشرةً على القرص أو تمريره كتدفق إلى العميل.

const API_KEY = process.env.FORMATEX_API_KEY!;
const API_URL = "https://api.formatex.io/api/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`, {
  method: "POST",
  headers: {
    "X-API-Key": API_KEY,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    latex: 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{متغيرات البيئة}

تخزين مفتاح API

لا تكتب مفتاح API مباشرةً في الشيفرة أبدًا. خزّنه في متغير بيئة وحمّله وقت التشغيل.

Shell / .env

FORMATEX_API_KEY=your-key

حزمة dotenv

require('dotenv').config()

القراءة في الشيفرة

process.env.FORMATEX_API_KEY

\section{التكامل مع Express}

مثال على مسار Express

اعرض نقطة نهاية POST /generate-pdf تستقبل شيفرة LaTeX المصدرية من جسم الطلب وتمرّر ملف PDF المجمَّع مباشرةً إلى المستدعي كتدفق.

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/api/v1/compile", {
    method: "POST",
    headers: {
      "X-API-Key": process.env.FORMATEX_API_KEY!,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({ latex: 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{التخزين السحابي}

ارفع ملفات PDF المجمَّعة إلى S3

جمّع وارفع إلى AWS S3 في دالة واحدة. ينطبق النمط نفسه على أي تخزين متوافق مع S3، مثل R2 وGCS وDigitalOcean Spaces و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/api/v1/compile", {
    method: "POST",
    headers: {
      "X-API-Key": process.env.FORMATEX_API_KEY!,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({ 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{اختيار المحرك}

اختر محرك LaTeX

مرّر الحقل الاختياري engine في جسم الطلب. القيمة الافتراضية pdflatex عند عدم تحديده.

pdflatexxelatexlualatex

\section{مرجع API}

مرجع نقاط النهاية

الحقلالنوعمطلوبالوصف
latexstringنعمالمصدر الكامل لمستند LaTeX
enginestringلاpdflatex (الافتراضي) أو xelatex أو lualatex
POST https://api.formatex.io/api/v1/compile← ملف PDF ثنائي (200) أو خطأ JSON (4xx/5xx)

\section{تكاملات أخرى}

FormaTeX من أي لغة

FormaTeX واجهة HTTP بسيطة. أمثلة جاهزة للنسخ واللصق لكل لغة رئيسية.

هل أنت جاهز لتجميع LaTeX من Node.js؟

الخطة المجانية: 15 عملية تجميع مجانية عبر API كل شهر. دون الحاجة إلى بطاقة ائتمان.