class Protocol { private $db; public function __construct() { $this->db = new Database(); } public function generateProtocolPDF($protocolId) { // Get protocol data $protocol = $this->getProtocolById($protocolId); if (!$protocol) { return false; } // Get protocol items $items = $this->getProtocolItemsByProtocolId($protocolId); // Include autoloader require_once __DIR__ . '/../vendor/autoload.php'; // Create output directory if it doesn't exist $outputDir = UPLOAD_DIR . '/protocols/pdf'; if (!file_exists($outputDir)) { mkdir($outputDir, 0755, true); } // Generate unique filename $filename = 'protocol_' . $protocol['protocol_number'] . '_' . date('Y-m-d_H-i-s') . '.pdf'; $outputPath = $outputDir . '/' . $filename; // Generate PDF using mPDF $mpdf = new \Mpdf\Mpdf([ 'mode' => 'utf-8', 'format' => 'A4', 'margin_top' => 20, 'margin_right' => 20, 'margin_bottom' => 20, 'margin_left' => 20, 'default_font' => 'dejavusans' ]); $mpdf->SetTitle('Протокол за извършена периодична проверка'); $mpdf->SetAuthor(PDF_COMPANY_NAME); $mpdf->SetFooter('Страница {PAGENO} от {nb}'); // Generate HTML content $html = $this->generateProtocolHTML($protocol, $items); $mpdf->WriteHTML($html); $mpdf->Output($outputPath, 'F'); return $outputPath; } private function generateProtocolHTML($protocol, $items) { $html = ' Протокол по график Д за пожарна безопасност
' . PDF_COMPANY_NAME . '
' . PDF_COMPANY_ADDRESS . '
Тел: ' . PDF_COMPANY_PHONE . ' | Имейл: ' . PDF_COMPANY_EMAIL . '

ПРОТОКОЛ

по график Д за пожарна безопасност

Номер на протокол: ' . htmlspecialchars($protocol['protocol_number']) . '

Дата: ' . date(DATE_FORMAT, strtotime($protocol['protocol_date'])) . '

Обект: ' . htmlspecialchars($protocol['object_name']) . '

Адрес: ' . htmlspecialchars($protocol['address'] ?? '') . '

Инспектор: ' . htmlspecialchars($protocol['inspector_name']) . ', ' . htmlspecialchars($protocol['inspector_position']) . '

Отговорно лице: ' . htmlspecialchars($protocol['responsible_person']) . ', ' . htmlspecialchars($protocol['responsible_position']) . '

Проверени елементи

'; if (!empty($items)) { $html .= ' '; foreach ($items as $item) { // Determine status class and text $statusClass = ''; $statusText = ''; switch ($item['item_status']) { case 'compliant': $statusClass = 'status-compliant'; $statusText = 'Съответства'; break; case 'non_compliant': $statusClass = 'status-non-compliant'; $statusText = 'Не съответства'; break; case 'not_applicable': $statusClass = 'status-not-applicable'; $statusText = 'Не е приложимо'; break; } $html .= ' '; } $html .= '
Описание Статус Забележки
' . $item['item_number'] . ' ' . htmlspecialchars($item['item_description']) . ' ' . $statusText . ' ' . htmlspecialchars($item['item_notes']) . '
'; } else { $html .= '

Няма добавени елементи към протокола.

'; } // Add notes if (!empty($protocol['notes'])) { $html .= '

Забележки

' . nl2br(htmlspecialchars($protocol['notes'])) . '

'; } // Add signatures $html .= '

Инспектор:

Име: ' . htmlspecialchars($protocol['inspector_name']) . '

Длъжност: ' . htmlspecialchars($protocol['inspector_position']) . '

Подпис: ________________

Отговорно лице:

Име: ' . htmlspecialchars($protocol['responsible_person']) . '

Длъжност: ' . htmlspecialchars($protocol['responsible_position']) . '

Подпис: ________________

'; return $html; } }