Implement PDF text extraction and HTML conversion in Satzung upload process
This commit introduces a new mechanism for extracting text from uploaded PDF files using pdftotext, followed by a basic plausibility check of the extracted content. If the text meets the criteria, it is converted to HTML format and stored in the configuration, replacing the previous static content handling. This enhancement improves the accuracy and reliability of the Satzung content management.
This commit is contained in:
@@ -96,7 +96,36 @@ export default defineEventHandler(async (event) => {
|
||||
// Zusätzliche Validierung: Magic-Bytes prüfen (mimetype kann gespooft sein)
|
||||
await assertPdfMagicHeader(file.path)
|
||||
|
||||
// Config aktualisieren: Nur PDF-Pfad setzen, HTML-Inhalt nicht automatisch neu generieren
|
||||
// 1. Versuche, den Text mit pdftotext zu extrahieren
|
||||
let extractedText = ''
|
||||
try {
|
||||
// UTF-8 erzwingen, Ausgabe nach stdout
|
||||
const { stdout } = await execAsync(`pdftotext -enc UTF-8 "${file.path}" -`)
|
||||
extractedText = stdout || ''
|
||||
} catch (err) {
|
||||
console.error('pdftotext Fehler beim Verarbeiten der Satzung:', err)
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
statusMessage: 'Die Satzung konnte nicht aus dem PDF gelesen werden (pdftotext-Fehler). Bitte den Server-Administrator kontaktieren.'
|
||||
})
|
||||
}
|
||||
|
||||
// Minimale Plausibilitätsprüfung: genug Text & typische Satzungs-Merkmale
|
||||
const cleaned = extractedText.trim()
|
||||
if (!cleaned || cleaned.length < 500 || !cleaned.includes('§')) {
|
||||
console.error('Satzung: extrahierter Text wirkt unplausibel oder zu kurz:', {
|
||||
length: cleaned.length
|
||||
})
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
statusMessage: 'Die Satzung konnte nicht zuverlässig aus dem PDF gelesen werden. Bitte die PDF-Datei prüfen.'
|
||||
})
|
||||
}
|
||||
|
||||
// 2. In HTML-Format konvertieren
|
||||
const htmlContent = convertTextToHtml(cleaned)
|
||||
|
||||
// 3. Config aktualisieren (PDF + geparster Inhalt)
|
||||
const configPath = getDataPath('config.json')
|
||||
const configData = JSON.parse(await fs.readFile(configPath, 'utf-8'))
|
||||
|
||||
@@ -104,12 +133,9 @@ export default defineEventHandler(async (event) => {
|
||||
configData.seiten = {}
|
||||
}
|
||||
|
||||
const previousContent = configData.seiten.satzung?.content || ''
|
||||
|
||||
configData.seiten.satzung = {
|
||||
pdfUrl: '/documents/satzung.pdf',
|
||||
// Entweder bestehenden Text behalten oder einen neutralen Hinweis setzen
|
||||
content: previousContent || '<p>Die gültige Satzung des Harheimer Tischtennis-Club 1954 e. V. steht als PDF zum Download bereit. Die PDF-Fassung ist rechtlich verbindlich.</p>'
|
||||
content: htmlContent
|
||||
}
|
||||
|
||||
await fs.writeFile(configPath, JSON.stringify(configData, null, 2), 'utf-8')
|
||||
|
||||
Reference in New Issue
Block a user