Dynamic Currency Conversion Tool
Real-time conversions using the latest exchange rates.
1,000.00 USD equals
920.00 EUR
1 USD = 0.9200 EUR
Exchange Rates (Base: USD)
Rates are fetched from Frankfurter API. Last updated: N/A
Currency Conversion Statement
Generated by Dynamic Currency Conversion Tool
1,000.00 USD is equivalent to
920.00 EUR
The exchange rate used was 1 USD = 0.9200 EUR
CONVERSION DATE
RATE DATE
Exchange rates are subject to fluctuation and are provided for informational purposes only.
Failed to load rates.
`; } finally { fetchRatesBtn.textContent = 'Fetch Latest Rates'; fetchRatesBtn.disabled = false; } } // --- RENDER FUNCTIONS --- function populateCurrencySelects() { const currencyCodes = Object.keys(rates).sort(); fromCurrencySelect.innerHTML = ''; toCurrencySelect.innerHTML = ''; currencyCodes.forEach(code => { const option1 = new Option(code, code); const option2 = new Option(code, code); fromCurrencySelect.add(option1); toCurrencySelect.add(option2); }); } function renderRatesTable() { ratesTableContainer.innerHTML = ''; const sortedRates = Object.entries(rates).sort((a, b) => a[0].localeCompare(b[0])); sortedRates.forEach(([currency, rate]) => { const row = `
${currency}
${rate.toFixed(4)}
`;
ratesTableContainer.insertAdjacentHTML('beforeend', row);
});
}
// --- EVENT HANDLERS ---
[amountInput, fromCurrencySelect, toCurrencySelect].forEach(el => {
el.addEventListener('change', convert);
el.addEventListener('input', convert);
});
swapBtn.addEventListener('click', () => {
const fromVal = fromCurrencySelect.value;
fromCurrencySelect.value = toCurrencySelect.value;
toCurrencySelect.value = fromVal;
convert();
});
fetchRatesBtn.addEventListener('click', fetchRates);
downloadPdfBtn.addEventListener('click', generatePdf);
// --- TABS & NAVIGATION ---
window.switchTab = (tabName) => {
currentTab = tabName;
Object.values(tabBtns).forEach(btn => btn.classList.replace('tab-active', 'tab-inactive'));
Object.values(tabContents).forEach(content => content.style.display = 'none');
tabBtns[tabName].classList.replace('tab-inactive', 'tab-active');
tabContents[tabName].style.display = 'block';
};
window.navigateTabs = (direction) => {
if (direction === 'next' && currentTab === 'converter') switchTab('rates');
else if (direction === 'prev' && currentTab === 'rates') switchTab('converter');
};
// --- PDF GENERATION ---
async function generatePdf() {
const { jsPDF } = window.jspdf;
const pdfReportElement = document.getElementById('pdf-report');
// Populate PDF with current data
document.getElementById('pdf-from-text').textContent = resultInputText.textContent;
document.getElementById('pdf-to-text').textContent = resultOutputText.textContent;
document.getElementById('pdf-rate-text').textContent = `The exchange rate used was ${resultRateText.textContent}`;
document.getElementById('pdf-conversion-date').textContent = new Date().toLocaleString();
document.getElementById('pdf-rate-date').textContent = ratesLastUpdated.textContent;
const canvas = await html2canvas(pdfReportElement, { scale: 2 });
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF('p', 'mm', 'a4');
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = (canvas.height * pdfWidth) / canvas.width;
pdf.addImage(imgData, 'PNG', 0, 0, pdfWidth, pdfHeight);
pdf.save('Currency-Conversion-Statement.pdf');
}
// --- INITIALIZATION ---
fetchRates();
});
