`;
});
downloadBtn.style.display = 'block'; // Show download button
} else if (generatedClues.length > 0) { // Handle specific error messages from generator
resultsDiv.innerHTML = `
${generatedClues[0].clue}
`; downloadBtn.style.display = 'none'; } else { resultsDiv.innerHTML = 'Could not generate clues. Please check locations and try again.
'; downloadBtn.style.display = 'none'; } }); document.getElementById('clearBtn').addEventListener('click', () => { document.getElementById('locationList').value = ''; document.getElementById('clueStyle').value = 'any'; document.getElementById('clueResults').innerHTML = 'Your scavenger hunt clues will appear here!
'; document.getElementById('downloadPdfBtn').style.display = 'none'; // Hide PDF button }); // Download PDF Button document.getElementById('downloadPdfBtn').addEventListener('click', () => { const resultsDiv = document.getElementById('clueResults'); const clueItems = resultsDiv.querySelectorAll('.clue-item'); if (clueItems.length === 0 || resultsDiv.textContent.includes('Your scavenger hunt clues will appear here!') || resultsDiv.textContent.includes('Generating clues...')) { alert("No clues to download."); return; } // --- PDF Generation --- const doc = new jsPDF(); const margin = 15; let y = margin; const pageHeight = doc.internal.pageSize.height; const pageWidth = doc.internal.pageSize.width; const maxLineWidth = pageWidth - 2 * margin; // Max width for text line doc.setFontSize(18); doc.setTextColor(52, 58, 64); // Match text-color variable #343a0 doc.text("Your Scavenger Hunt Clues", margin, y); y += 15; // Space after title doc.setFontSize(10); // Smaller font for clue details doc.setTextColor(52, 58, 64); // Match text-color variable #343a0 clueItems.forEach((itemElement, index) => { const locationText = itemElement.querySelector('p:nth-child(1)') ? itemElement.querySelector('p:nth-child(1)').textContent.trim() : ''; const clueText = itemElement.querySelector('.clue-text') ? itemElement.querySelector('.clue-text').textContent.trim() : ''; // Check if content fits on current page let contentHeightEstimate = 10; // For labels contentHeightEstimate += doc.splitTextToSize(locationText, maxLineWidth).length * 6; // Estimate location height contentHeightEstimate += doc.splitTextToSize(clueText, maxLineWidth - 10).length * 6; // Estimate clue height (with indent) contentHeightEstimate += 15; // Space after item if (y + contentHeightEstimate > pageHeight - margin && y > margin) { doc.addPage(); y = margin; doc.setFontSize(18); doc.setTextColor(52, 58, 64); doc.text("Scavenger Hunt Clues (cont.)", margin, y); y += 15; doc.setFontSize(10); doc.setTextColor(52, 58, 64); } // Add Location doc.setFont(undefined, 'bold'); // Set font bold for label doc.text(`Location ${index + 1}:`, margin, y); doc.setFont(undefined, 'normal'); // Reset font const locationLabelWidth = doc.getTextWidth(`Location ${index + 1}:`); const locationContent = locationText.replace(`Location ${index + 1}:`, '').trim(); // Get text after label doc.text(doc.splitTextToSize(locationContent, maxLineWidth - locationLabelWidth - 5), margin + locationLabelWidth + 5, y); y += (doc.splitTextToSize(locationContent, maxLineWidth - locationLabelWidth - 5).length * 6); // Add Clue doc.setFont(undefined, 'bold'); // Set font bold for label doc.text("Clue:", margin, y + 7); doc.setFont(undefined, 'normal'); // Reset font const clueLabelWidth = doc.getTextWidth("Clue:"); const clueLines = doc.splitTextToSize(clueText, maxLineWidth - clueLabelWidth - 15); // Account for label + indent clueLines.forEach((line, i) => { if (y + 7 + (i > 0 ? 0 : 7) > pageHeight - margin) { // Check space for line, adding label space on first line doc.addPage(); y = margin; doc.setFontSize(18); doc.setTextColor(52, 58, 64); doc.text("Scavenger Hunt Clues (cont.)", margin, y); y += 15; doc.setFontSize(10); doc.setTextColor(52, 58, 64); if (i === 0) { // If it's the first line of the clue on a new page, add the label again doc.setFont(undefined, 'bold'); doc.text("Clue:", margin, y); doc.setFont(undefined, 'normal'); y += 8; } } doc.text(line, margin + clueLabelWidth + 10, y + (i === 0 ? 7 : 0)); // Indent clue text, adjust starting y for first line y += 6; // Estimate line height }); y += 10; // Space after clue item }); doc.save('scavenger_hunt_clues.pdf'); });