<?php
require_once __DIR__ . '/../vendor/autoload.php';

use Dompdf\Dompdf;
use Dompdf\Options;

$name = $_GET['name'] ?? 'STUDENT NAME';
$certId = 'RSL-' . date('Y') . '-' . rand(10000,99999);
$issueDate = date('F Y');

$html = file_get_contents(__DIR__ . '/certificate_tmp.html');

$html = str_replace('{{NAME}}', htmlspecialchars($name), $html);
$html = str_replace('{{CERT_ID}}', $certId, $html);
$html = str_replace('{{ISSUE_DATE}}', $issueDate, $html);

$options = new Options();
$options->set('isRemoteEnabled', true);
$options->set('chroot', realpath(__DIR__));

$dompdf = new Dompdf($options);
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$dompdf->stream("certificate_$name.pdf", ["Attachment" => false]);