Smart Home Gadgets: Cost vs. Savings Calculator
Enter Smart Gadget Information
Cost vs. Savings Summary
Please add gadgets and their details on the 'Gadget Details' tab to see the savings summary.
No gadget details available.
`; } pdfExportContainer.innerHTML = pdfHtmlContent; // Temporarily append to body for html2canvas to render (off-screen) pdfExportContainer.style.position = 'absolute'; pdfExportContainer.style.left = '-9999px'; // Position off-screen document.body.appendChild(pdfExportContainer); try { const canvas = await html2canvas(pdfExportContainer, { scale: 2, // Higher scale for better PDF quality useCORS: true, logging: false, // true for debugging onclone: (clonedDoc) => { // You can make further adjustments to the cloned document specifically for PDF rendering here // For example, force certain styles or remove interactive elements } }); document.body.removeChild(pdfExportContainer); // Clean up const imgData = canvas.toDataURL('image/png'); const pdf = new jsPDF({ orientation: 'p', // portrait unit: 'pt', // points format: 'a4' // A4 paper }); const pdfWidth = pdf.internal.pageSize.getWidth(); const pdfHeight = pdf.internal.pageSize.getHeight(); const canvasWidth = canvas.width; const canvasHeight = canvas.height; // Maintain aspect ratio const ratio = canvasWidth / canvasHeight; let imgWidth = pdfWidth - 40; // Subtract margins (20pt each side) let imgHeight = imgWidth / ratio; // If the image height exceeds page height, scale down if (imgHeight > pdfHeight - 40) { imgHeight = pdfHeight - 40; // Subtract margins (20pt top/bottom) imgWidth = imgHeight * ratio; } const x = (pdfWidth - imgWidth) / 2; // Center the image const y = 20; // Top margin pdf.addImage(imgData, 'PNG', x, y, imgWidth, imgHeight); pdf.save('Smart_Gadgets_Cost_Savings_Summary.pdf'); } catch (error) { console.error("Error generating PDF:", error); alert("An error occurred while generating the PDF. Please check the console for details."); if (document.body.contains(pdfExportContainer)) { // Ensure clone is removed in case of error document.body.removeChild(pdfExportContainer); } } };