Mini Kabibi Habibi

Current Path : C:/xampp/htdocs/clinic/
Upload File :
Current File : C:/xampp/htdocs/clinic/export_logged_patients_pdf.php

<?php
session_start();
require_once('includes/db.php');

// Access control: admin only
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'admin') {
    header("Location: login.php");
    exit();
}

// Include TCPDF library
require_once(__DIR__ . '/tcpdf/tcpdf.php');

// Create a new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// Set document info
$pdf->SetCreator('Clinic System');
$pdf->SetAuthor('Admin');
$pdf->SetTitle('All Logged Patients');
$pdf->SetSubject('Patient Log Records');
$pdf->SetKeywords('clinic, patients, logs, PDF');

// Set margins
$pdf->SetMargins(10, 10, 10, true);
$pdf->SetAutoPageBreak(TRUE, 10);

// Set font
$pdf->SetFont('helvetica', '', 10);

// Add a page
$pdf->AddPage();

// Title
$pdf->Write(0, 'All Logged Patients', '', 0, 'C', true, 0, false, false, 0);

// Table header
$html = '
<table border="1" cellpadding="4">
    <thead>
        <tr style="background-color: #f2f2f2;">
            
            <th><b>Date</b></th>
            <th><b>Name</b></th>
            <th><b>Type</b></th>
            <th><b>School</b></th>
            <th><b>Age</b></th>
            <th><b>Sex</b></th>
            <th><b>Address</b></th>
            <th><b>Contact</b></th>
            <th><b>Signature</b></th>
            <th><b>Attended By</b></th>
        </tr>
    </thead>
    <tbody>
';

$stmt = $pdo->prepare("SELECT * FROM logs ORDER BY id DESC");
$stmt->execute();
$patients = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Base path to signature images
$signatureBaseUrl = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['REQUEST_URI']) . '/signatures/';

foreach ($patients as $row) {
    $signatureHtml = '<i>No Signature</i>';
    if (!empty($row['signature'])) {
        $signaturePath = __DIR__ . '/signatures/' . $row['signature'];
        if (file_exists($signaturePath)) {
            // Embed image using base64
            $imgData = base64_encode(file_get_contents($signaturePath));
            $signatureHtml = '<img src="data:image/png;base64,' . $imgData . '" width="80"/>';
        }
    }

    $html .= '<tr>
        
        <td>' . date('m/d/Y', strtotime($row['log_date'])) . '</td>
        <td>' . htmlspecialchars($row['patient_name']) . '</td>
        <td>' . htmlspecialchars($row['client_type']) . '</td>
        <td>' . htmlspecialchars($row['school']) . '</td>
        <td>' . htmlspecialchars($row['age']) . '</td>
        <td>' . htmlspecialchars($row['sex']) . '</td>
        <td>' . htmlspecialchars($row['address']) . '</td>
        <td>' . htmlspecialchars($row['contact_number']) . '</td>
        <td>' . $signatureHtml . '</td>
        <td>' . htmlspecialchars($row['attended_by']) . '</td>
    </tr>';
}

$html .= '</tbody></table>';

// Output the HTML content
$pdf->writeHTML($html, true, false, true, false, '');

// Close and output PDF document
$pdf->Output('logged_patients.pdf', 'D');
exit;