Mini Kabibi Habibi

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

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

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

require_once('tcpdf/tcpdf.php'); // Make sure you have TCPDF library installed

// Create new PDF document
$pdf = new TCPDF();
$pdf->AddPage();

// Set some basic styles
$pdf->SetFont('helvetica', '', 10);

// Column headers
$headers = ['ID', 'Date', 'Name', 'Type', 'School', 'Age', 'Sex', 'Address', 'Contact', 'Signature', 'Attended By'];

// Print headers
foreach ($headers as $col) {
    $pdf->Cell(18, 7, $col, 1, 0, 'C');
}
$pdf->Ln();

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

$signatureDir = __DIR__ . '/signatures/';

foreach ($rows as $row) {
    $pdf->Cell(18, 20, $row['id'], 1);
    $pdf->Cell(18, 20, date('m/d/Y', strtotime($row['log_date'])), 1);
    $pdf->Cell(18, 20, $row['patient_name'], 1);
    $pdf->Cell(18, 20, $row['client_type'], 1);
    $pdf->Cell(18, 20, $row['school'], 1);
    $pdf->Cell(18, 20, $row['age'], 1);
    $pdf->Cell(18, 20, $row['sex'], 1);
    $pdf->Cell(18, 20, $row['address'], 1);
    $pdf->Cell(18, 20, $row['contact_number'], 1);

    // Signature cell - if signature file exists, display image
    if (!empty($row['signature']) && file_exists($signatureDir . $row['signature'])) {
        $x = $pdf->GetX();
        $y = $pdf->GetY();
        $pdf->Cell(18, 20, '', 1); // empty cell for image space
        $pdf->Image($signatureDir . $row['signature'], $x + 2, $y + 2, 14, 16); // adjust size as needed
    } else {
        $pdf->Cell(18, 20, 'No Signature', 1, 0, 'C');
    }

    $pdf->Cell(18, 20, $row['attended_by'], 1);
    $pdf->Ln();
}

// Output PDF to browser for download
$pdf->Output('logged_patients.pdf', 'D');
exit();