FormaTeX

\usepackage{ruby}

Ruby LaTeX to PDF API

Compile LaTeX to PDF from Ruby using the FormaTeX REST API. Standard Net::HTTP — no gem required, no TeX Live installation needed.

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

\section{Why Ruby}

Why Ruby + FormaTeX

No gem required

The FormaTeX API is a plain HTTPS endpoint. Use the Ruby standard library — no httparty, no faraday, nothing to add to your Gemfile.

Rails-ready

Drop a single controller action into your Rails app to stream PDFs directly to the browser — no background job needed for small documents.

No binary bundling

Forget adding TeX Live to your Heroku buildpack or Docker image. FormaTeX handles the compilation infrastructure entirely.

\section{Quick Start}

Quick start

The example below compiles a minimal LaTeX document and writes the resulting PDF to disk using Ruby's built-in Net::HTTP. Replace FORMATEX_API_KEY with a key from your dashboard.

compile.rb
require 'net/http'
require 'json'
require 'uri'

API_KEY = ENV.fetch('FORMATEX_API_KEY')
API_URL = URI.parse('https://api.formatex.io/v1/compile/sync')

def compile_latex(source, engine: 'pdflatex')
  http          = Net::HTTP.new(API_URL.host, API_URL.port)
  http.use_ssl  = true

  request                  = Net::HTTP::Post.new(API_URL.path)
  request['Authorization'] = "Bearer #{API_KEY}"
  request['Content-Type']  = 'application/json'
  request.body             = { source: source, engine: engine }.to_json

  response = http.request(request)

  raise "API error #{response.code}: #{response.body}" unless response.code == '200'

  response.body
end

latex = <<~LATEX
  \documentclass{article}
  \usepackage{amsmath}
  \begin{document}
  Hello from Ruby! $E = mc^2$
  \end{document}
LATEX

pdf = compile_latex(latex)
File.binwrite('output.pdf', pdf)
puts 'PDF saved to output.pdf'

\section{Rails Integration}

Rails controller

Add a DocumentsController to your Rails app. The compile action accepts a JSON body with source and optional engine fields, then streams the compiled PDF back with send_data.

app/controllers/documents_controller.rb
# app/controllers/documents_controller.rb
require 'net/http'
require 'json'
require 'uri'

class DocumentsController < ApplicationController
  FORMATEX_URL = URI.parse('https://api.formatex.io/v1/compile/sync').freeze

  # POST /documents/compile
  def compile
    source = params.require(:source)
    engine = params.fetch(:engine, 'pdflatex')

    pdf_bytes = call_formatex(source, engine)

    send_data pdf_bytes,
              type:        'application/pdf',
              disposition: 'inline',
              filename:    'document.pdf'
  rescue => e
    render json: { error: e.message }, status: :unprocessable_entity
  end

  private

  def call_formatex(source, engine)
    http           = Net::HTTP.new(FORMATEX_URL.host, FORMATEX_URL.port)
    http.use_ssl   = true
    http.open_timeout = 5
    http.read_timeout = 60

    req                  = Net::HTTP::Post.new(FORMATEX_URL.path)
    req['Authorization'] = "Bearer #{ENV.fetch('FORMATEX_API_KEY')}"
    req['Content-Type']  = 'application/json'
    req.body             = { source: source, engine: engine }.to_json

    res = http.request(req)
    raise "FormaTeX error #{res.code}: #{res.body}" unless res.code == '200'

    res.body
  end
end

\section{Authentication}

Environment variable setup

Store your API key in the environment — never commit it to source control. Use ENV.fetch so your app raises an error at startup if the key is missing:

API_KEY = ENV.fetch('FORMATEX_API_KEY')

Shell / .env

export FORMATEX_API_KEY=fmtx_…

Rails credentials

rails credentials:edit

Heroku

heroku config:set FORMATEX_API_KEY=fmtx_…

\section{Engines}

Engine selection

EngineBest for
pdflatexDefault. Fast, widely compatible, good for most documents
xelatexUnicode text, custom system fonts, Arabic/CJK scripts
lualatexAdvanced typography, scripting with Lua, large documents
latexmkAutomatically runs the correct number of passes for cross-references

\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).

\section{Error Handling}

Error handling

Check response.code before reading the body. The API returns standard HTTP status codes:

  • 200 — raw PDF bytes. Read with response.body.
  • 422 — LaTeX compilation error. JSON body contains an error field with the TeX log excerpt.
  • 401 — invalid or missing API key.

\end{ruby}

Start compiling LaTeX from Ruby

Get your API key and have PDF output from Ruby in minutes — no TeX Live 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