Refactor PDF table generation to support manual column exclusion

Updated the addTable method in PDFGenerator.js to enhance the functionality for excluding specified columns when generating PDF tables. This change allows for more flexible table rendering by manually parsing the HTML table if columns are to be excluded, improving the overall usability of the PDF generation feature.
This commit is contained in:
Torsten Schulz (local)
2025-10-20 23:43:14 +02:00
parent b69684ad03
commit 67f4f728fe

View File

@@ -243,37 +243,69 @@ class PDFGenerator {
addTable(tableId, highlightName = '', excludeColumns = []) {
this.pdf.setFontSize(11);
autoTable(this.pdf, {
html: `#${tableId}`,
startY: this.cursorY,
margin: { left: this.margin, right: this.margin },
styles: { fontSize: this.pdf.getFontSize() },
headStyles: { fillColor: [220, 220, 220], textColor: 0, halign: 'left' },
theme: 'grid',
didParseCell: (data) => {
// Spalten ausschließen
if (excludeColumns.includes(data.column.index)) {
data.cell.styles.display = 'none';
return;
// Wenn Spalten ausgeschlossen werden sollen, parse die Tabelle manuell
if (excludeColumns.length > 0) {
const table = document.querySelector(`#${tableId}`);
if (!table) return;
const headers = Array.from(table.querySelectorAll('thead th'))
.map((th, index) => ({ text: th.textContent.trim(), index }))
.filter(h => !excludeColumns.includes(h.index))
.map(h => h.text);
const rows = Array.from(table.querySelectorAll('tbody tr')).map(tr => {
return Array.from(tr.querySelectorAll('td'))
.map((td, index) => ({ text: td.textContent.trim(), index }))
.filter(cell => !excludeColumns.includes(cell.index))
.map(cell => cell.text);
});
autoTable(this.pdf, {
head: [headers],
body: rows,
startY: this.cursorY,
margin: { left: this.margin, right: this.margin },
styles: { fontSize: this.pdf.getFontSize(), cellPadding: 3, overflow: 'linebreak' },
headStyles: { fillColor: [220, 220, 220], textColor: 0, halign: 'left', fontStyle: 'bold' },
theme: 'grid',
tableWidth: 'auto',
didParseCell: (data) => {
if (data.section === 'body') {
const cellText = Array.isArray(data.cell.text)
? data.cell.text.join(' ')
: String(data.cell.text);
if (highlightName && cellText.includes(highlightName)) {
data.cell.styles.fontStyle = 'bold';
}
}
},
didDrawPage: (data) => {
this.cursorY = data.cursor.y + 10;
}
const cellText = Array.isArray(data.cell.text)
? data.cell.text.join(' ')
: String(data.cell.text);
if (highlightName && cellText.includes(highlightName)) {
data.cell.styles.fontStyle = 'bold';
});
} else {
// Ohne Ausschluss: normale HTML-Tabelle verwenden
autoTable(this.pdf, {
html: `#${tableId}`,
startY: this.cursorY,
margin: { left: this.margin, right: this.margin },
styles: { fontSize: this.pdf.getFontSize() },
headStyles: { fillColor: [220, 220, 220], textColor: 0, halign: 'left' },
theme: 'grid',
didParseCell: (data) => {
const cellText = Array.isArray(data.cell.text)
? data.cell.text.join(' ')
: String(data.cell.text);
if (highlightName && cellText.includes(highlightName)) {
data.cell.styles.fontStyle = 'bold';
}
},
didDrawPage: (data) => {
this.cursorY = data.cursor.y + 10;
}
},
didDrawPage: (data) => {
this.cursorY = data.cursor.y + 10;
},
// Spalten aus dem Header entfernen
willDrawCell: (data) => {
if (excludeColumns.includes(data.column.index)) {
return false;
}
}
});
});
}
}
addParticipantsSummary(tournamentTitle, tournamentDateText, groups) {