Productivity Motivation Generator
Your daily dose of drive and focus.
Loading motivation...
Your Daily Dose of Drive
`; // Create a container for the PDF content const pdfContainer = document.createElement('div'); pdfContainer.appendChild(pdfHeader); pdfContainer.appendChild(contentToPrint); // Append to body to render, but make it invisible to the user document.body.appendChild(pdfContainer); pdfContainer.style.position = 'absolute'; pdfContainer.style.left = '-9999px'; pdfContainer.style.width = '700px'; // A fixed width for consistent rendering before capture const { jsPDF } = window.jspdf; const canvas = await html2canvas(pdfContainer, { scale: 2 }); const imgData = canvas.toDataURL('image/png'); // Clean up the cloned element from the DOM immediately after capture document.body.removeChild(pdfContainer); // Initialize jsPDF with a standard A4 page size const pdf = new jsPDF({ orientation: 'portrait', unit: 'px', format: 'a4' }); const pdfWidth = pdf.internal.pageSize.getWidth(); const canvasWidth = canvas.width; const canvasHeight = canvas.height; // Calculate the aspect ratio of the captured image const ratio = canvasHeight / canvasWidth; // Calculate the height of the image to fit the PDF's width const pdfHeight = pdfWidth * ratio; // Add the image to the PDF, scaling it to fit the page width pdf.addImage(imgData, 'PNG', 0, 0, pdfWidth, pdfHeight); pdf.save('productivity-motivation.pdf'); }; // --- EVENT LISTENERS --- if (generateBtn) { generateBtn.addEventListener('click', generateMotivation); } if (downloadPdfBtn) { downloadPdfBtn.addEventListener('click', downloadMotivationAsPDF); } // --- INITIALIZATION --- // Generate the first motivation on page load generateMotivation(); });