Employee Information
Employee Name: ${employeeName}
Employee ID: ${employeeId}
Position: ${position}
Department: ${department}
Reviewer: ${reviewerName}
Competency Ratings
| Competency | Rating |
${competencyRatings.map(c => `| ${c.name} | ${c.value} - ${c.text} |
`).join('')}
Goals & Achievements This Period
Areas for Improvement
Goals for Next Period
`;
}
// --- PDF DOWNLOAD ---
window.downloadPDF = () => {
const { jsPDF } = window.jspdf;
const content = document.getElementById('pdf-content');
const downloadBtnContainer = document.getElementById('download-button-container');
if (!content || !downloadBtnContainer) {
console.error("PDF content or button container not found.");
return;
}
const employeeName = document.getElementById('employeeName')?.value || 'report';
const sanitizedFileName = employeeName.replace(/[^a-z0-9]/gi, '_').toLowerCase();
const fileName = `performance_review_${sanitizedFileName}.pdf`;
// Temporarily hide the download button to not capture it in the PDF
downloadBtnContainer.classList.add('pdf-hide');
html2canvas(content, {
scale: 2,
useCORS: true,
logging: false,
windowWidth: content.scrollWidth,
windowHeight: content.scrollHeight
}).then(canvas => {
const imgData = canvas.toDataURL('image/jpeg', 0.9); // Use JPEG for better compression
const pdf = new jsPDF({
orientation: 'portrait',
unit: 'pt', // Use points for better scaling
format: 'a4'
});
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = pdf.internal.pageSize.getHeight();
const canvasWidth = canvas.width;
const canvasHeight = canvas.height;
const canvasAspectRatio = canvasWidth / canvasHeight;
let imgWidth = pdfWidth;
let imgHeight = imgWidth / canvasAspectRatio;
let heightLeft = imgHeight;
let position = 0;
// Add the first page
pdf.addImage(imgData, 'JPEG', 0, position, imgWidth, imgHeight);
heightLeft -= pdfHeight;
// Add new pages if content is taller than one page
while (heightLeft > 0) {
position = heightLeft - imgHeight;
pdf.addPage();
pdf.addImage(imgData, 'JPEG', 0, position, imgWidth, imgHeight);
heightLeft -= pdfHeight;
}
pdf.save(fileName);
// Show the button again
downloadBtnContainer.classList.remove('pdf-hide');
}).catch(error => {
console.error("Error generating PDF:", error);
// Ensure button is shown even if there's an error
downloadBtnContainer.classList.remove('pdf-hide');
});
};
// --- RUN ON LOAD ---
initializeCompetencies();
showTab(1);
});