import { Button } from '@/components/ui/button'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'; import jsPDF from 'jspdf'; import { Award, Calendar, Download, FileImage, FileText } from 'lucide-react'; import { useRef, useState } from 'react'; import { toast } from 'sonner'; const CertificateGenerator = () => { const [studentName, setStudentName] = useState(''); const [courseName, setCourseName] = useState(''); const [completionDate, setCompletionDate] = useState(''); const [isGenerating, setIsGenerating] = useState(false); const [certificateSize, setCertificateSize] = useState('standard'); const [downloadFormat, setDownloadFormat] = useState('png'); const certificateRef = useRef(null); const handleGenerateCertificate = async () => { if (!studentName || !courseName || !completionDate) { toast.error('Please fill in all required fields.'); return; } setIsGenerating(true); // Simulate certificate generation setTimeout(() => { setIsGenerating(false); toast.success('Your course completion certificate has been created successfully.'); }, 2000); }; const getSizeDimensions = () => { return certificateSize === 'a4' ? { width: 842, height: 595 } // A4 landscape : { width: 800, height: 600 }; // Standard }; const handleDownloadCertificate = () => { if (!certificateRef.current) return; if (downloadFormat === 'pdf') { downloadAsPDF(); } else { downloadAsPNG(); } }; const downloadAsPNG = () => { const canvas = document.createElement('canvas'); const ctx = canvas.getContext('2d'); if (!ctx) return; const dimensions = getSizeDimensions(); canvas.width = dimensions.width; canvas.height = dimensions.height; drawCertificate(ctx, dimensions); canvas.toBlob((blob) => { if (!blob) return; const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = `${studentName}_${courseName}_Certificate.png`; document.body.appendChild(a); a.click(); document.body.removeChild(a); URL.revokeObjectURL(url); toast.success('Your PNG certificate has been saved to your downloads folder.'); }, 'image/png'); }; const downloadAsPDF = () => { const dimensions = getSizeDimensions(); const isLandscape = dimensions.width > dimensions.height; // Create PDF with proper dimensions const pdf = new jsPDF({ orientation: isLandscape ? 'landscape' : 'portrait', unit: 'px', format: [dimensions.width, dimensions.height], }); // Create canvas for drawing const canvas = document.createElement('canvas'); const ctx = canvas.getContext('2d'); if (!ctx) return; canvas.width = dimensions.width; canvas.height = dimensions.height; // Draw certificate on canvas drawCertificate(ctx, dimensions); // Convert canvas to image and add to PDF const imgData = canvas.toDataURL('image/png'); pdf.addImage(imgData, 'PNG', 0, 0, dimensions.width, dimensions.height); // Save the PDF pdf.save(`${studentName}_${courseName}_Certificate.pdf`); toast.success('Your PDF certificate has been saved to your downloads folder.'); }; const drawCertificate = (ctx: CanvasRenderingContext2D, dimensions: { width: number; height: number }) => { // Create gradient background const gradient = ctx.createLinearGradient(0, 0, dimensions.width, dimensions.height); gradient.addColorStop(0, '#dbeafe'); gradient.addColorStop(1, '#e0e7ff'); ctx.fillStyle = gradient; ctx.fillRect(0, 0, dimensions.width, dimensions.height); // Add decorative border ctx.strokeStyle = '#f59e0b'; ctx.lineWidth = 8; ctx.strokeRect(20, 20, dimensions.width - 40, dimensions.height - 40); // Inner border ctx.strokeStyle = '#3730a3'; ctx.lineWidth = 2; ctx.strokeRect(40, 40, dimensions.width - 80, dimensions.height - 80); // Set text styles ctx.fillStyle = '#1f2937'; ctx.textAlign = 'center'; // Title ctx.font = 'bold 42px serif'; ctx.fillText('Certificate of Completion', dimensions.width / 2, 120); // Decorative line under title ctx.strokeStyle = '#f59e0b'; ctx.lineWidth = 3; ctx.beginPath(); ctx.moveTo(dimensions.width / 2 - 150, 140); ctx.lineTo(dimensions.width / 2 + 150, 140); ctx.stroke(); // "This is to certify that" ctx.font = '22px serif'; ctx.fillStyle = '#4b5563'; ctx.fillText('This is to certify that', dimensions.width / 2, 190); // Student name with underline ctx.font = 'bold 36px serif'; ctx.fillStyle = '#3730a3'; ctx.fillText(studentName, dimensions.width / 2, 250); // Underline for student name const nameWidth = ctx.measureText(studentName).width; ctx.strokeStyle = '#f59e0b'; ctx.lineWidth = 3; ctx.beginPath(); ctx.moveTo((dimensions.width - nameWidth) / 2 - 20, 270); ctx.lineTo((dimensions.width + nameWidth) / 2 + 20, 270); ctx.stroke(); // "has successfully completed the course" ctx.font = '22px serif'; ctx.fillStyle = '#4b5563'; ctx.fillText('has successfully completed the course', dimensions.width / 2, 320); // Course name ctx.font = 'bold 28px serif'; ctx.fillStyle = '#3730a3'; ctx.fillText(courseName, dimensions.width / 2, 370); // Completion date ctx.font = '18px serif'; ctx.fillStyle = '#6b7280'; ctx.fillText(`Completed on: ${completionDate}`, dimensions.width / 2, 430); // Footer ctx.font = '16px serif'; ctx.fillStyle = '#9ca3af'; ctx.fillText('Authorized Certificate of Achievement', dimensions.width / 2, dimensions.height - 60); }; const currentDate = new Date().toISOString().split('T')[0]; return (

Course Certificate Generator

Generate your official course completion certificate

{/* Form Section */} Certificate Details
setStudentName(e.target.value)} placeholder="Enter your full name" />
setCourseName(e.target.value)} placeholder="Enter the course name" />
setCompletionDate(e.target.value)} max={currentDate} />
{/* Preview Section */} Certificate Preview
{/* Inner decorative border */}

Certificate of Completion

This is to certify that

{studentName || 'Student Name'}

has successfully completed the course

{courseName || 'Course Name'}

Completed on: {completionDate || 'Date'}

Authorized Certificate of Achievement

{studentName && courseName && completionDate && ( )}
); }; export default CertificateGenerator;