Stock Market Data API - Client Demo

Fetch Stock Quote (Mock Data)

Change: ${changeSign}$${Math.abs(data.change).toFixed(2)} (${changeSign}${data.changePercent.toFixed(2)}%)

Volume: ${data.volume.toLocaleString()}

Timestamp: ${escapeHtml(data.timestamp)}

`; notesContainerEl.style.display = 'block'; downloadPdfBtn.style.display = 'block'; } else { resultsHTML = `

No mock data found for symbol "${escapeHtml(symbol)}". Try AAPL, MSFT, GOOGL, or TSLA.

`; 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('sdtPdfOutputArea'); if (!pdfOutputArea || resultsOutputDiv.style.display === 'none' || resultsOutputDiv.querySelector('.sdt-error')) { alert('Please fetch a valid mock quote first before downloading PDF.'); return; } // Ensure the notes are visible in the PDF capture area document.getElementById('sdtNotesContainer').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('Stock_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, "'"); } });
Scroll to Top