Path : /home/vishqocm/pcib.in/student/
File Upload :
Current File : /home/vishqocm//pcib.in/student/download_certificate.php

<?php
// Start session
session_start();

// Check if user is logged in
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'student') {
    header("Location: ../login.php");
    exit();
}

// Check if certificate number is provided
if (!isset($_GET['number']) || empty($_GET['number'])) {
    header("Location: mycertificate.php");
    exit();
}

$certificate_number = $_GET['number'];
$userId = $_SESSION['user_id'];
$format = isset($_GET['format']) ? $_GET['format'] : 'pdf';

// Include database connection
require_once 'includes/db_config.php';

// Verify this certificate belongs to the logged-in user
$query = "SELECT e.certificate_data, e.certificate_number, e.verification_code, e.certificate_meta,
                 c.title as course_title
          FROM enrollments e
          JOIN courses c ON e.course_id = c.id
          WHERE e.certificate_number = ? AND e.user_id = ?";

$stmt = $conn->prepare($query);
$stmt->bind_param("si", $certificate_number, $userId);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows === 0) {
    // Certificate not found or doesn't belong to this user
    header("Location: mycertificate.php");
    exit();
}

// Get certificate data
$certificate = $result->fetch_assoc();
$certificate_html = $certificate['certificate_data'];
$course_title = $certificate['course_title'];

if (empty($certificate_html)) {
    die("Certificate data not found. Please regenerate your certificate.");
}

// If format is PDF, generate PDF
if ($format === 'pdf') {
    // Set headers for PDF download
    header('Content-Type: application/pdf');
    header('Content-Disposition: attachment; filename="Certificate_' . $certificate_number . '.pdf"');
    
    // Use wkhtmltopdf or other PDF conversion library if available
    // For this example, we'll use a serverside API (use your own API key in production)
    $api_url = 'https://api.html2pdf.app/v1/generate';
    $api_key = 'e7DulyvHLEq9LPH2EsJa2i0tM8r2bptsDm1rlKfalpUvbRB9AAmw5Qd2GetAU8lZ'; // Replace with your API key
    
    $data = [
        'apiKey' => $api_key,
        'html' => $certificate_html,
        'options' => [
            'marginTop' => 0,
            'marginBottom' => 0,
            'marginLeft' => 0,
            'marginRight' => 0,
            'printBackground' => true,
            'format' => 'A4',
            'orientation' => 'portrait'
        ]
    ];
    
    // Initialize cURL
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $api_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Content-Type: application/json'
    ]);
    
    // Execute cURL request
    $response = curl_exec($ch);
    
    // Check for errors
    if (curl_errno($ch)) {
        die('Error generating PDF: ' . curl_error($ch));
    }
    
    // Close cURL
    curl_close($ch);
    
    // Output the PDF
    echo $response;
} else {
    // Just display the HTML
    echo $certificate_html;
}
?>