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

<?php
// Start session
session_start();

// Check if user is logged in
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'student') {
    header('Content-Type: application/json');
    echo json_encode(['success' => false, 'message' => 'Authentication required']);
    exit();
}

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

// Get the certificate number
if (!isset($_POST['certificate_number']) || empty($_POST['certificate_number'])) {
    header('Content-Type: application/json');
    echo json_encode(['success' => false, 'message' => 'Certificate number is required']);
    exit();
}

$certificate_number = $_POST['certificate_number'];
$userId = $_SESSION['user_id'];

// Retrieve certificate data
$query = "SELECT e.certificate_data, 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) {
    header('Content-Type: application/json');
    echo json_encode(['success' => false, 'message' => 'Certificate not found']);
    exit();
}

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

if (empty($certificate_html)) {
    header('Content-Type: application/json');
    echo json_encode(['success' => false, 'message' => 'Certificate data not found']);
    exit();
}

// 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)) {
    header('Content-Type: application/json');
    echo json_encode(['success' => false, 'message' => 'Error generating PDF: ' . curl_error($ch)]);
    exit();
}

// Close cURL
curl_close($ch);

// Output the PDF
echo $response;
?>