`;
memoOutputContainer.innerHTML = memoHTML;
};
const handleDownloadPdf = () => {
const { jsPDF } = window.jspdf;
if (!jsPDF) {
console.error("jsPDF is not loaded.");
return;
}
// 1. Get data from form
const toVal = memoTo.value.trim() || memoTo.placeholder;
const fromVal = memoFrom.value.trim() || memoFrom.placeholder;
const subjectVal = memoSubject.value.trim() || memoSubject.placeholder;
const bodyVal = memoBody.value.trim() || memoBody.placeholder;
let dateVal;
try {
// Use a consistent, clear date format
const dateObj = new Date(memoDate.value);
// Add timezone offset to prevent date from shifting by one day
const utcDate = new Date(dateObj.getTime() + dateObj.getTimezoneOffset() * 60000);
dateVal = utcDate.toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' });
} catch (e) {
dateVal = memoDate.value; // Fallback
}
// 2. Initialize PDF
const doc = new jsPDF({
orientation: 'portrait',
unit: 'in',
format: 'letter'
});
// 3. Define layout variables
const margin = 1;
const pageWidth = doc.internal.pageSize.getWidth();
const usableWidth = pageWidth - (margin * 2);
let currentY = margin;
// 4. Build PDF Content
// --- Header ---
doc.setFont('helvetica', 'bold');
doc.setFontSize(18);
doc.text('MEMORANDUM', pageWidth / 2, currentY, { align: 'center' });
currentY += 0.4; // Move down
doc.setLineWidth(0.01);
doc.line(margin, currentY, pageWidth - margin, currentY); // Top line
currentY += 0.3;
// --- Memo Info Section ---
doc.setFontSize(11);
const infoStartX = margin;
const labelWidth = 0.8;
// TO:
doc.setFont('helvetica', 'bold');
doc.text('TO:', infoStartX, currentY);
doc.setFont('helvetica', 'normal');
doc.text(toVal, infoStartX + labelWidth, currentY);
currentY += 0.25;
// FROM:
doc.setFont('helvetica', 'bold');
doc.text('FROM:', infoStartX, currentY);
doc.setFont('helvetica', 'normal');
doc.text(fromVal, infoStartX + labelWidth, currentY);
currentY += 0.25;
// DATE:
doc.setFont('helvetica', 'bold');
doc.text('DATE:', infoStartX, currentY);
doc.setFont('helvetica', 'normal');
doc.text(dateVal, infoStartX + labelWidth, currentY);
currentY += 0.25;
// SUBJECT:
doc.setFont('helvetica', 'bold');
doc.text('SUBJECT:', infoStartX, currentY);
doc.setFont('helvetica', 'normal');
const subjectLines = doc.splitTextToSize(subjectVal, usableWidth - labelWidth);
doc.text(subjectLines, infoStartX + labelWidth, currentY);
currentY += (subjectLines.length * 0.2) + 0.15;
doc.line(margin, currentY, pageWidth - margin, currentY); // Bottom line of header
currentY += 0.3;
// --- Body Section ---
doc.setFont('times', 'normal');
doc.setFontSize(12);
const bodyLines = doc.splitTextToSize(bodyVal, usableWidth);
doc.text(bodyLines, margin, currentY);
// 5. Save the PDF
doc.save('Corporate-Memo-Report.pdf');
};
// --- Event Listeners ---
tabDetails.addEventListener('click', () => switchTab('details'));
tabPreview.addEventListener('click', () => switchTab('preview'));
nextBtn.addEventListener('click', () => {
if (currentTab === 'details') {
switchTab('preview');
}
});
prevBtn.addEventListener('click', () => {
if (currentTab === 'preview') {
switchTab('details');
}
});
downloadPdfBtn.addEventListener('click', handleDownloadPdf);
});