Standortplaner/append_pdf.js

98 lines
4.8 KiB
JavaScript

const fs = require('fs');
const js = `
// --- PDF Export Logic ---
const btnExportPDF = document.getElementById('btnExportPDF');
if (btnExportPDF) {
btnExportPDF.addEventListener('click', async () => {
const originalText = btnExportPDF.innerText;
btnExportPDF.innerText = 'Exportiere...';
btnExportPDF.disabled = true;
try {
if (typeof html2canvas === 'undefined' || typeof jspdf === 'undefined') {
throw new Error('PDF Bibliotheken (html2canvas, jspdf) sind noch nicht geladen.');
}
const mapContainer = document.getElementById('map');
// Add Overlay
const exportOverlay = document.createElement('div');
exportOverlay.style.position = 'absolute';
exportOverlay.style.bottom = '30px';
exportOverlay.style.left = '30px';
exportOverlay.style.zIndex = '9999';
exportOverlay.style.background = 'rgba(255, 255, 255, 0.95)';
exportOverlay.style.padding = '15px';
exportOverlay.style.border = '2px solid #333';
exportOverlay.style.borderRadius = '5px';
exportOverlay.style.color = '#333';
exportOverlay.style.fontFamily = 'Arial, sans-serif';
exportOverlay.style.boxShadow = '0 0 10px rgba(0,0,0,0.3)';
const projName = document.getElementById('projectSelector')?.options[document.getElementById('projectSelector')?.selectedIndex]?.text || 'Unbekannt';
exportOverlay.innerHTML = \`
<div style="font-weight: bold; font-size: 1.4rem; margin-bottom: 8px; color: #004a99;">ENWELO Standortplanung</div>
<div style="font-size: 1rem; margin-bottom: 12px; font-weight: bold;">Projekt: \${projName}</div>
<div style="display: flex; flex-direction: column; gap: 6px; font-size: 0.9rem;">
<div style="display: flex; align-items: center; gap: 8px;"><div style="width: 16px; height: 16px; background: #00c8ff; border: 1px dashed #00c8ff; opacity: 0.5;"></div> Rotorüberstreichfläche</div>
<div style="display: flex; align-items: center; gap: 8px;"><div style="width: 16px; height: 16px; background: #00aaff; border: 1px dashed #00aaff; opacity: 0.5;"></div> Gebietsumring</div>
<div style="display: flex; align-items: center; gap: 8px;"><div style="width: 16px; height: 3px; background: #ff0000;"></div> Abstandslinien</div>
<div style="display: flex; align-items: center; gap: 8px;"><div style="width: 16px; height: 16px; border-radius: 50%; background: #00c8ff;"></div> WEA Standort</div>
</div>
\`;
mapContainer.appendChild(exportOverlay);
// Wait a tiny bit for DOM to render overlay
await new Promise(r => setTimeout(r, 100));
const canvas = await html2canvas(mapContainer, {
useCORS: true,
allowTaint: true,
ignoreElements: (element) => {
return element.classList.contains('leaflet-control-zoom');
}
});
mapContainer.removeChild(exportOverlay);
const imgData = canvas.toDataURL('image/png');
const pdf = new jspdf.jsPDF({
orientation: 'landscape',
unit: 'mm',
format: 'a3'
});
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = pdf.internal.pageSize.getHeight();
const imgProps = pdf.getImageProperties(imgData);
const ratio = imgProps.width / imgProps.height;
let renderWidth = pdfWidth;
let renderHeight = pdfWidth / ratio;
if (renderHeight > pdfHeight) {
renderHeight = pdfHeight;
renderWidth = pdfHeight * ratio;
}
// Center image in PDF
const xOffset = (pdfWidth - renderWidth) / 2;
const yOffset = (pdfHeight - renderHeight) / 2;
pdf.addImage(imgData, 'PNG', xOffset, yOffset, renderWidth, renderHeight);
pdf.save('Karte_Enwelo.pdf');
} catch (err) {
console.error('Fehler beim PDF Export:', err);
alert('Fehler beim PDF Export: ' + err.message);
} finally {
btnExportPDF.innerText = originalText;
btnExportPDF.disabled = false;
}
});
}
`;
fs.appendFileSync('app.js', js);