Virtual Mystery Puzzle Generator
Generate ideas for codes, riddles, and contraptions to challenge your detectives!
Generated Puzzle Idea
${generatedPuzzleData.type} (${generatedPuzzleData.difficulty}, ${generatedPuzzleData.theme} Theme)
Description:
${generatedPuzzleData.description}
Hint: ${generatedPuzzleData.hint}
`; resultsDiv.style.display = 'block'; pdfBtn.disabled = false; } // --- PDF Download Function --- function downloadPDF() { if (!generatedPuzzleData || !generatedPuzzleData.description) { alert("Please generate a puzzle idea first."); return; } // Ensure jsPDF is loaded if (typeof jspdf === 'undefined' || typeof jspdf.jsPDF === 'undefined') { console.error("jsPDF library or jsPDF constructor is not loaded correctly."); alert("Error: PDF generation library failed to load. Please check internet connection or Elementor setup."); return; } const { jsPDF } = jspdf; const doc = new jsPDF(); // --- PDF Styling --- const primaryColor = '#8d6e63'; // Brown const secondaryColor = '#546e7a'; // Blue Grey const accentColor = '#ef5350'; // Red const textColor = '#37474f'; // Dark Slate const pageMargin = 15; const lineSpacing = 7; let currentY = pageMargin; const pageHeight = doc.internal.pageSize.height; const pageWidth = doc.internal.pageSize.width; // Helper to add text with page breaks function addTextToPDF(text, size, color, style = 'normal') { doc.setFont("helvetica", style); // Use standard font doc.setFontSize(size); doc.setTextColor(color); const splitText = doc.splitTextToSize(text, pageWidth - pageMargin * 2); splitText.forEach(line => { if (currentY + lineSpacing > pageHeight - pageMargin) { doc.addPage(); currentY = pageMargin; // Reset font on new page doc.setFont("helvetica", style); doc.setFontSize(size); doc.setTextColor(color); } doc.text(line, pageMargin, currentY); currentY += lineSpacing; }); } // --- PDF Content --- addTextToPDF("Mystery Puzzle Idea", 18, primaryColor, 'bold'); currentY += lineSpacing; addTextToPDF(`Puzzle Type: ${generatedPuzzleData.type}`, 12, secondaryColor, 'bold'); addTextToPDF(`Theme: ${generatedPuzzleData.theme}`, 10, secondaryColor); addTextToPDF(`Difficulty: ${generatedPuzzleData.difficulty}`, 10, secondaryColor); currentY += lineSpacing; // Extra space addTextToPDF("Description:", 12, primaryColor, 'bold'); addTextToPDF(generatedPuzzleData.description, 11, textColor); currentY += lineSpacing * 0.5; addTextToPDF("Hint:", 12, accentColor, 'bold'); // Red hint title addTextToPDF(generatedPuzzleData.hint, 11, textColor); // --- Save PDF --- const safeFileName = generatedPuzzleData.type.replace(/[^a-z0-9]/gi, '_').toLowerCase(); doc.save(`mystery_puzzle_${safeFileName}.pdf`); } // --- Event Listeners --- if (generateBtn) { generateBtn.addEventListener('click', generatePuzzle); } else { console.error("Generate button not found!"); } if (pdfBtn) { pdfBtn.addEventListener('click', downloadPDF); } else { console.error("PDF button not found!"); } })(); // Immediately invoke the function