|
|
|
|
@@ -1,5 +1,4 @@
|
|
|
|
|
import jsPDF from 'jspdf';
|
|
|
|
|
import autoTable from 'jspdf-autotable';
|
|
|
|
|
import QRCode from 'qrcode';
|
|
|
|
|
|
|
|
|
|
interface ScheduleInfo {
|
|
|
|
|
@@ -30,12 +29,24 @@ interface PassengerVoucherData {
|
|
|
|
|
createdAt: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ─── shared drawing helpers ───────────────────────────────────────────────────
|
|
|
|
|
// ─── palette ───────────────────────────────────────────────────────────────
|
|
|
|
|
// A restrained, mostly-neutral palette (ink / slate / hairline / surface) with the
|
|
|
|
|
// brand green reserved for the few elements that should draw the eye — the PNR,
|
|
|
|
|
// times, and the fare — rather than tinting large areas of the page.
|
|
|
|
|
|
|
|
|
|
const PRIMARY = [20, 113, 76] as const;
|
|
|
|
|
const DARK = [51, 51, 51] as const;
|
|
|
|
|
const MED = [102, 102, 102] as const;
|
|
|
|
|
const LIGHT = [200, 200, 200] as const;
|
|
|
|
|
const BRAND = [20, 113, 76] as const; // brand green — accents only
|
|
|
|
|
const BRAND_SOFT = [235, 245, 240] as const; // pale green tint for subtle fills
|
|
|
|
|
const INK = [24, 28, 33] as const; // headings, high-emphasis text
|
|
|
|
|
const BODY = [71, 85, 105] as const; // slate-600 — body text
|
|
|
|
|
const MUTED = [148, 163, 184] as const; // slate-400 — labels/captions
|
|
|
|
|
const HAIRLINE = [226, 232, 240] as const; // slate-200 — borders/dividers
|
|
|
|
|
const SURFACE = [250, 250, 251] as const; // near-white card fill
|
|
|
|
|
const SUCCESS = [21, 128, 61] as const; // green-700
|
|
|
|
|
const AMBER_TEXT = [146, 64, 14] as const; // amber-800
|
|
|
|
|
const AMBER_FILL = [255, 251, 235] as const; // amber-50
|
|
|
|
|
const AMBER_BORDER = [251, 191, 36] as const; // amber-400
|
|
|
|
|
|
|
|
|
|
const PAGE_MARGIN = 18;
|
|
|
|
|
|
|
|
|
|
// ─── QR code ───────────────────────────────────────────────────────────────
|
|
|
|
|
// Encodes everything a gate scanner needs to verify this specific ticket without
|
|
|
|
|
@@ -63,7 +74,7 @@ async function generateTicketQrDataUrl(data: PassengerVoucherData): Promise<stri
|
|
|
|
|
width: 240,
|
|
|
|
|
margin: 0,
|
|
|
|
|
errorCorrectionLevel: 'M',
|
|
|
|
|
color: { dark: '#0f172a', light: '#ffffff' },
|
|
|
|
|
color: { dark: '#181c21', light: '#ffffff' },
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Failed to generate ticket QR code:', error);
|
|
|
|
|
@@ -71,11 +82,30 @@ async function generateTicketQrDataUrl(data: PassengerVoucherData): Promise<stri
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ─── shared drawing helpers ───────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
function label(doc: jsPDF, text: string, x: number, y: number, opts?: { align?: 'left' | 'right' | 'center'; color?: readonly [number, number, number] }): void {
|
|
|
|
|
doc.setFont('helvetica', 'normal');
|
|
|
|
|
doc.setFontSize(7.5);
|
|
|
|
|
const c = opts?.color ?? MUTED;
|
|
|
|
|
doc.setTextColor(c[0], c[1], c[2]);
|
|
|
|
|
doc.text(text.toUpperCase(), x, y, { align: opts?.align ?? 'left', charSpace: 0.3 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function hairline(doc: jsPDF, x1: number, y: number, x2: number): void {
|
|
|
|
|
doc.setDrawColor(...HAIRLINE);
|
|
|
|
|
doc.setLineWidth(0.25);
|
|
|
|
|
doc.line(x1, y, x2, y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ─── header ────────────────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
async function drawHeader(doc: jsPDF, margin: number): Promise<number> {
|
|
|
|
|
const pageWidth = doc.internal.pageSize.getWidth();
|
|
|
|
|
const bandHeight = 24;
|
|
|
|
|
|
|
|
|
|
doc.setFillColor(...PRIMARY);
|
|
|
|
|
doc.rect(0, 0, pageWidth, 30, 'F');
|
|
|
|
|
doc.setFillColor(...BRAND);
|
|
|
|
|
doc.rect(0, 0, pageWidth, bandHeight, 'F');
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const logoImg = await fetch('/edr-logo.png');
|
|
|
|
|
@@ -87,206 +117,243 @@ async function drawHeader(doc: jsPDF, margin: number): Promise<number> {
|
|
|
|
|
});
|
|
|
|
|
const img = new Image();
|
|
|
|
|
await new Promise((resolve) => { img.onload = resolve; img.src = logoDataUrl; });
|
|
|
|
|
const logoH = 18;
|
|
|
|
|
const logoH = 13;
|
|
|
|
|
const logoW = (img.width / img.height) * logoH;
|
|
|
|
|
doc.addImage(logoDataUrl, 'PNG', margin, 6, logoW, logoH);
|
|
|
|
|
const textX = margin + logoW + 5;
|
|
|
|
|
doc.addImage(logoDataUrl, 'PNG', margin, (bandHeight - logoH) / 2, logoW, logoH);
|
|
|
|
|
doc.setTextColor(255, 255, 255);
|
|
|
|
|
doc.setFontSize(18); doc.setFont('helvetica', 'bold');
|
|
|
|
|
doc.text('ETHIO-DJIBOUTI RAILWAY', margin + logoW + 5, 14);
|
|
|
|
|
doc.setFontSize(9); doc.setFont('helvetica', 'normal');
|
|
|
|
|
doc.text('Premium Travel Experience', margin + logoW + 5, 20);
|
|
|
|
|
doc.setFontSize(13); doc.setFont('helvetica', 'bold');
|
|
|
|
|
doc.text('ETHIO-DJIBOUTI RAILWAY', textX, bandHeight / 2 - 1);
|
|
|
|
|
doc.setFontSize(7.5); doc.setFont('helvetica', 'normal');
|
|
|
|
|
doc.setTextColor(230, 240, 236);
|
|
|
|
|
doc.text('E-TICKET · BOARDING VOUCHER', textX, bandHeight / 2 + 5, { charSpace: 0.4 });
|
|
|
|
|
} catch {
|
|
|
|
|
doc.setTextColor(255, 255, 255);
|
|
|
|
|
doc.setFontSize(22); doc.setFont('helvetica', 'bold');
|
|
|
|
|
doc.text('ETHIO-DJIBOUTI RAILWAY', pageWidth / 2, 13, { align: 'center' });
|
|
|
|
|
doc.setFontSize(9); doc.setFont('helvetica', 'normal');
|
|
|
|
|
doc.text('Premium Travel Experience', pageWidth / 2, 20, { align: 'center' });
|
|
|
|
|
doc.setFontSize(15); doc.setFont('helvetica', 'bold');
|
|
|
|
|
doc.text('ETHIO-DJIBOUTI RAILWAY', pageWidth / 2, bandHeight / 2 - 1, { align: 'center' });
|
|
|
|
|
doc.setFontSize(7.5); doc.setFont('helvetica', 'normal');
|
|
|
|
|
doc.setTextColor(230, 240, 236);
|
|
|
|
|
doc.text('E-TICKET · BOARDING VOUCHER', pageWidth / 2, bandHeight / 2 + 5, { align: 'center', charSpace: 0.4 });
|
|
|
|
|
}
|
|
|
|
|
return 40;
|
|
|
|
|
return bandHeight + 16;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function drawStatusBadge(doc: jsPDF, status: string, y: number, pageWidth: number): number {
|
|
|
|
|
const label = (status === 'TICKETED' || status === 'CONFIRMED') ? 'CONFIRMED' : status;
|
|
|
|
|
const color = (status === 'TICKETED' || status === 'CONFIRMED') ? [34, 197, 94] : [234, 179, 8];
|
|
|
|
|
// ─── status pill ───────────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
function drawStatusPill(doc: jsPDF, status: string, x: number, y: number, align: 'left' | 'right' = 'right'): void {
|
|
|
|
|
const isConfirmed = status === 'TICKETED' || status === 'CONFIRMED';
|
|
|
|
|
const text = isConfirmed ? 'CONFIRMED' : status;
|
|
|
|
|
const color = isConfirmed ? SUCCESS : [180, 83, 9] as const;
|
|
|
|
|
|
|
|
|
|
doc.setFontSize(7.5); doc.setFont('helvetica', 'bold');
|
|
|
|
|
const textWidth = doc.getTextWidth(text.toUpperCase());
|
|
|
|
|
const padX = 3.5;
|
|
|
|
|
const pillH = 5.5;
|
|
|
|
|
const pillW = textWidth + padX * 2;
|
|
|
|
|
const pillX = align === 'right' ? x - pillW : x;
|
|
|
|
|
|
|
|
|
|
doc.setFillColor(color[0], color[1], color[2]);
|
|
|
|
|
doc.rect(pageWidth / 2 - 22, y - 4, 44, 8, 'F');
|
|
|
|
|
doc.roundedRect(pillX, y, pillW, pillH, pillH / 2, pillH / 2, 'F');
|
|
|
|
|
doc.setTextColor(255, 255, 255);
|
|
|
|
|
doc.setFontSize(9); doc.setFont('helvetica', 'bold');
|
|
|
|
|
doc.text(label, pageWidth / 2, y + 1, { align: 'center' });
|
|
|
|
|
return y + 12;
|
|
|
|
|
doc.text(text.toUpperCase(), pillX + pillW / 2, y + pillH / 2 + 1.4, { align: 'center', charSpace: 0.3 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function drawBookingRefBox(doc: jsPDF, bookingRef: string, ticketNumber: string, qrDataUrl: string | null, y: number, margin: number, pageWidth: number): number {
|
|
|
|
|
const boxHeight = 32;
|
|
|
|
|
const qrSize = 24;
|
|
|
|
|
const qrPad = 3;
|
|
|
|
|
const qrBlockWidth = qrDataUrl ? qrSize + qrPad * 2 + 5 : 0;
|
|
|
|
|
// ─── hero card: PNR + ticket number + QR ──────────────────────────────────
|
|
|
|
|
|
|
|
|
|
doc.setFillColor(245, 245, 245);
|
|
|
|
|
doc.roundedRect(margin, y, pageWidth - margin * 2, boxHeight, 2, 2, 'F');
|
|
|
|
|
function drawTicketHero(doc: jsPDF, bookingRef: string, ticketNumber: string, status: string, qrDataUrl: string | null, y: number, margin: number, pageWidth: number): number {
|
|
|
|
|
const cardH = 32;
|
|
|
|
|
const qrSize = 22;
|
|
|
|
|
const qrPad = 2.5;
|
|
|
|
|
const cardSize = qrSize + qrPad * 2;
|
|
|
|
|
|
|
|
|
|
// Booking reference (top-left)
|
|
|
|
|
doc.setTextColor(...MED); doc.setFontSize(8); doc.setFont('helvetica', 'normal');
|
|
|
|
|
doc.text('BOOKING REFERENCE', margin + 5, y + 8);
|
|
|
|
|
doc.setTextColor(...PRIMARY); doc.setFontSize(18); doc.setFont('helvetica', 'bold');
|
|
|
|
|
doc.text(bookingRef, margin + 5, y + 18);
|
|
|
|
|
doc.setFillColor(...SURFACE);
|
|
|
|
|
doc.setDrawColor(...HAIRLINE);
|
|
|
|
|
doc.setLineWidth(0.3);
|
|
|
|
|
doc.roundedRect(margin, y, pageWidth - margin * 2, cardH, 3, 3, 'FD');
|
|
|
|
|
|
|
|
|
|
// Ticket number, stacked below — leaves room on the right for the QR block
|
|
|
|
|
const textRightBound = pageWidth - margin - qrBlockWidth - 5;
|
|
|
|
|
doc.setTextColor(...MED); doc.setFontSize(8); doc.setFont('helvetica', 'normal');
|
|
|
|
|
doc.text('TICKET NUMBER', textRightBound, y + 8, { align: 'right' });
|
|
|
|
|
doc.setTextColor(...DARK); doc.setFontSize(11); doc.setFont('helvetica', 'bold');
|
|
|
|
|
doc.text(ticketNumber, textRightBound, y + 16, { align: 'right' });
|
|
|
|
|
const padX = 7;
|
|
|
|
|
drawStatusPill(doc, status, pageWidth - margin - padX, y + 5.5, 'right');
|
|
|
|
|
|
|
|
|
|
label(doc, 'Booking reference', margin + padX, y + 12);
|
|
|
|
|
doc.setTextColor(...INK); doc.setFontSize(21); doc.setFont('helvetica', 'bold');
|
|
|
|
|
doc.text(bookingRef, margin + padX, y + 23, { charSpace: 0.6 });
|
|
|
|
|
|
|
|
|
|
label(doc, 'Ticket no.', margin + padX, y + 28.5);
|
|
|
|
|
doc.setTextColor(...BODY); doc.setFontSize(9); doc.setFont('helvetica', 'normal');
|
|
|
|
|
doc.text(ticketNumber, margin + padX + 22, y + 28.7);
|
|
|
|
|
|
|
|
|
|
// QR code — clean white card with a thin border, right-aligned in the box
|
|
|
|
|
if (qrDataUrl) {
|
|
|
|
|
const cardSize = qrSize + qrPad * 2;
|
|
|
|
|
const cardX = pageWidth - margin - cardSize - 3;
|
|
|
|
|
const cardY = y + (boxHeight - cardSize) / 2;
|
|
|
|
|
const cardY = y + (cardH - cardSize) / 2;
|
|
|
|
|
doc.setFillColor(255, 255, 255);
|
|
|
|
|
doc.setDrawColor(...LIGHT);
|
|
|
|
|
doc.setLineWidth(0.4);
|
|
|
|
|
doc.setDrawColor(...HAIRLINE);
|
|
|
|
|
doc.setLineWidth(0.3);
|
|
|
|
|
doc.roundedRect(cardX, cardY, cardSize, cardSize, 2, 2, 'FD');
|
|
|
|
|
doc.addImage(qrDataUrl, 'PNG', cardX + qrPad, cardY + qrPad, qrSize, qrSize);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return y + boxHeight + 6;
|
|
|
|
|
return y + cardH + 10;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function drawJourneyLeg(doc: jsPDF, schedule: ScheduleInfo, label: string | null, y: number, margin: number, pageWidth: number): number {
|
|
|
|
|
doc.setTextColor(...DARK); doc.setFontSize(11); doc.setFont('helvetica', 'bold');
|
|
|
|
|
doc.text(label ? `JOURNEY DETAILS — ${label.toUpperCase()}` : 'JOURNEY DETAILS', margin, y);
|
|
|
|
|
y += 7;
|
|
|
|
|
// ─── journey card ──────────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
doc.setDrawColor(...LIGHT); doc.setLineWidth(0.5);
|
|
|
|
|
doc.rect(margin, y, pageWidth - margin * 2, 40);
|
|
|
|
|
function drawJourneyLeg(doc: jsPDF, schedule: ScheduleInfo, legLabel: string | null, y: number, margin: number, pageWidth: number): number {
|
|
|
|
|
const cardW = pageWidth - margin * 2;
|
|
|
|
|
const routeH = 30;
|
|
|
|
|
const trainRowH = 9;
|
|
|
|
|
const cardH = routeH + trainRowH;
|
|
|
|
|
|
|
|
|
|
// Origin
|
|
|
|
|
doc.setFontSize(8); doc.setTextColor(...MED); doc.setFont('helvetica', 'normal');
|
|
|
|
|
doc.text('FROM', margin + 5, y + 6);
|
|
|
|
|
doc.setFontSize(15); doc.setTextColor(...DARK); doc.setFont('helvetica', 'bold');
|
|
|
|
|
doc.text(schedule.origin.code, margin + 5, y + 14);
|
|
|
|
|
doc.setFontSize(9); doc.setFont('helvetica', 'normal');
|
|
|
|
|
doc.text(schedule.origin.name, margin + 5, y + 20);
|
|
|
|
|
doc.setFontSize(8); doc.setTextColor(...MED);
|
|
|
|
|
doc.text(schedule.origin.city, margin + 5, y + 25);
|
|
|
|
|
doc.setDrawColor(...HAIRLINE);
|
|
|
|
|
doc.setLineWidth(0.3);
|
|
|
|
|
doc.roundedRect(margin, y, cardW, cardH, 3, 3, 'D');
|
|
|
|
|
|
|
|
|
|
const dep = new Date(schedule.departureAt);
|
|
|
|
|
doc.setFontSize(13); doc.setTextColor(...PRIMARY); doc.setFont('helvetica', 'bold');
|
|
|
|
|
doc.text(dep.toLocaleTimeString('en-US', { hour: 'numeric', minute: '2-digit', hour12: true }), margin + 5, y + 33);
|
|
|
|
|
doc.setFontSize(7); doc.setTextColor(...MED); doc.setFont('helvetica', 'normal');
|
|
|
|
|
doc.text(dep.toLocaleDateString('en-US', { weekday: 'short', month: 'short', day: 'numeric', year: 'numeric' }), margin + 5, y + 38);
|
|
|
|
|
|
|
|
|
|
// Arrow
|
|
|
|
|
doc.setDrawColor(...PRIMARY); doc.setLineWidth(0.8);
|
|
|
|
|
const ax = pageWidth / 2, ay = y + 20;
|
|
|
|
|
doc.line(ax - 10, ay, ax + 10, ay);
|
|
|
|
|
doc.line(ax + 10, ay, ax + 7, ay - 2);
|
|
|
|
|
doc.line(ax + 10, ay, ax + 7, ay + 2);
|
|
|
|
|
|
|
|
|
|
// Destination
|
|
|
|
|
const dx = pageWidth - margin - 50;
|
|
|
|
|
doc.setFontSize(8); doc.setTextColor(...MED); doc.setFont('helvetica', 'normal');
|
|
|
|
|
doc.text('TO', dx, y + 6);
|
|
|
|
|
doc.setFontSize(15); doc.setTextColor(...DARK); doc.setFont('helvetica', 'bold');
|
|
|
|
|
doc.text(schedule.destination.code, dx, y + 14);
|
|
|
|
|
doc.setFontSize(9); doc.setFont('helvetica', 'normal');
|
|
|
|
|
doc.text(schedule.destination.name, dx, y + 20);
|
|
|
|
|
doc.setFontSize(8); doc.setTextColor(...MED);
|
|
|
|
|
doc.text(schedule.destination.city, dx, y + 25);
|
|
|
|
|
|
|
|
|
|
const arr = new Date(schedule.arrivalAt);
|
|
|
|
|
doc.setFontSize(13); doc.setTextColor(...PRIMARY); doc.setFont('helvetica', 'bold');
|
|
|
|
|
doc.text(arr.toLocaleTimeString('en-US', { hour: 'numeric', minute: '2-digit', hour12: true }), dx, y + 33);
|
|
|
|
|
doc.setFontSize(7); doc.setTextColor(...MED); doc.setFont('helvetica', 'normal');
|
|
|
|
|
doc.text(arr.toLocaleDateString('en-US', { weekday: 'short', month: 'short', day: 'numeric', year: 'numeric' }), dx, y + 38);
|
|
|
|
|
|
|
|
|
|
y += 47;
|
|
|
|
|
|
|
|
|
|
// Train info bar
|
|
|
|
|
doc.setFillColor(248, 248, 248);
|
|
|
|
|
doc.rect(margin, y, pageWidth - margin * 2, 12, 'F');
|
|
|
|
|
doc.setFontSize(8); doc.setTextColor(...MED); doc.setFont('helvetica', 'normal');
|
|
|
|
|
doc.text('TRAIN', margin + 5, y + 5);
|
|
|
|
|
doc.setTextColor(...DARK); doc.setFont('helvetica', 'bold');
|
|
|
|
|
doc.text(schedule.trainNumber + (schedule.trainName ? ` — ${schedule.trainName}` : ''), margin + 20, y + 9);
|
|
|
|
|
if (schedule.seatClass) {
|
|
|
|
|
doc.setFont('helvetica', 'normal'); doc.setTextColor(...MED);
|
|
|
|
|
doc.text(schedule.seatClass, pageWidth - margin - 5, y + 9, { align: 'right' });
|
|
|
|
|
if (legLabel) {
|
|
|
|
|
doc.setFillColor(...BRAND);
|
|
|
|
|
doc.roundedRect(margin + 6, y - 3, doc.getTextWidth(legLabel.toUpperCase()) + 7, 6, 3, 3, 'F');
|
|
|
|
|
doc.setTextColor(255, 255, 255); doc.setFontSize(7.5); doc.setFont('helvetica', 'bold');
|
|
|
|
|
doc.text(legLabel.toUpperCase(), margin + 6 + (doc.getTextWidth(legLabel.toUpperCase()) + 7) / 2, y, { align: 'center', charSpace: 0.3 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return y + 18;
|
|
|
|
|
const padX = 8;
|
|
|
|
|
const topY = y + (legLabel ? 12 : 8);
|
|
|
|
|
|
|
|
|
|
// Origin block
|
|
|
|
|
label(doc, 'From', margin + padX, topY);
|
|
|
|
|
doc.setTextColor(...INK); doc.setFontSize(16); doc.setFont('helvetica', 'bold');
|
|
|
|
|
doc.text(schedule.origin.code, margin + padX, topY + 8);
|
|
|
|
|
doc.setTextColor(...BODY); doc.setFontSize(8.5); doc.setFont('helvetica', 'normal');
|
|
|
|
|
doc.text(schedule.origin.city || schedule.origin.name, margin + padX, topY + 13);
|
|
|
|
|
|
|
|
|
|
const dep = new Date(schedule.departureAt);
|
|
|
|
|
doc.setTextColor(...BRAND); doc.setFontSize(11.5); doc.setFont('helvetica', 'bold');
|
|
|
|
|
doc.text(dep.toLocaleTimeString('en-US', { hour: 'numeric', minute: '2-digit', hour12: true }), margin + padX, topY + 20.5);
|
|
|
|
|
doc.setTextColor(...MUTED); doc.setFontSize(7); doc.setFont('helvetica', 'normal');
|
|
|
|
|
doc.text(dep.toLocaleDateString('en-US', { weekday: 'short', month: 'short', day: 'numeric' }), margin + padX, topY + 25);
|
|
|
|
|
|
|
|
|
|
// Destination block (right-aligned)
|
|
|
|
|
const dx = pageWidth - margin - padX;
|
|
|
|
|
label(doc, 'To', dx, topY, { align: 'right' });
|
|
|
|
|
doc.setTextColor(...INK); doc.setFontSize(16); doc.setFont('helvetica', 'bold');
|
|
|
|
|
doc.text(schedule.destination.code, dx, topY + 8, { align: 'right' });
|
|
|
|
|
doc.setTextColor(...BODY); doc.setFontSize(8.5); doc.setFont('helvetica', 'normal');
|
|
|
|
|
doc.text(schedule.destination.city || schedule.destination.name, dx, topY + 13, { align: 'right' });
|
|
|
|
|
|
|
|
|
|
const arr = new Date(schedule.arrivalAt);
|
|
|
|
|
doc.setTextColor(...BRAND); doc.setFontSize(11.5); doc.setFont('helvetica', 'bold');
|
|
|
|
|
doc.text(arr.toLocaleTimeString('en-US', { hour: 'numeric', minute: '2-digit', hour12: true }), dx, topY + 20.5, { align: 'right' });
|
|
|
|
|
doc.setTextColor(...MUTED); doc.setFontSize(7); doc.setFont('helvetica', 'normal');
|
|
|
|
|
doc.text(arr.toLocaleDateString('en-US', { weekday: 'short', month: 'short', day: 'numeric' }), dx, topY + 25, { align: 'right' });
|
|
|
|
|
|
|
|
|
|
// Dashed route line with endpoint markers, connecting the two blocks
|
|
|
|
|
const lineY = topY + 8.5;
|
|
|
|
|
const lineX1 = margin + padX + 24;
|
|
|
|
|
const lineX2 = dx - 24;
|
|
|
|
|
doc.setDrawColor(...HAIRLINE);
|
|
|
|
|
doc.setLineWidth(0.5);
|
|
|
|
|
doc.setLineDashPattern([1, 1.2], 0);
|
|
|
|
|
doc.line(lineX1, lineY, lineX2, lineY);
|
|
|
|
|
doc.setLineDashPattern([], 0);
|
|
|
|
|
doc.setFillColor(...BRAND);
|
|
|
|
|
doc.circle(lineX1, lineY, 0.9, 'F');
|
|
|
|
|
doc.circle(lineX2, lineY, 0.9, 'F');
|
|
|
|
|
|
|
|
|
|
// Train info sub-row
|
|
|
|
|
const rowY = y + routeH;
|
|
|
|
|
hairline(doc, margin, rowY, margin + cardW);
|
|
|
|
|
doc.setFontSize(8); doc.setTextColor(...MUTED); doc.setFont('helvetica', 'normal');
|
|
|
|
|
doc.text('TRAIN', margin + padX, rowY + 6, { charSpace: 0.3 });
|
|
|
|
|
doc.setTextColor(...INK); doc.setFont('helvetica', 'bold');
|
|
|
|
|
doc.text(schedule.trainNumber + (schedule.trainName ? ` · ${schedule.trainName}` : ''), margin + padX + 15, rowY + 6);
|
|
|
|
|
if (schedule.seatClass) {
|
|
|
|
|
doc.setFont('helvetica', 'normal'); doc.setTextColor(...BODY);
|
|
|
|
|
doc.text(schedule.seatClass, pageWidth - margin - padX, rowY + 6, { align: 'right' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return y + cardH + 8;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function drawPassengerDetails(doc: jsPDF, data: PassengerVoucherData, y: number, margin: number): number {
|
|
|
|
|
doc.setTextColor(...DARK); doc.setFontSize(11); doc.setFont('helvetica', 'bold');
|
|
|
|
|
doc.text('PASSENGER DETAILS', margin, y);
|
|
|
|
|
// ─── passenger details ─────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
function drawPassengerDetails(doc: jsPDF, data: PassengerVoucherData, y: number, margin: number, pageWidth: number): number {
|
|
|
|
|
label(doc, 'Passenger details', margin, y);
|
|
|
|
|
y += 7;
|
|
|
|
|
|
|
|
|
|
const rows: [string, string][] = [
|
|
|
|
|
['Full Name', data.passengerName || '—'],
|
|
|
|
|
['Date of Birth', data.dateOfBirth ? new Date(data.dateOfBirth).toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' }) : '—'],
|
|
|
|
|
['Full name', data.passengerName || '—'],
|
|
|
|
|
['Date of birth', data.dateOfBirth ? new Date(data.dateOfBirth).toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' }) : '—'],
|
|
|
|
|
['Nationality', data.nationality || '—'],
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if (data.isRoundTrip) {
|
|
|
|
|
rows.push(['Outbound Seat', data.outboundSeatNumber || '—']);
|
|
|
|
|
rows.push(['Return Seat', data.inboundSeatNumber || '—']);
|
|
|
|
|
rows.push(['Outbound seat', data.outboundSeatNumber || '—']);
|
|
|
|
|
rows.push(['Return seat', data.inboundSeatNumber || '—']);
|
|
|
|
|
} else {
|
|
|
|
|
rows.push(['Seat', data.seatNumber || '—']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
autoTable(doc, {
|
|
|
|
|
startY: y,
|
|
|
|
|
body: rows,
|
|
|
|
|
theme: 'plain',
|
|
|
|
|
styles: { fontSize: 9, cellPadding: 3 },
|
|
|
|
|
columnStyles: {
|
|
|
|
|
0: { fontStyle: 'bold', textColor: [MED[0], MED[1], MED[2]], cellWidth: 45 },
|
|
|
|
|
1: { textColor: [DARK[0], DARK[1], DARK[2]] },
|
|
|
|
|
},
|
|
|
|
|
alternateRowStyles: { fillColor: [248, 248, 248] },
|
|
|
|
|
margin: { left: margin, right: margin },
|
|
|
|
|
const rowH = 8;
|
|
|
|
|
rows.forEach(([k, v], i) => {
|
|
|
|
|
const rowY = y + i * rowH;
|
|
|
|
|
doc.setFontSize(8.5); doc.setTextColor(...MUTED); doc.setFont('helvetica', 'normal');
|
|
|
|
|
doc.text(k.toUpperCase(), margin, rowY + 5, { charSpace: 0.2 });
|
|
|
|
|
doc.setFontSize(9.5); doc.setTextColor(...INK); doc.setFont('helvetica', 'bold');
|
|
|
|
|
doc.text(v, pageWidth - margin, rowY + 5, { align: 'right' });
|
|
|
|
|
if (i < rows.length - 1) hairline(doc, margin, rowY + rowH, pageWidth - margin);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return (doc as any).lastAutoTable.finalY + 8;
|
|
|
|
|
return y + rows.length * rowH + 6;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ─── fare summary ──────────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
function drawFareSummary(doc: jsPDF, fareMinor: number, currency: string, y: number, margin: number, pageWidth: number): number {
|
|
|
|
|
doc.setFillColor(248, 248, 248);
|
|
|
|
|
doc.rect(margin, y, pageWidth - margin * 2, 20, 'F');
|
|
|
|
|
doc.setFontSize(9); doc.setTextColor(...MED); doc.setFont('helvetica', 'normal');
|
|
|
|
|
doc.text('Fare', margin + 5, y + 7);
|
|
|
|
|
doc.setFontSize(15); doc.setTextColor(...PRIMARY); doc.setFont('helvetica', 'bold');
|
|
|
|
|
doc.text(`${currency} ${(fareMinor / 100).toFixed(2)}`, pageWidth - margin - 5, y + 7, { align: 'right' });
|
|
|
|
|
doc.setFontSize(9); doc.setTextColor(34, 197, 94); doc.setFont('helvetica', 'bold');
|
|
|
|
|
doc.text('✓ PAID', margin + 5, y + 15);
|
|
|
|
|
return y + 26;
|
|
|
|
|
const cardH = 20;
|
|
|
|
|
doc.setFillColor(...BRAND_SOFT);
|
|
|
|
|
doc.roundedRect(margin, y, pageWidth - margin * 2, cardH, 3, 3, 'F');
|
|
|
|
|
|
|
|
|
|
const padX = 7;
|
|
|
|
|
label(doc, 'Total fare paid', margin + padX, y + 8, { color: BODY });
|
|
|
|
|
doc.setFontSize(7.5); doc.setFont('helvetica', 'bold'); doc.setTextColor(...SUCCESS);
|
|
|
|
|
doc.text('✓ PAID', margin + padX, y + 15);
|
|
|
|
|
|
|
|
|
|
doc.setTextColor(...BRAND); doc.setFontSize(16); doc.setFont('helvetica', 'bold');
|
|
|
|
|
doc.text(`${currency} ${(fareMinor / 100).toFixed(2)}`, pageWidth - margin - padX, y + 13, { align: 'right' });
|
|
|
|
|
|
|
|
|
|
return y + cardH + 8;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ─── instructions ──────────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
function drawInstructions(doc: jsPDF, y: number, margin: number, pageWidth: number): number {
|
|
|
|
|
doc.setFillColor(252, 211, 77);
|
|
|
|
|
doc.rect(margin, y, pageWidth - margin * 2, 18, 'F');
|
|
|
|
|
doc.setFontSize(9); doc.setTextColor(...DARK); doc.setFont('helvetica', 'bold');
|
|
|
|
|
doc.text('⚠ IMPORTANT INSTRUCTIONS', margin + 5, y + 6);
|
|
|
|
|
const cardH = 17;
|
|
|
|
|
const barW = 1.4;
|
|
|
|
|
|
|
|
|
|
doc.setFillColor(...AMBER_FILL);
|
|
|
|
|
doc.roundedRect(margin, y, pageWidth - margin * 2, cardH, 2, 2, 'F');
|
|
|
|
|
doc.setFillColor(...AMBER_BORDER);
|
|
|
|
|
doc.rect(margin, y, barW, cardH, 'F');
|
|
|
|
|
|
|
|
|
|
const padX = 6;
|
|
|
|
|
doc.setFontSize(8); doc.setTextColor(...AMBER_TEXT); doc.setFont('helvetica', 'bold');
|
|
|
|
|
doc.text('BEFORE YOU TRAVEL', margin + padX, y + 6, { charSpace: 0.3 });
|
|
|
|
|
doc.setFont('helvetica', 'normal'); doc.setFontSize(8);
|
|
|
|
|
doc.text('• Present this voucher at the terminal for boarding', margin + 5, y + 11);
|
|
|
|
|
doc.text('• Arrive at least 30 minutes before departure', margin + 5, y + 15);
|
|
|
|
|
return y + 24;
|
|
|
|
|
doc.text('Present this voucher (printed or on your phone) at the terminal for boarding.', margin + padX, y + 11);
|
|
|
|
|
doc.text('Please arrive at least 30 minutes before scheduled departure.', margin + padX, y + 15);
|
|
|
|
|
|
|
|
|
|
return y + cardH + 6;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ─── footer ────────────────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
function drawFooter(doc: jsPDF, createdAt: string): void {
|
|
|
|
|
const pageWidth = doc.internal.pageSize.getWidth();
|
|
|
|
|
const pageHeight = doc.internal.pageSize.getHeight();
|
|
|
|
|
const footerY = pageHeight - 22;
|
|
|
|
|
const footerY = pageHeight - 20;
|
|
|
|
|
|
|
|
|
|
doc.setDrawColor(...LIGHT);
|
|
|
|
|
doc.line(15, footerY, pageWidth - 15, footerY);
|
|
|
|
|
doc.setFontSize(8); doc.setTextColor(...MED); doc.setFont('helvetica', 'normal');
|
|
|
|
|
doc.text('Support: support@edr.com | +251-11-XXX-XXXX', pageWidth / 2, footerY + 5, { align: 'center' });
|
|
|
|
|
doc.text('Terms & Conditions apply. Visit www.edr.com for details.', pageWidth / 2, footerY + 9, { align: 'center' });
|
|
|
|
|
doc.setFontSize(7);
|
|
|
|
|
doc.text(`Generated: ${new Date(createdAt).toLocaleString('en-US')}`, pageWidth / 2, footerY + 13, { align: 'center' });
|
|
|
|
|
hairline(doc, PAGE_MARGIN, footerY, pageWidth - PAGE_MARGIN);
|
|
|
|
|
doc.setFontSize(7.5); doc.setTextColor(...MUTED); doc.setFont('helvetica', 'normal');
|
|
|
|
|
doc.text('support@edr.com · +251-11-XXX-XXXX · www.edr.com', pageWidth / 2, footerY + 6, { align: 'center' });
|
|
|
|
|
doc.setFontSize(6.5);
|
|
|
|
|
doc.text(`Issued ${new Date(createdAt).toLocaleString('en-US')}`, pageWidth / 2, footerY + 10.5, { align: 'center' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ─── public API ──────────────────────────────────────────────────────────────
|
|
|
|
|
@@ -295,25 +362,22 @@ function drawFooter(doc: jsPDF, createdAt: string): void {
|
|
|
|
|
export const generatePassengerVoucherPDF = async (data: PassengerVoucherData): Promise<void> => {
|
|
|
|
|
const doc = new jsPDF({ orientation: 'portrait', unit: 'mm', format: 'a4' });
|
|
|
|
|
const pageW = doc.internal.pageSize.getWidth();
|
|
|
|
|
const margin = 15;
|
|
|
|
|
const margin = PAGE_MARGIN;
|
|
|
|
|
|
|
|
|
|
let y = await drawHeader(doc, margin);
|
|
|
|
|
const qrDataUrl = await generateTicketQrDataUrl(data);
|
|
|
|
|
|
|
|
|
|
// Title
|
|
|
|
|
doc.setTextColor(...DARK); doc.setFontSize(18); doc.setFont('helvetica', 'bold');
|
|
|
|
|
doc.text('PASSENGER VOUCHER', pageW / 2, y, { align: 'center' });
|
|
|
|
|
y += 10;
|
|
|
|
|
y = drawTicketHero(doc, data.bookingRef, data.ticketNumber, data.status, qrDataUrl, y, margin, pageW);
|
|
|
|
|
|
|
|
|
|
y = drawStatusBadge(doc, data.status, y, pageW);
|
|
|
|
|
y = drawBookingRefBox(doc, data.bookingRef, data.ticketNumber, qrDataUrl, y, margin, pageW);
|
|
|
|
|
label(doc, 'Journey details', margin, y);
|
|
|
|
|
y += 7;
|
|
|
|
|
y = drawJourneyLeg(doc, data.outboundSchedule, data.isRoundTrip ? 'Outbound' : null, y, margin, pageW);
|
|
|
|
|
|
|
|
|
|
if (data.isRoundTrip && data.inboundSchedule) {
|
|
|
|
|
y = drawJourneyLeg(doc, data.inboundSchedule, 'Return', y, margin, pageW);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
y = drawPassengerDetails(doc, data, y, margin);
|
|
|
|
|
y = drawPassengerDetails(doc, data, y, margin, pageW);
|
|
|
|
|
y = drawFareSummary(doc, data.fareMinor, data.currency, y, margin, pageW);
|
|
|
|
|
drawInstructions(doc, y, margin, pageW);
|
|
|
|
|
drawFooter(doc, data.createdAt);
|
|
|
|
|
|