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 = '
Номер на протокол: ' . 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']) . '
| № | Описание | Статус | Забележки |
|---|---|---|---|
| ' . $item['item_number'] . ' | ' . htmlspecialchars($item['item_description']) . ' | ' . $statusText . ' | ' . htmlspecialchars($item['item_notes']) . ' |
Няма добавени елементи към протокола.
'; } // 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']) . '
Подпис: ________________