Car Insurance Deductible vs. Premium Analyzer
Enter Insurance Options & Analysis Period
Option 1 (e.g., Lower Deductible)
Option 2 (e.g., Higher Deductible)
Analysis Parameters
Analysis Results
Total Cost = (Monthly Premium × 12 × Years) + (Deductible Amount × Number of Claims)
`; cidResultsTableContainerDiv.innerHTML = tableHTML; cidToolOutputDiv.style.display = 'block'; cidDownloadPdfButtonElem.style.display = 'inline-block'; } // --- PDF Download Functionality --- async function cidDownloadPDF() { const { jsPDF } = window.jspdf; if (typeof html2canvas === 'undefined' || typeof jsPDF === 'undefined') { alert('Error: PDF generation libraries not loaded.'); console.error('jsPDF or html2canvas is not loaded.'); return; } const inputs = cidGetInputs(); if (!inputs) { alert("Cannot generate PDF: please ensure valid inputs are entered and analyzed."); return; } const pdfContentWrapper = document.createElement('div'); pdfContentWrapper.className = 'cid-pdf-content-wrapper'; let inputsHTML = `Input Parameters
| Option 1 Deductible: | ${cidFormatCurrency(inputs.deductible1)} |
| Option 1 Monthly Premium: | ${cidFormatCurrency(inputs.premium1)} |
| Option 2 Deductible: | ${cidFormatCurrency(inputs.deductible2)} |
| Option 2 Monthly Premium: | ${cidFormatCurrency(inputs.premium2)} |
| Analysis Period: | ${inputs.years} Year${inputs.years === 1 ? '' : 's'} |
Analysis Results
| Scenario (Over ${inputs.years} Years) | Option 1 Total Cost | Option 2 Total Cost | Cheaper Option |
|---|---|---|---|
| ${i} Claim${i === 1 ? '' : 's'} | ${cidFormatCurrency(costs.option1.total)} | ${cidFormatCurrency(costs.option2.total)} | ${cheaperOptionText} |
Total Cost = (Monthly Premium × 12 × Years) + (Deductible Amount × Number of Claims)
`; pdfContentWrapper.innerHTML = `Car Insurance Deductible vs. Premium Analysis
${inputsHTML} ${resultsHTML}Report generated on: ${new Date().toLocaleDateString()} ${new Date().toLocaleTimeString()}
`; document.body.appendChild(pdfContentWrapper); try { const canvas = await html2canvas(pdfContentWrapper, { scale: 2, useCORS: true, backgroundColor: '#ffffff' }); const imgData = canvas.toDataURL('image/png'); const pdf = new jsPDF({ orientation: 'p', unit: 'pt', format: 'a4' }); const pdfWidth = pdf.internal.pageSize.getWidth(); const pdfHeight = pdf.internal.pageSize.getHeight(); const imgProps = pdf.getImageProperties(imgData); const imgWidth = imgProps.width; const imgHeight = imgProps.height; const ratio = Math.min((pdfWidth - 40) / imgWidth, (pdfHeight - 40) / imgHeight); const scaledWidth = imgWidth * ratio; const scaledHeight = imgHeight * ratio; const xOffset = (pdfWidth - scaledWidth) / 2; const yOffset = 20; pdf.addImage(imgData, 'PNG', xOffset, yOffset, scaledWidth, scaledHeight); pdf.save('Car_Insurance_Analysis.pdf'); } catch (error) { console.error("Error during PDF generation:", error); alert("An error occurred while generating the PDF."); } finally { document.body.removeChild(pdfContentWrapper); } }