import * as pdfjsLib from 'pdfjs-dist/legacy/build/pdf.js'; import * as fs from 'fs'; async function debugText(filePath: string) { console.log(`\nLese PDF für Text-Debugging: ${filePath}`); const dataBuffer = fs.readFileSync(filePath); const loadingTask = pdfjsLib.getDocument({ data: new Uint8Array(dataBuffer) }); const pdfDocument = await loadingTask.promise; console.log(`Anzahl Seiten: ${pdfDocument.numPages}`); // Wir nehmen uns Seite 1 vor const page = await pdfDocument.getPage(1); const textContent = await page.getTextContent(); console.log(`\n--- RAW TEXT ITEMS (Die ersten 40 Fragmente) ---`); for (let i = 0; i < Math.min(40, textContent.items.length); i++) { const item = textContent.items[i] as any; console.log(`Y: ${item.transform[5].toFixed(1).padStart(6)} | X: ${item.transform[4].toFixed(1).padStart(6)} | Text: "${item.str}"`); } // Unsere Sortier- und Bereinigungslogik anwenden const sortedItems = textContent.items .map((item: any) => ({ text: item.str, x: item.transform[4], y: item.transform[5] })) .sort((a: any, b: any) => { if (Math.abs(b.y - a.y) > 5) return b.y - a.y; return a.x - b.x; }); const textRaw = sortedItems.map((i: any) => i.text).join('').toUpperCase().replace(/[^A-Z]/g, ''); console.log(`\n--- BEREINIGTER SUCH-STRING (Die ersten 200 Zeichen) ---`); console.log(textRaw.substring(0, 200)); console.log(`\nEnthält 'BUYERINFORMATIONSHEET'? -> ${textRaw.includes('BUYERINFORMATIONSHEET')}`); } const args = process.argv.slice(2); const pdfArgIndex = args.indexOf('--pdf'); if (pdfArgIndex !== -1 && args[pdfArgIndex + 1]) { debugText(args[pdfArgIndex + 1]).catch(console.error); } else { console.error("Bitte --pdf Parameter angeben."); }