Forex Market API - Client Demo
Fetch Forex Quote (Mock Data)
Important Notes:
- This is a DEMONSTRATION tool using MOCK (sample) data. It does NOT connect to a live Forex market API.
- The data displayed is pre-defined and not real-time or indicative of actual market values.
- This tool illustrates how a client application might fetch and display data from a real API.
- For actual trading or financial decisions, always use reliable, real-time data sources from reputable Forex data providers and consult with a qualified financial advisor.
Mid Price: ${data.mid.toFixed(5)}
Last Update: ${escapeHtml(data.lastUpdate)}
`; notesContainerEl.style.display = 'block'; downloadPdfBtn.style.display = 'block'; } else { resultsHTML = `No mock data found for currency pair "${escapeHtml(pairInput)}". Try EUR/USD, GBP/JPY, USD/CAD, or AUD/USD.
`; notesContainerEl.style.display = 'none'; downloadPdfBtn.style.display = 'none'; } resultsOutputDiv.innerHTML = resultsHTML; resultsOutputDiv.style.display = 'block'; }); downloadPdfBtn?.addEventListener('click', function () { const { jsPDF } = window.jspdf; const pdfOutputArea = document.getElementById('fxdPdfOutputArea'); if (!pdfOutputArea || resultsOutputDiv.style.display === 'none' || resultsOutputDiv.querySelector('.fxd-error')) { alert('Please fetch a valid mock quote first before downloading PDF.'); return; } // Ensure notes are visible for PDF capture document.getElementById('fxdNotesContainer').style.display = 'block'; html2canvas(pdfOutputArea, { scale: 1.5, useCORS: true, backgroundColor: '#ffffff' }) .then(canvas => { const imgData = canvas.toDataURL('image/jpeg', 0.9); const pdf = new jsPDF({ orientation: 'portrait', unit: 'pt', format: 'a4' }); const pdfWidth = pdf.internal.pageSize.getWidth(); const pdfHeight = pdf.internal.pageSize.getHeight(); const margin = 40; const contentWidth = pdfWidth - 2 * margin; const canvasWidth = canvas.width; const canvasHeight = canvas.height; const ratio = canvasWidth / canvasHeight; let imgWidth = contentWidth; let imgHeight = imgWidth / ratio; if (imgHeight > pdfHeight - 2 * margin) { imgHeight = pdfHeight - 2 * margin; imgWidth = imgHeight * ratio; } const x = (pdfWidth - imgWidth) / 2; pdf.addImage(imgData, 'JPEG', x, margin, imgWidth, imgHeight, undefined, 'MEDIUM'); pdf.save('Forex_Quote_Demo.pdf'); }) .catch(err => { console.error("Error generating PDF:", err); alert("Error generating PDF. See console for details."); }); }); function escapeHtml(unsafe) { if (typeof unsafe !== 'string') return unsafe === undefined || unsafe === null ? '' : String(unsafe); return unsafe.replace(/&/g, "&").replace(//g, ">").replace(/"/g, """).replace(/'/g, "'"); } });