Pressure Unit Converter
fromDropdown.innerHTML = ""; toDropdown.innerHTML = "";
for (let unit in units) { let option1 = new Option(unit, unit); let option2 = new Option(unit, unit); fromDropdown.add(option1); toDropdown.add(option2); } }
document.addEventListener("DOMContentLoaded", function() { if (window.jspdf && window.jspdf.jsPDF) { window.jsPDF = window.jspdf.jsPDF; } populateDropdowns(); });
function convertPressure() { let pressure = parseFloat(document.getElementById('pressure').value); let fromUnit = document.getElementById('fromUnit').value; let toUnit = document.getElementById('toUnit').value;
if (isNaN(pressure) || fromUnit === "" || toUnit === "") { alert("Please enter a valid pressure value and select both units."); return; }
let conversionFactor = units[toUnit] / units[fromUnit]; let convertedPressure = pressure * conversionFactor; document.getElementById('output').textContent = convertedPressure.toFixed(4); document.getElementById("downloadPdf").disabled = false; }
function downloadPDF() { if (!window.jsPDF) { alert("jsPDF library not loaded correctly."); return; }
let doc = new jsPDF(); let pressure = document.getElementById('pressure').value; let fromUnit = document.getElementById('fromUnit').value; let toUnit = document.getElementById('toUnit').value; let result = document.getElementById('output').textContent;
if (pressure === "" || fromUnit === "" || toUnit === "" || result === "--") { alert("Please perform a conversion before downloading the PDF."); return; }
let conversionFactor = (units[toUnit] / units[fromUnit]).toFixed(6); let formula = "Basic Formula: Converted Pressure = Input Pressure × Conversion Factor"; let steps = [ `Step 1: Identify the conversion factor from ${fromUnit} to ${toUnit}.`, `Step 2: Multiply the input pressure (${pressure} ${fromUnit}) by the conversion factor (${conversionFactor}).`, `Step 3: The result is ${result} ${toUnit}.` ];
doc.text("Pressure Unit Conversion Result", 15, 20); doc.text(formula, 15, 30); doc.text(`Conversion Factor: 1 ${fromUnit} = ${conversionFactor} ${toUnit}`, 15, 40); doc.text(`Input Pressure: ${pressure} ${fromUnit}`, 15, 50); doc.text("Step-by-Step Conversion:", 15, 60); let wrappedSteps = doc.splitTextToSize(steps.join("\n"), 180); doc.text(wrappedSteps, 15, 70); doc.text(`Final Converted Pressure: ${result} ${toUnit}`, 15, 120);
doc.save("Pressure_Conversion_Result.pdf");
}