`).join('');
resultsOutput.classList.remove('hidden');
downloadPdfBtn.disabled = false;
}
async function handlePdfDownload() {
if (!analysisDataForPdf) {
alert("Please generate an analysis first.");
return;
}
// Populate the hidden div
document.getElementById('pdf-optimized').textContent = analysisDataForPdf.optimizedLetter;
document.getElementById('pdf-job-desc').textContent = jobDescInput.value;
document.getElementById('pdf-cover-letter').textContent = coverLetterInput.value;
document.getElementById('pdf-suggestions').innerHTML = analysisDataForPdf.suggestions.map(item =>
`${item.suggestion}: ${item.reason} `
).join('');
const contentToPrint = document.getElementById('pdf-output');
contentToPrint.style.display = 'block';
try {
const canvas = await html2canvas(contentToPrint, { scale: 2 });
const { jsPDF } = window.jspdf;
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF({ orientation: 'portrait', unit: 'pt', format: 'a4' });
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = (canvas.height * pdfWidth) / canvas.width;
pdf.addImage(imgData, 'PNG', 0, 0, pdfWidth, pdfHeight);
pdf.save('Cover-Letter-Optimization-Report.pdf');
} catch (err) {
console.error("Error generating PDF:", err);
alert("Could not generate PDF.");
} finally {
contentToPrint.style.display = 'none';
}
}
// --- EVENT LISTENERS ---
optimizeBtn.addEventListener('click', handleOptimization);
downloadPdfBtn.addEventListener('click', handlePdfDownload);
// --- INITIALIZATION ---
lucide.createIcons();
});
