![]() Server : Apache/2 System : Linux server-15-235-50-60 5.15.0-164-generic #174-Ubuntu SMP Fri Nov 14 20:25:16 UTC 2025 x86_64 User : gositeme ( 1004) PHP Version : 8.2.29 Disable Function : exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname Directory : /home/gositeme/backups/lavocat.quebec/backup-20250730-021618/src/pages/api/admin/ |
import type { NextApiRequest, NextApiResponse } from 'next';
import fs from 'fs';
import path from 'path';
import jsPDF from 'jspdf';
import autoTable from 'jspdf-autotable';
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method !== 'GET') {
return res.status(405).json({ error: 'Method not allowed' });
}
try {
const filePath = path.join(process.cwd(), 'registrations.json');
const fileContent = fs.readFileSync(filePath, 'utf-8');
const registrations = JSON.parse(fileContent);
// Sort registrations by date (newest first)
registrations.sort((a: any, b: any) => new Date(b.date).getTime() - new Date(a.date).getTime());
const doc = new jsPDF();
// Add title
doc.setFontSize(16);
doc.text('All Leads (Newest First)', 14, 15);
// Define columns
const columns = [
{ header: 'Date', dataKey: 'date' },
{ header: 'Name', dataKey: 'name' },
{ header: 'Email', dataKey: 'email' },
{ header: 'Phone', dataKey: 'phone' },
{ header: 'Address', dataKey: 'address' },
{ header: 'City', dataKey: 'city' },
{ header: 'State', dataKey: 'state' },
{ header: 'Country', dataKey: 'country' },
{ header: 'Postal Code', dataKey: 'postalCode' },
{ header: 'Status', dataKey: 'status' },
{ header: 'Notes', dataKey: 'notes' },
];
// Add table
autoTable(doc, {
head: [columns.map(col => col.header)],
body: registrations.map((reg: any) => columns.map(col => reg[col.dataKey] || '')),
startY: 20,
styles: { fontSize: 8, cellPadding: 1 },
headStyles: { fillColor: [41, 128, 185], textColor: 255, fontStyle: 'bold' },
alternateRowStyles: { fillColor: [245, 245, 245] },
margin: { top: 20 },
});
// Save the PDF
const pdfBuffer = doc.output('arraybuffer');
res.setHeader('Content-Type', 'application/pdf');
res.setHeader('Content-Disposition', 'attachment; filename=all-leads.pdf');
res.send(Buffer.from(pdfBuffer));
} catch (error) {
console.error('Error exporting PDF:', error);
res.status(500).json({ error: 'Error exporting PDF' });
}
}