Generating downloadable PDF invoices, reports, or certificates from dynamic HTML and CSS templates in PHP is cleanly accomplished using the mPDF library.

PHP mPDF Implementation Code Example

generate_pdf.phpphp
<?php
 
require_once __DIR__ . '/vendor/autoload.php';
 
use Mpdf\Mpdf;
 
function generatePdfInvoice(string $customerName, float $amount): void {
    $mpdf = new Mpdf([
        'mode' => 'utf-8',
        'format' => 'A4',
        'margin_left' => 15,
        'margin_right' => 15,
    ]);
 
    // HTML / CSS content string
    $html = "
        <style>
            h1 { color: #0066CC; font-family: sans-serif; }
            .invoice-table { width: 100%; border-collapse: collapse; }
            .invoice-table td { border: 1px solid #ddd; padding: 8px; }
        </style>
        <h1>Invoice Statement</h1>
        <p>Customer: <strong>" . htmlspecialchars($customerName) . "</strong></p>
        <table class='invoice-table'>
            <tr><td>Total Due:</td><td>$" . number_format($amount, 2) . "</td></tr>
        </table>
    ";
 
    $mpdf->WriteHTML($html);
    
    // Stream inline PDF to browser
    $mpdf->Output('invoice.pdf', \Mpdf\Output\Destination::INLINE);
}
 
generatePdfInvoice("Lynxbee Client", 250.00);