BossBey File Manager
PHP:
8.4.18
OS:
Linux
User:
kids
Root
/
home
/
kids
/
public_html
/
app
📤 Upload
📝 New File
📁 New Folder
Close
Editing: gerar_pdf.php
<?php session_start(); // Ensure user is logged in if (!isset($_SESSION['id'])) { header('Location: login.php'); exit(); } // Include database connection and TCPDF library require_once('evento/action/conexao.php'); // Adjust path if necessary require_once('tcpdf/tcpdf.php'); // IMPORTANT: Adjust this path to where you placed the TCPDF folder date_default_timezone_set('America/Sao_Paulo'); $database = new Database(); // $db = $database->conectar(); // // Get data from the POST request from relatorio_aula.php $selected_classes_ids = isset($_POST['selected_classes']) ? $_POST['selected_classes'] : []; $aluno_id = isset($_POST['aluno_id']) ? intval($_POST['aluno_id']) : 0; $aluno_nome = isset($_POST['aluno_nome']) ? $_POST['aluno_nome'] : 'Aluno Desconhecido'; // Basic validation if (empty($selected_classes_ids) || $aluno_id === 0) { die("Nenhuma aula selecionada ou aluno não especificado."); } // Prepare SQL query to fetch details of selected classes // Using placeholders for security $placeholders = implode(',', array_fill(0, count($selected_classes_ids), '?')); $sql = "SELECT id, start, end, descricao, observacao, title FROM `events` WHERE id IN ($placeholders) ORDER BY start ASC"; $stmt = $db->prepare($sql); // Bind parameters foreach ($selected_classes_ids as $k => $id) { $stmt->bindValue(($k+1), $id, PDO::PARAM_INT); } $stmt->execute(); $aulas_selecionadas = $stmt->fetchAll(PDO::FETCH_ASSOC); // Extend TCPDF to create custom header/footer class MYPDF extends TCPDF { // Custom property to pass student name to the header public $aluno_nome; // Page header public function Header() { // Set font $this->SetFont('helvetica', 'B', 12); // Title $this->Cell(0, 15, 'Relatório de Aulas', 0, false, 'C', 0, '', 0, false, 'M', 'M'); $this->Ln(5); // New line $this->SetFont('helvetica', '', 10); // Display student name in the header $this->Cell(0, 15, 'Aluno: ' . $this->aluno_nome, 0, false, 'C', 0, '', 0, false, 'M', 'M'); $this->Ln(10); // New line // Draw a horizontal line $this->writeHTML('<hr>', false, false, false, false, ''); } // Page footer public function Footer() { // Position at 15 mm from bottom $this->SetY(-15); // Set font $this->SetFont('helvetica', 'I', 8); // Page number $this->Cell(0, 10, 'Página ' . $this->getAliasNumPage() . '/' . $this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M'); } } // Create new PDF document $pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); // Set student name for the custom header $pdf->aluno_nome = $aluno_nome; // Set document information $pdf->SetCreator(PDF_CREATOR); $pdf->SetAuthor('Seu Nome/Empresa'); $pdf->SetTitle('Relatório de Aulas do Aluno ' . $aluno_nome); $pdf->SetSubject('Aulas do Aluno'); $pdf->SetKeywords('TCPDF, PDF, aulas, relatório, aluno'); // Set default header data (can be left empty if custom header is fully defined) $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING); // Set header and footer fonts $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN)); $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA)); // Set default monospaced font $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED); // Set margins (Adjusted top margin to accommodate custom header) $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP + 20, PDF_MARGIN_RIGHT); $pdf->SetHeaderMargin(PDF_MARGIN_HEADER); $pdf->SetFooterMargin(PDF_MARGIN_FOOTER); // Set auto page breaks enabled $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM); // Set image scale factor $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); // Set some language-dependent strings (optional, for localization) if (@file_exists(dirname(__FILE__).'/lang/eng.php')) { require_once(dirname(__FILE__).'/lang/eng.php'); $pdf->setLanguageArray($l); } // Set font for the main content $pdf->SetFont('dejavusans', '', 10); // 'dejavusans' supports UTF-8 characters // Add a page $pdf->AddPage(); // Build the HTML content for the PDF body $html = '<h1 style="text-align: center;">Detalhes das Aulas Selecionadas</h1>'; $html .= '<p><strong>Aluno:</strong> ' . htmlspecialchars($aluno_nome) . '</p>'; $html .= '<p><strong>Gerado em:</strong> ' . date('d/m/Y H:i:s') . '</p><hr>'; if (empty($aulas_selecionadas)) { $html .= '<p>Nenhuma aula encontrada para os IDs selecionados.</p>'; } else { foreach ($aulas_selecionadas as $aula) { $start_date = new DateTime($aula['start']); $end_date = new DateTime($aula['end']); $html .= '<div>'; $html .= '<h3>Aula em ' . $start_date->format('d/m/Y H:i') . ' - ' . $end_date->format('H:i') . '</h3>'; $html .= '<p><strong>Título:</strong> ' . htmlspecialchars($aula['title']) . '</p>'; $html .= '<p><strong>Descrição:</strong> ' . (!empty($aula['descricao']) ? htmlspecialchars($aula['descricao']) : 'N/A') . '</p>'; $html .= '<p><strong>Observação:</strong> ' . (!empty($aula['observacao']) ? htmlspecialchars($aula['observacao']) : 'N/A') . '</p>'; $html .= '<hr>'; // Separator for each class $html .= '</div>'; } } // Print the HTML content into the PDF $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true); // Close and output PDF document to the browser for download ('D') $pdf->Output('relatorio_aulas_' . str_replace(' ', '_', $aluno_nome) . '.pdf', 'D'); exit; ?>
Save
Cancel