`;
});
}
resumePreview.innerHTML = html;
};
// --- Event Listeners ---
// Tab navigation
tabButtons.forEach(btn => {
btn.addEventListener('click', () => {
currentTab = parseInt(btn.dataset.tab);
updateTabs();
});
});
// Next/Previous buttons
nextBtn.addEventListener('click', () => {
if (currentTab < 4) {
currentTab++;
updateTabs();
}
});
prevBtn.addEventListener('click', () => {
if (currentTab > 1) {
currentTab--;
updateTabs();
}
});
// Add dynamic blocks
addExperienceBtn.addEventListener('click', addExperienceBlock);
addEducationBtn.addEventListener('click', addEducationBlock);
// Live preview update on any form input
document.getElementById('resume-builder-container').addEventListener('input', updatePreview);
// PDF download functionality
downloadPdfBtn.addEventListener('click', () => {
// Ensure the latest content is captured
updatePreview();
const { jsPDF } = window.jspdf;
const resumeContent = document.getElementById('resume-preview-content');
if (!resumeContent) {
console.error("Resume preview element not found for PDF generation.");
return;
}
html2canvas(resumeContent, {
scale: 2, // Improve resolution
useCORS: true
}).then(canvas => {
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF({
orientation: 'portrait',
unit: 'pt',
format: 'a4'
});
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = pdf.internal.pageSize.getHeight();
const canvasWidth = canvas.width;
const canvasHeight = canvas.height;
const ratio = canvasWidth / canvasHeight;
const imgWidth = pdfWidth - 40; // with margin
const imgHeight = imgWidth / ratio;
let heightLeft = imgHeight;
let position = 20; // top margin
pdf.addImage(imgData, 'PNG', 20, position, imgWidth, imgHeight);
heightLeft -= (pdfHeight - 40);
while (heightLeft > 0) {
position = heightLeft - imgHeight + 20; // Adjust position for new page
pdf.addPage();
pdf.addImage(imgData, 'PNG', 20, position, imgWidth, imgHeight);
heightLeft -= (pdfHeight - 40);
}
const fileName = (fullNameInput.value || "resume").replace(/\s+/g, '_').toLowerCase();
pdf.save(`${fileName}.pdf`);
}).catch(error => {
console.error("Error generating PDF:", error);
});
});
// --- Initialization ---
updateTabs();
addExperienceBlock();
addEducationBlock();
});
