<?php
require_once __DIR__ . '/../../functions/security.php';

$tenant = enrep_tenant_segment($_REQUEST['tenant'] ?? null);
if ($tenant !== '' && file_exists('../../../' . $tenant . '/config.php')) {
    require_once('../../../' . $tenant . '/config.php');
} elseif (file_exists('/config.php')) {
    require_once('/config.php');
}

enrep_require_user();
require_once __DIR__ . '/manual_render.php';
require_once __DIR__ . '/../../packages/mpd/vendor/autoload.php';

$meta = enrepManualMeta();
$title = (string)$meta['title'];
$css = enrepManualCss();
$body = enrepManualBodyHtml(true);

$mpdf = new \Mpdf\Mpdf([
    'mode' => 'utf-8',
    'format' => 'A4',
    'orientation' => 'P',
    'margin_left' => 12,
    'margin_right' => 12,
    'margin_top' => 14,
    'margin_bottom' => 14,
]);

$mpdf->SetTitle($title);
$mpdf->SetHTMLFooter('<div style="font-size:8pt;color:#607d8b;text-align:center;">' . htmlspecialchars($title, ENT_QUOTES, 'UTF-8') . ' | Page {PAGENO} of {nbpg}</div>');
$mpdf->WriteHTML('<style>' . $css . '</style>', \Mpdf\HTMLParserMode::HEADER_CSS);
$mpdf->WriteHTML($body);
$mpdf->Output('enrep-user-manual.pdf', \Mpdf\Output\Destination::INLINE);
