IP Address Lookup

ISP: ${data.isp || 'N/A'}

Organization: ${data.org || 'N/A'}

ASN: ${data.as || 'N/A'}

`; resultsContainer.style.display = 'block'; downloadPdfBtn.disabled = false; } function displayError(message) { errorMessage.textContent = message; errorMessage.style.display = 'block'; hideResults(); // Hide any previous results } function hideError() { errorMessage.style.display = 'none'; errorMessage.textContent = ''; } function hideResults() { resultsContainer.style.display = 'none'; resultsArea.innerHTML = ''; downloadPdfBtn.disabled = true; } function showLoading(show) { loadingIndicator.style.display = show ? 'flex' : 'none'; } function disableButtons(disable) { lookupBtn.disabled = disable; myIpBtn.disabled = disable; // Don't disable PDF button here, its state depends on results availability } function generatePdf() { // Check if libraries are available before attempting to use them if (typeof jspdf === 'undefined' || typeof html2canvas === 'undefined') { console.error("PDF generation failed: jsPDF or html2canvas library not available."); displayError("PDF generation failed due to missing libraries."); return; } const { jsPDF } = jspdf; const pdfElement = document.getElementById('resultsArea'); // Capture only the results div const pdfButton = document.getElementById('downloadPdfBtn'); // Get ref to button const originalButtonDisplay = pdfButton.style.display; // Store original display style // Temporarily hide the PDF button itself so it's not in the screenshot pdfButton.style.display = 'none'; // Set background to white explicitly for capture if needed, avoids transparency issues const originalBg = pdfElement.style.backgroundColor; pdfElement.style.backgroundColor = '#ffffff'; html2canvas(pdfElement, { scale: 2, // Increase scale for better resolution in PDF useCORS: true, // Good practice, though maybe not needed for simple text backgroundColor: '#ffffff' // Ensure background is white }).then(canvas => { // Restore button visibility and original background *after* capture pdfButton.style.display = originalButtonDisplay; pdfElement.style.backgroundColor = originalBg; const imgData = canvas.toDataURL('image/png'); const pdf = new jsPDF({ orientation: 'p', // portrait unit: 'mm', // millimeters format: 'a4' // standard A4 size }); const pdfWidth = pdf.internal.pageSize.getWidth(); const pdfHeight = pdf.internal.pageSize.getHeight(); const canvasWidth = canvas.width; const canvasHeight = canvas.height; const ratio = canvasHeight / canvasWidth; // Calculate the width and height maintaining aspect ratio let imgWidth = pdfWidth - 20; // pdf width with 10mm margin on each side let imgHeight = imgWidth * ratio; // If the calculated height exceeds the page height, scale based on height instead if (imgHeight > pdfHeight - 20) { // 10mm margin top/bottom imgHeight = pdfHeight - 20; imgWidth = imgHeight / ratio; } // Center the image on the page const xPos = (pdfWidth - imgWidth) / 2; const yPos = 10; // 10mm margin from top pdf.setFontSize(16); // Add a title to the PDF pdf.text('IP Address Lookup Results', pdfWidth / 2, yPos, { align: 'center' }); pdf.addImage(imgData, 'PNG', xPos, yPos + 10, imgWidth, imgHeight); // Add image below title // Construct filename const ipForFilename = document.getElementById('ipAddressInput').value.trim().replace(/\./g, '_') || 'my_ip'; const filename = `ip_lookup_${ipForFilename}.pdf`; pdf.save(filename); }).catch(err => { // Restore button visibility and background even if there's an error pdfButton.style.display = originalButtonDisplay; pdfElement.style.backgroundColor = originalBg; console.error("Error generating PDF: ", err); displayError("An error occurred while generating the PDF."); }); } // Initial setup hideResults(); hideError(); showLoading(false); downloadPdfBtn.disabled = true; // Ensure PDF button starts disabled }); // End DOMContentLoaded listener
Scroll to Top