Personalized Sci-Fi Alien Pet Generator

Design your unique companion from the stars! Choose its traits below.

${generatedPetData.diet}

Quirk:

${generatedPetData.quirk}

`; resultsDiv.style.display = 'block'; pdfBtn.disabled = false; } function downloadPDF() { if (!generatedPetData || Object.keys(generatedPetData).length === 0) { alert("Please generate a pet first."); return; } // Ensure jsPDF is loaded if (typeof jspdf === 'undefined') { console.error("jsPDF library is not loaded."); alert("Error: PDF generation library failed to load. Please check internet or try again."); return; } const { jsPDF } = jspdf; const doc = new jsPDF(); // --- PDF Styling --- const primaryColor = '#38b2ac'; // Teal const secondaryColor = '#805ad5'; // Purple const textColor = '#2d3748'; // Dark Slate const pageMargin = 15; const lineSpacing = 7; let currentY = pageMargin; // Helper to add text and manage Y position + page breaks function addTextToPDF(text, size, color, isBold = false, indent = 0) { const pageHeight = doc.internal.pageSize.height; const textLines = doc.splitTextToSize(text, doc.internal.pageSize.width - pageMargin * 2 - indent); const textX = pageMargin + indent; if (currentY + size / 2 > pageHeight - pageMargin) { doc.addPage(); currentY = pageMargin; } doc.setFontSize(size); doc.setTextColor(color); doc.setFont("helvetica", isBold ? 'bold' : 'normal'); textLines.forEach(line => { if (currentY + size / 2 > pageHeight - pageMargin) { doc.addPage(); currentY = pageMargin; } doc.text(line, textX, currentY); currentY += lineSpacing; }); currentY += lineSpacing * 0.1; // Small buffer } function addHeadingToPDF(text) { addTextToPDF(text, 13, primaryColor, true); currentY += lineSpacing * 0.2; // Space after heading } function addDescriptionToPDF(text) { addTextToPDF(text, 11, textColor, false, 5); // Indent description currentY += lineSpacing * 0.3; // Space after description block } // --- PDF Content --- addTextToPDF("Your Sci-Fi Alien Pet", 18, secondaryColor, true); currentY += lineSpacing * 1.5; addHeadingToPDF("Name:"); addDescriptionToPDF(generatedPetData.name); addHeadingToPDF("Origin:"); addDescriptionToPDF(generatedPetData.origin); addHeadingToPDF("Temperament:"); addDescriptionToPDF(generatedPetData.temperament); addHeadingToPDF("Size:"); addDescriptionToPDF(generatedPetData.size); addHeadingToPDF("Appearance:"); addDescriptionToPDF(generatedPetData.appearance); addHeadingToPDF("Special Ability:"); addDescriptionToPDF(generatedPetData.ability); addHeadingToPDF("Diet:"); addDescriptionToPDF(generatedPetData.diet); addHeadingToPDF("Quirk:"); addDescriptionToPDF(generatedPetData.quirk); // --- Save PDF --- doc.save('my_alien_pet.pdf'); } // --- Event Listeners --- generateBtn.addEventListener('click', generatePet); pdfBtn.addEventListener('click', downloadPDF); })(); // IIFE closes
Scroll to Top