HTML to PDF in PHP with the Rendlet API

Last updated: . Every code sample on this page was run against the live API with the sandbox key on that date.

Short answer: POST your HTML as JSON to https://rendlet.com/v1/pdf with an Authorization: Bearer header and write the response body to a file. PHP's curl extension is enough; Rendlet renders with headless Chromium on its side, so you don't install Chrome, Puppeteer or wkhtmltopdf on your server.

1. Try it with curl

The public sandbox key needs no signup (5 renders a day per network, 1 page, up to 50 KB, faint watermark).

# Works as-is with the public sandbox key. With your own key: export RENDLET_API_KEY=YOUR_API_KEY
curl -X POST https://rendlet.com/v1/pdf \
  -H "Authorization: Bearer ${RENDLET_API_KEY:-sandbox}" \
  -H "Content-Type: application/json" \
  -d '{"html":"<h1>Hello</h1><p>Rendered by Rendlet.</p>","options":{"format":"A4","margin":"15mm"}}' \
  -o hello.pdf

2. PHP: an html_to_pdf() helper

Save as render.php and run php render.php (needs the curl extension). It builds HTML from data, renders it, and saves order.pdf. Output when we ran it: saved order.pdf (1 page, 12823 bytes); 4 renders left (byte size varies slightly with Chromium).

<?php
// PHP 8+ with curl extension. Save as render.php and run: php render.php
// Uses the public sandbox key unless you set RENDLET_API_KEY=YOUR_API_KEY
$apiKey = getenv('RENDLET_API_KEY') ?: 'sandbox';

function html_to_pdf(string $html, array $options = ['format' => 'A4', 'margin' => '15mm']): array {
    global $apiKey;
    $payload = json_encode(['html' => $html, 'options' => $options], JSON_UNESCAPED_UNICODE);
    $ch = curl_init('https://rendlet.com/v1/pdf');
    curl_setopt_array($ch, [
        CURLOPT_POST => true,
        CURLOPT_HTTPHEADER => [
            'Authorization: Bearer ' . $apiKey,
            'Content-Type: application/json',
        ],
        CURLOPT_POSTFIELDS => $payload,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HEADER => true,
        CURLOPT_TIMEOUT => 60,
    ]);
    $raw = curl_exec($ch);
    if ($raw === false) {
        throw new RuntimeException('curl error: ' . curl_error($ch));
    }
    $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
    $headers = substr($raw, 0, $headerSize);
    $body = substr($raw, $headerSize);
    curl_close($ch);
    if ($status < 200 || $status >= 300) {
        // Errors are JSON, e.g. {"error":"quota_exceeded", ...}
        throw new RuntimeException("Rendlet $status: $body");
    }
    preg_match('/^X-Pages:\s*(\S+)/mi', $headers, $pages);
    preg_match('/^X-Quota-Remaining:\s*(\S+)/mi', $headers, $remaining);
    return [
        'pdf' => $body,
        'pages' => $pages[1] ?? null,
        'remaining' => $remaining[1] ?? null,
    ];
}

$order = ['id' => 'A-1042', 'customer' => 'Globex Corp', 'total' => '$2,035.00'];
$html = '<!doctype html>
<html><body style="font-family:sans-serif;padding:24px">
  <h1 style="color:#1f4e79">Order ' . htmlspecialchars($order['id']) . '</h1>
  <p>Customer: ' . htmlspecialchars($order['customer']) . '</p>
  <p><b>Total: ' . htmlspecialchars($order['total']) . '</b></p>
</body></html>';

$result = html_to_pdf($html);
file_put_contents('order.pdf', $result['pdf']);
echo 'saved order.pdf (' . $result['pages'] . ' page, ' . strlen($result['pdf']) . ' bytes); '
   . $result['remaining'] . " renders left\n";

In production, escape user-supplied values with htmlspecialchars (as above) or render HTML with your template engine (Twig, Blade, Laravel views), then pass the string to html_to_pdf(). Keep the API key in an environment variable on the server, never in browser code.

Returning the PDF from Laravel or a plain PHP route

Once you have the PDF bytes, send them with Content-Type: application/pdf (and Content-Disposition if you want a download), or upload them to object storage.

3. Options

FieldValues
options.formatA4, Letter, Legal, A3, A5
options.landscapetrue / false
options.marginCSS length, e.g. "15mm" or "0"
options.printBackgroundtrue / false
filenameName used in the Content-Disposition header
response"binary" (default) or "base64": JSON {filename, pages, bytes, content_type, base64}

For HTML input, JavaScript in the page is not run, and only data: URIs and public https:// assets (images, fonts, CSS) load. Inline your CSS or link it over https. Simplified Chinese works out of the box (Noto Sans SC is bundled).

Errors and response headers

Successful renders return X-Pages, X-Plan, X-Quota-Limit and X-Quota-Remaining. Errors are JSON like {"error":"...","message":"..."}: 400 bad request, 401 missing key, 403 invalid key, 413 HTML too large or too many pages, 429 quota exceeded, 500 render failed.

Limits to know

SandboxFreeStarterProScale
Price$0$0$7/mo$19/mo$59/mo
Documents5 / day / network100 / month1,000 / month5,000 / month25,000 / month
Requests per minute3103060120
Pages per document11050200500
HTML size50 KB1 MB2 MB2 MB2 MB

Every plan has a hard monthly cap: at the cap the API returns 429 with an upgrade_url, and you are never billed for overage. Failed renders don't count. Source: docs.

Try it on your own HTML. The sandbox renders a real PDF with no signup (5 a day per network, 1 page, watermarked). A free key gives 100 documents a month, up to 10 pages each, with no watermark.

Open the sandbox →   Get a free key · Pricing

Related