Your supply chain in this emerging market currently presents low risk. This indicates a relatively stable environment for operations. However, continuous vigilance is always recommended due to the dynamic nature of emerging markets. Consider:
- Maintaining strong due diligence processes for new and existing suppliers.
- Staying informed about regional and global developments that could impact stability.
- Leveraging this stable environment for potential growth and efficiency improvements.
`;
}
recommendationsContent.innerHTML = recommendationsText;
}
/**
* Generates a PDF report of the tool's inputs and calculated results.
*/
async function generatePdf() {
// Show loading spinner
pdfSpinner.classList.remove('hidden');
downloadPdfBtn.disabled = true; // Disable button during PDF generation
// Ensure the risk is calculated and displayed
calculateSupplyChainRisk();
// Create a temporary div to render PDF content into
const pdfContentDiv = document.createElement('div');
pdfContentDiv.className = 'pdf-content-hidden'; // Hidden and styled for print
document.body.appendChild(pdfContentDiv);
// Add title to PDF content
const title = document.createElement('h1');
title.className = 'text-2xl font-bold text-gray-800 text-center mb-6';
title.textContent = 'AI-Based Supply Chain Risk Analysis Report';
pdfContentDiv.appendChild(title);
// Add input values to PDF content
const inputsSectionTitle = document.createElement('h2');
inputsSectionTitle.className = 'text-xl font-semibold text-gray-800 mb-4';
inputsSectionTitle.textContent = 'Input Factors';
pdfContentDiv.appendChild(inputsSectionTitle);
const inputsTable = document.createElement('table');
inputsTable.className = 'w-full text-left border-collapse mb-6'; // Tailwind classes for table
inputsTable.innerHTML = `
| Factor |
Value |
`;
const inputsTableBody = inputsTable.querySelector('tbody');
pdfContentDiv.appendChild(inputsTable);
// Collect input values and populate the table
const inputElements = [
{ id: 'geoPoliticalStability', label: 'Geopolitical/Political Stability' },
{ id: 'economicStability', label: 'Economic Stability' },
{ id: 'infrastructureQuality', label: 'Infrastructure Quality' },
{ id: 'logisticsComplexity', label: 'Logistics Complexity' },
{ id: 'regulatoryEnvironment', label: 'Regulatory Environment' },
{ id: 'corruptionPerception', label: 'Corruption Perception Index (CPI)' },
{ id: 'supplierConcentration', label: 'Supplier Concentration Risk' },
{ id: 'naturalDisasterFrequency', label: 'Natural Disaster Frequency' },
{ id: 'currencyVolatility', label: 'Currency Volatility' },
{ id: 'laborRelations', label: 'Labor Relations/Workforce Stability' }
];
inputElements.forEach(item => {
const element = document.getElementById(item.id);
if (element) {
const row = inputsTableBody.insertRow();
const labelCell = row.insertCell();
const valueCell = row.insertCell();
labelCell.className = 'py-2 px-4 border-b border-gray-200';
valueCell.className = 'py-2 px-4 border-b border-gray-200';
labelCell.textContent = item.label;
if (element.tagName === 'SELECT') {
valueCell.textContent = element.options[element.selectedIndex].text;
} else {
valueCell.textContent = element.value;
}
}
});
// Add risk assessment results to PDF content
const resultsSection = document.createElement('div');
resultsSection.className = 'mt-8 p-6 bg-gray-50 rounded-lg shadow-inner';
resultsSection.innerHTML = `
Risk Assessment
Overall Supply Chain Risk Level: ${overallRiskLevelSpan.textContent}
Total Risk Score: ${totalRiskScoreSpan.textContent}
Contributing Factors:
${contributingFactorsList.innerHTML}
`;
pdfContentDiv.appendChild(resultsSection);
// Add recommendations to PDF content
const recommendationsSection = document.createElement('div');
recommendationsSection.className = 'mt-8 p-6 bg-blue-50 rounded-lg shadow-inner text-gray-700';
recommendationsSection.innerHTML = `
Recommendations
${recommendationsContent.innerHTML}
`;
pdfContentDiv.appendChild(recommendationsSection);
try {
const canvas = await html2canvas(pdfContentDiv, {
scale: 2, // Increase scale for better resolution in PDF
useCORS: true // Required if you have external images/resources (though none here)
});
const imgData = canvas.toDataURL('image/png');
const { jsPDF } = window.jspdf;
const pdf = new jsPDF({
orientation: 'portrait',
unit: 'px',
format: 'a4'
});
const imgWidth = pdf.internal.pageSize.getWidth() - 40; // Subtract margins (20px each side)
const imgHeight = (canvas.height * imgWidth) / canvas.width;
let position = 20; // Initial Y position with top margin
pdf.addImage(imgData, 'PNG', 20, position, imgWidth, imgHeight);
// If content overflows, add new pages. This is a basic check.
let heightLeft = imgHeight;
heightLeft -= (pdf.internal.pageSize.getHeight() - 40); // Subtract current page usable height
while (heightLeft >= 0) {
position = heightLeft - imgHeight + 20; // Calculate position for next page
pdf.addPage();
pdf.addImage(imgData, 'PNG', 20, position, imgWidth, imgHeight);
heightLeft -= (pdf.internal.pageSize.getHeight() - 40);
}
pdf.save('AI_Supply_Chain_Risk_Report.pdf');
} catch (error) {
console.error("Error generating PDF:", error);
// Provide user feedback without alert()
const messageBox = document.createElement('div');
messageBox.textContent = "Failed to generate PDF. Please try again or check console for details.";
messageBox.style.cssText = `
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: #f8d7da;
color: #721c24;
padding: 15px 25px;
border-radius: 8px;
border: 1px solid #f5c6cb;
z-index: 1000;
box-shadow: 0 4px 12px rgba(0,0,0,0.2);
font-family: 'Inter', sans-serif;
`;
document.body.appendChild(messageBox);
setTimeout(() => messageBox.remove(), 5000); // Remove after 5 seconds
} finally {
// Hide loading spinner and re-enable button
pdfSpinner.classList.add('hidden');
downloadPdfBtn.disabled = false;
// Remove the temporary PDF content div from the DOM
if (pdfContentDiv.parentNode) {
pdfContentDiv.parentNode.removeChild(pdfContentDiv);
}
}
}
});