Personalized Sci-Fi Invention Creator

Configure Your Invention

Type: ${inventionType}

Primary Function: ${functionDescription} (Utilizing ${keyTechnology} principles)

Aesthetic: ${aestheticDescription}

Key Components:

    ${components.map(comp => `
  • ${comp}
  • `).join('')}

Power Source: ${powerSource}

Known Quirk: ${quirk}

`; outputArea.innerHTML = outputHTML; outputContainer.style.display = 'block'; if (downloadBtn) { downloadBtn.style.display = 'inline-block'; // Make button visible // Re-enable button if it was disabled due to previous error downloadBtn.disabled = (typeof window.jspdf === 'undefined'); } console.log("Invention generated and displayed."); } catch (error) { console.error("Error during invention generation:", error); errorMsgDiv.textContent = "An error occurred while generating the invention details."; } } // --- PDF Download Logic --- function downloadPDF() { console.log("PDF Download button clicked."); errorMsgDiv.textContent = ''; // Clear previous errors // Double-check library presence before attempting download if (typeof window.jspdf === 'undefined' || typeof window.jspdf.jsPDF === 'undefined') { console.error("jsPDF library not available when trying to download."); errorMsgDiv.textContent = "Error: PDF library not available. Cannot download PDF."; if(downloadBtn) downloadBtn.disabled = true; // Disable again if check fails here return; } // Ensure output area has content const inventionOutput = document.getElementById('sciFiOutputArea'); if (!inventionOutput || !inventionOutput.innerHTML.trim()) { console.error("No content found in output area to generate PDF."); errorMsgDiv.textContent = "Error: No invention details generated yet."; return; } try { console.log("Attempting PDF generation..."); const { jsPDF } = window.jspdf; const inventionName = inventionOutput.querySelector('h4')?.innerText || 'SciFi-Invention'; const pdfFilename = `${inventionName.replace(/[^a-z0-9]/gi, '_').toLowerCase()}.pdf`; console.log(`PDF Filename: ${pdfFilename}`); // Get computed styles for colors const toolStyle = window.getComputedStyle(document.getElementById('sciFiCreatorTool')); const primaryColor = toolStyle.getPropertyValue('--sciFi-primary-color').trim(); const secondaryColor = toolStyle.getPropertyValue('--sciFi-secondary-color').trim(); const textColor = toolStyle.getPropertyValue('--sciFi-text-color').trim(); console.log(`Colors - Primary: ${primaryColor}, Secondary: ${secondaryColor}, Text: ${textColor}`); // Create new PDF instance const doc = new jsPDF({ orientation: 'p', unit: 'pt', format: 'a4' }); console.log("jsPDF object created."); // Add CSS for the PDF rendering const pdfStyles = ` body { font-family: sans-serif; color: ${textColor}; background-color: #ffffff; } h4 { color: ${primaryColor}; margin-bottom: 10pt; font-size: 16pt; border-bottom: 1px solid ${secondaryColor}; padding-bottom: 4pt;} p { margin-bottom: 8pt; font-size: 11pt; line-height: 1.5;} strong { color: ${secondaryColor}; font-weight: bold; } ul { margin-left: 20pt; margin-bottom: 8pt; list-style-position: outside; } li { margin-bottom: 4pt; font-size: 11pt;} `; const styleTag = document.createElement('style'); styleTag.innerHTML = pdfStyles; document.head.appendChild(styleTag); console.log("Temporary PDF styles appended to head."); // Use jsPDF's html method doc.html(inventionOutput, { callback: function (doc) { console.log("jsPDF html() callback executed."); // Remove the temporary style tag try { document.head.removeChild(styleTag); console.log("Temporary PDF styles removed from head."); } catch (e) { console.warn("Could not remove temporary style tag, it might have already been removed.", e); } // Save the PDF doc.save(pdfFilename); console.log("PDF save command issued."); }, error: function(err) { // Add error callback for doc.html() console.error("Error occurred within jsPDF.html() method:", err); errorMsgDiv.textContent = "Error during PDF rendering process. Check console."; try { // Attempt cleanup even on error document.head.removeChild(styleTag); } catch (e) {} }, x: 40, // Left margin (in points) y: 40, // Top margin (in points) width: 515, // A4 width (595pt) - margins (40*2 = 80pt) windowWidth: inventionOutput.scrollWidth, // Use element's width for scaling calculation margin: [40, 40, 40, 40] // T, R, B, L margins (Ensure consistent) }); console.log("jsPDF.html() method invoked."); } catch (error) { console.error("Error during PDF generation process:", error); errorMsgDiv.textContent = "An unexpected error occurred generating the PDF. See console for details."; // Attempt to remove style tag if it exists from an error mid-process const tempStyle = document.head.querySelector('style'); if (tempStyle && tempStyle.innerHTML.includes('pdfStyles')) { // Basic check try { document.head.removeChild(tempStyle); } catch(e){} } } } // --- Attach Event Listeners --- if (generateBtn) { generateBtn.addEventListener('click', generateInvention); console.log("Event listener attached to Generate button."); } else { console.error("Generate button not found!"); } if (downloadBtn) { downloadBtn.addEventListener('click', downloadPDF); console.log("Event listener attached to Download button."); } else { console.error("Download button not found!"); } } // End of initializeSciFiTool // --- Run Initialization --- // Ensure the DOM is fully loaded before running the script if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', initializeSciFiTool); } else { // DOMContentLoaded has already fired initializeSciFiTool(); }
Scroll to Top