A lightweight Express.js microservice that compiles LaTeX documents to PDF format. This service provides a simple REST API for converting LaTeX source code into PDF files.
- ✅ RESTful API for LaTeX to PDF compilation
- ✅ Docker containerized with all LaTeX dependencies included
- ✅ Error handling with detailed compilation logs
- ✅ Temporary file cleanup
- ✅ JSON request/response format
- Node.js 14.x or higher
- npm or yarn
- LaTeX distribution (texlive-latex-base, texlive-latex-extra, etc.)
- Docker installed
- Clone the repository:
git clone <repository-url>
cd latex-service- Install dependencies:
npm install- Install LaTeX dependencies (Ubuntu/Debian):
sudo apt-get update
sudo apt-get install -y \
texlive-latex-base \
texlive-latex-extra \
texlive-fonts-recommended \
texlive-latex-recommended- Start the server:
npm start
# or
node server.jsThe service will be available at http://localhost:3000
- Build the Docker image:
docker build -t latex-service .- Run the container:
docker run -p 3000:3000 latex-serviceThe service will be available at http://localhost:3000
Compiles LaTeX code to PDF.
Request:
curl -X POST http://localhost:3000/compile \
-H "Content-Type: application/json" \
-d '{"latex":"\\documentclass{article}\\begin{document}Hello World\\end{document}"}'Request Body:
{
"latex": "\\documentclass{article}\n\\begin{document}\nHello World\n\\end{document}"
}Success Response (200):
- Returns the compiled PDF file with
Content-Type: application/pdf
Error Response (500):
{
"error": "PDF not generated",
"log": "<pdflatex compilation log>"
}const axios = require("axios");
const fs = require("fs");
const latexCode = `
\\documentclass{article}
\\usepackage[utf-8]{inputenc}
\\title{Sample Document}
\\author{Author Name}
\\date{\\today}
\\begin{document}
\\maketitle
\\section{Introduction}
This is a sample document.
\\end{document}
`;
axios
.post(
"http://localhost:3000/compile",
{ latex: latexCode },
{ responseType: "arraybuffer" },
)
.then((response) => {
fs.writeFileSync("output.pdf", response.data);
console.log("PDF generated successfully!");
})
.catch((error) => {
console.error("Compilation error:", error.response.data);
});import requests
import json
latex_code = r"""
\documentclass{article}
\begin{document}
Hello World
\end{document}
"""
response = requests.post(
'http://localhost:3000/compile',
json={'latex': latex_code},
headers={'Content-Type': 'application/json'}
)
if response.status_code == 200:
with open('output.pdf', 'wb') as f:
f.write(response.content)
print('PDF generated successfully!')
else:
print('Error:', response.json())Currently, the service uses a hardcoded port (3000). To make this configurable, modify server.js:
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));- Check the returned
logfield in the error response for LaTeX compilation errors - Ensure your LaTeX syntax is correct
- Verify all required LaTeX packages are included in the Dockerfile
# Find the process using port 3000
lsof -i :3000
# Kill the process (replace PID with the actual process ID)
kill -9 <PID># Clear Docker cache and rebuild
docker build --no-cache -t latex-service .Add the required package to the Dockerfile:
RUN apt-get install -y texlive-<package-name>Then rebuild the image.
latex-service/
├── Dockerfile # Docker configuration
├── package.json # Node.js dependencies
├── server.js # Main Express server
└── README.md # This file
- express (^4.18.2): Web framework for Node.js
- pdflatex: LaTeX to PDF compiler (system-level)
- Maximum request body size: 1MB
- PDF generation timeout: Uses system default (pdflatex timeout)
- Temporary files are stored in
/tmp
pdflatex --no-shell-escape to prevent shell command injection. However, ensure proper input validation and rate limiting are implemented in production environments.
For issues or questions, please open an issue in the repository.