404)); } // Delete token to prevent reuse delete_transient('hvac_certificate_token_' . $token); // Get file path $upload_dir = wp_upload_dir(); $file_path = $upload_dir['basedir'] . '/' . $certificate_data['file_path']; if (!file_exists($file_path)) { status_header(404); wp_die('Certificate file not found', 'Not Found', array('response' => 404)); } // Serve the file $file_name = basename($file_path); $file_size = filesize($file_path); // Set headers nocache_headers(); header('Content-Type: application/pdf'); header('Content-Disposition: attachment; filename="certificate.pdf"'); header('Content-Transfer-Encoding: binary'); header('Content-Length: ' . $file_size); // Clear any output buffers while (ob_get_level()) { ob_end_clean(); } // Output file readfile($file_path); exit; }, 1); } }, 1);