Handballbooking/server/utils/pdf.js
2025-06-04 15:13:40 +02:00

74 lines
No EOL
2 KiB
JavaScript

import PDFDocument from 'pdfkit';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
export async function generateTicketPDF({ ticketId, match, name, email, phone, seats, pdfFile }) {
const doc = new PDFDocument({
size: 'A4',
margin: 50
});
const outputPath = path.join(__dirname, '../uploads', `ticket-${ticketId}.pdf`);
const writeStream = fs.createWriteStream(outputPath);
doc.pipe(writeStream);
// Add logo if exists
const logoPath = path.join(__dirname, '../assets/logo.png');
if (fs.existsSync(logoPath)) {
doc.image(logoPath, 50, 50, { width: 100 });
}
// Add ticket information
doc.fontSize(24).text('Handball Match Ticket', { align: 'center' });
doc.moveDown();
doc.fontSize(16).text(match.name, { align: 'center' });
doc.moveDown();
// Add match details
doc.fontSize(12);
doc.text(`Date: ${new Date(match.date).toLocaleString()}`);
doc.text(`Location: ${match.location}`);
// Add detailed seat information
doc.text('Seats:');
seats.forEach(seat => {
doc.text(` - ${seat.direction || 'Seat'} ${seat.extractedSeatNumber || seat.seatNumber}`);
});
doc.moveDown();
// Add customer details
doc.text('Customer Information:');
doc.text(`Name: ${name}`);
doc.text(`Email: ${email}`);
doc.text(`Phone: ${phone}`);
doc.moveDown();
// Add ticket ID
doc.text(`Ticket ID: ${ticketId}`);
doc.moveDown();
// Add QR code or barcode if needed
// TODO: Add QR code generation
// Add terms and conditions
doc.fontSize(10);
doc.text('Terms and Conditions:', { underline: true });
doc.text('1. This ticket is non-refundable');
doc.text('2. Please arrive at least 30 minutes before the match');
doc.text('3. Present this ticket at the entrance');
// Finalize PDF
doc.end();
return new Promise((resolve, reject) => {
writeStream.on('finish', () => {
resolve(outputPath);
});
writeStream.on('error', reject);
});
}