Laravel6 min read · June 1, 2026

How to Generate PDFs from HTML in Laravel (2026 Guide)

A practical guide to generating PDFs from HTML in Laravel. Skip the server config — use the PHP SDK and a REST API to get pixel-perfect PDFs in minutes.

PDF generation is one of those features that looks simple on the surface — until you actually have to ship it. You install DomPDF, CSS breaks everywhere. You try wkhtmltopdf, and now you need a system binary on every server. You reach for Puppeteer, and suddenly you're managing a headless Chromium process in production.

There's a better way. This guide shows you how to generate PDFs from HTML in Laravel using a REST API — no binaries, no browser processes, no server configuration. Just a Composer package and a single method call.

Why DomPDF and wkhtmltopdf Fall Short

DomPDF is the most popular PHP PDF library, and it works well for simple documents. But it has a CSS renderer from a different era: no flexbox, limited grid support, and quirky font handling. If your design uses modern CSS, expect to spend hours fighting it.

wkhtmltopdf uses a real browser engine (WebKit) so it renders CSS faithfully. The catch: it's an unmaintained binary that requires installation on your server, conflicts with cloud deployments, and has security considerations when rendering untrusted HTML.

A hosted REST API solves both problems. You send HTML, you get a PDF back. The rendering runs on the API side — always up to date, always consistent.

Install the PHP SDK

The HTML to PDF API has a first-class PHP package. Install it via Composer:

Terminalbash
composer require html-to-pdf-api/php-sdk

Generate a PDF from an HTML String

Once installed, grab your API key from the dashboard and add it to your .env file:

.envenv
HTMLTOPDF_API_KEY=your_api_key_here

Generate a PDF in a Laravel Controller

Use the SDK client to convert a Blade-rendered view to PDF and stream it to the browser:

app/Http/Controllers/InvoiceController.phpphp
<?php

namespace App\Http\Controllers;

use HtmlToPdfApi\Client;
use Illuminate\Http\Response;

class InvoiceController extends Controller
{
    public function download(int $id): Response
    {
        $invoice = Invoice::findOrFail($id);
        $html    = view('invoices.pdf', compact('invoice'))->render();

        $client = new Client(config('services.htmltopdfapi.key'));
        $pdf    = $client->fromHtml($html)
                         ->paperSize('a4')
                         ->orientation('portrait')
                         ->generate();

        return response($pdf, 200, [
            'Content-Type'        => 'application/pdf',
            'Content-Disposition' => 'inline; filename="invoice-' . $id . '.pdf"',
        ]);
    }
}

Convert a URL to PDF

You can also pass a URL instead of HTML. Useful for generating PDFs of public pages or staging previews:

controller snippetphp
$pdf = $client->fromUrl('https://your-app.com/invoice/42')
                 ->generate();

Next Steps

You're now generating PDFs in Laravel with a single method call and no server dependencies. The free tier includes 100 conversions per month — more than enough for development and low-traffic apps.

Check the integration guide for full SDK documentation, error handling, and advanced options like custom margins, headers, and footers.