FrameForge › Guides › HTML to PDF in PHP

HTML to PDF in PHP

Send HTML (or a URL) to the FrameForge API and get a PDF back. It is rendered in real Chromium, so modern CSS, web fonts and JavaScript all work. No browser to install or babysit.

The code

PHP 7.4+ with the cURL extension. Set FRAMEFORGE_KEY to your API key.

<?php
$payload = json_encode([
    "html"   => "<h1>Invoice #1001</h1><p>Total: £49.00</p>",
    "format" => "pdf",
    "width"  => 794,   // A4 at 96 DPI
    "height" => 1123,
]);
$ch = curl_init("https://api.frameforgeapi.com/render");
curl_setopt_array($ch, [
    CURLOPT_POST           => true,
    CURLOPT_POSTFIELDS     => $payload,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT        => 60,
    CURLOPT_HTTPHEADER     => [
        "Content-Type: application/json",
        "X-API-Key: " . getenv("FRAMEFORGE_KEY"),
    ],
]);
$pdf = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($status !== 200) { throw new RuntimeException("Render failed: $status $pdf"); }
file_put_contents("invoice.pdf", $pdf);

How it works

Rendering a URL instead of HTML

{"url": "https://example.com/report", "format": "pdf", "width": 1280, "height": 800, "fullPage": true}

Doing it yourself instead

Dompdf and mPDF are popular pure-PHP libraries. They are easy to install, but their CSS support is more limited than a real browser (modern flexbox and grid layouts often need rework).

An API makes sense when you don't want Chromium on your servers, or when you need images (PNG/JPEG) and PDFs from the same call. If you render a few PDFs a month on a server you already run, self-hosting is perfectly reasonable.

Free tool: Website to PDF. Other languages: Node.js · Python · Ruby · Go · Java · C# / .NET · curl / Bash · Next.js · Laravel · Django · Ruby on Rails