`;
} else {
letterContent += `
`;
}
// 7. Closing
letterContent += `
`;
generatedRequestDiv.innerHTML = letterContent;
}
// --- EVENT HANDLERS ---
function attachEventListeners() {
// Attach event listener to the main container for input changes
document.getElementById('prrg-tab-config').addEventListener('input', updateGeneratedRequest);
// Navigation
prevBtn.addEventListener('click', () => prrg_changeTab(state.currentTab - 1));
nextBtn.addEventListener('click', () => prrg_changeTab(state.currentTab + 1));
// PDF
document.getElementById('prrg-download-pdf-btn').addEventListener('click', generatePDF);
}
// --- TABS & NAVIGATION ---
window.prrg_changeTab = function(tabIndex) {
if (tabIndex < 0 || tabIndex >= tabButtons.length) return;
state.currentTab = tabIndex;
tabButtons.forEach((b, i) => b.classList.toggle('active', i === tabIndex));
tabContents.forEach((c, i) => c.classList.toggle('active', i === tabIndex));
// Update navigation buttons
prevBtn.style.visibility = state.currentTab === 0 ? 'hidden' : 'visible';
nextBtn.style.visibility = state.currentTab === tabButtons.length - 1 ? 'hidden' : 'visible';
if (tabIndex === 0) {
updateGeneratedRequest();
}
}
// --- PDF GENERATION ---
function generatePDF() {
const { jsPDF } = window.jspdf;
const container = document.getElementById('prrg-container');
const outputWrapper = document.getElementById('prrg-request-output-wrapper');
// Prepare for PDF capture
container.classList.add('prrg-pdf-export-mode');
html2canvas(outputWrapper, { scale: 2 }).then(canvas => {
// Restore original view
container.classList.remove('prrg-pdf-export-mode');
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF('p', 'mm', '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 - 20; // 10mm margin
const imgHeight = imgWidth / ratio;
let heightLeft = imgHeight;
let position = 10; // Top margin
pdf.addImage(imgData, 'PNG', 10, position, imgWidth, imgHeight);
heightLeft -= pdfHeight;
while (heightLeft > -pdfHeight) {
position = imgHeight - (pdfHeight * Math.floor(imgHeight / pdfHeight)); // Calculate position for next page
if (position > 0) position -= (pdfHeight * Math.floor(imgHeight / pdfHeight));
pdf.addPage();
pdf.addImage(imgData, 'PNG', 10, position, imgWidth, imgHeight);
heightLeft -= pdfHeight;
}
const agencyName = state.recipient.agency.split(' ')[0] || 'records';
pdf.save(`${agencyName}_request.pdf`);
}).catch(err => {
container.classList.remove('prrg-pdf-export-mode');
console.error("PDF generation failed:", err);
alert("Could not generate PDF. Please ensure your browser has permission.");
});
}
// --- START THE APP ---
init();
});
})();
I agree to pay reasonable search and duplication fees up to \$25.00, but please notify me if the cost will exceed this amount.
I look forward to receiving your response within the statutory time limit. Thank you for your time and cooperation.
Sincerely,
${requester.name}
