FrameForge › Guides › HTML to PDF in Laravel

HTML to PDF in Laravel

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

Laravel HTTP client. Set FRAMEFORGE_KEY to your API key.

use Illuminate\Support\Facades\Http;

Route::get('/invoices/{invoice}/pdf', function (App\Models\Invoice $invoice) {
    $html = view('invoices.pdf', ['invoice' => $invoice])->render();

    $res = Http::withHeaders(['X-API-Key' => config('services.frameforge.key')])
        ->timeout(60)
        ->post('https://api.frameforgeapi.com/render', [
            'html' => $html, 'format' => 'pdf', 'width' => 794, 'height' => 1123,
        ]);

    abort_unless($res->successful(), 502, 'Render failed');

    return response($res->body(), 200, [
        'Content-Type' => 'application/pdf',
        'Content-Disposition' => "inline; filename=invoice-{$invoice->id}.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

barryvdh/laravel-dompdf is the common in-process option (CSS support limited), and spatie/browsershot drives Puppeteer, so you need Node and Chrome on the server.

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 · PHP · Ruby · Go · Java · C# / .NET · curl / Bash · Next.js · Django · Ruby on Rails