Cost-Saving Strategies: Big Cities vs. Small Towns
Select a category to see strategies.
'; return; } const strategies = this.data[category]; let html = `${category.charAt(0).toUpperCase() + category.slice(1)} Strategies
`; if (strategies.length === 0) { html += 'No strategies listed for this category yet.
'; } else { strategies.forEach(strategy => { html += `${strategy.name}
Big City Considerations
- ${strategy.bigCity.map(item => `
- ${item} `).join('')}
Small Town Considerations
- ${strategy.smallTown.map(item => `
- ${item} `).join('')}
Cost-Saving Strategies: Big Cities vs. Small Towns
`; for (const category in this.data) { if (this.data.hasOwnProperty(category)) { const strategies = this.data[category]; pdfHtml += `${category.charAt(0).toUpperCase() + category.slice(1)}
`; if (strategies.length > 0) { strategies.forEach(strategy => { pdfHtml += `${strategy.name}
Big City Considerations:
- ${strategy.bigCity.map(item => `
- ${item} `).join('')}
Small Town Considerations:
- ${strategy.smallTown.map(item => `
- ${item} `).join('')}
No strategies listed for this category.
`; } } } pdfExportContainer.innerHTML = pdfHtml; // Temporarily append to body for html2canvas to render // Position it off-screen pdfExportContainer.style.position = 'absolute'; pdfExportContainer.style.left = '-9999px'; document.body.appendChild(pdfExportContainer); try { const canvas = await html2canvas(pdfExportContainer, { scale: 1.5, // Increase scale for better quality PDF useCORS: true, logging: false }); document.body.removeChild(pdfExportContainer); // Clean up the temporary div 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(); // Calculate image dimensions to fit A4, maintaining aspect ratio const imgProps = pdf.getImageProperties(imgData); const imgWidth = pdfWidth - 40; // 20pt margin on each side const imgHeight = (imgProps.height * imgWidth) / imgProps.width; let heightLeft = imgHeight; let position = 20; // Initial Y position with 20pt margin pdf.addImage(imgData, 'PNG', 20, position, imgWidth, imgHeight); heightLeft -= (pdfHeight - 40); // Subtract available height on page (with margins) while (heightLeft > 0) { position = heightLeft - imgHeight; // New Y position for the image segment on the new page pdf.addPage(); pdf.addImage(imgData, 'PNG', 20, position, imgWidth, imgHeight); heightLeft -= (pdfHeight - 40); } pdf.save('Cost_Saving_Strategies_Comparison.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 cleanup on error document.body.removeChild(pdfExportContainer); } } } }; document.addEventListener('DOMContentLoaded', function() { csApp.init(); });