add pdf reader modul
This commit is contained in:
@@ -5,6 +5,8 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Knowledge\Ingest;
|
||||
|
||||
use Smalot\PdfParser\Parser;
|
||||
|
||||
final class DocumentLoader
|
||||
{
|
||||
public function load(string $path): string
|
||||
@@ -17,8 +19,10 @@ final class DocumentLoader
|
||||
|
||||
return match ($ext) {
|
||||
'txt', 'md' => $this->loadText($path),
|
||||
// 'pdf' => $this->loadPdf($path),
|
||||
// 'docx' => $this->loadDocx($path),
|
||||
'pdf' => $this->loadPdf($path),
|
||||
|
||||
// vorbereitet für später:
|
||||
// 'docx' => $this->loadDocx($path),
|
||||
|
||||
default => throw new \RuntimeException("Unsupported file type: .{$ext}"),
|
||||
};
|
||||
@@ -30,7 +34,41 @@ final class DocumentLoader
|
||||
if ($content === false) {
|
||||
throw new \RuntimeException("Could not read file: {$path}");
|
||||
}
|
||||
return $content;
|
||||
|
||||
return $this->normalize($content);
|
||||
}
|
||||
|
||||
private function loadPdf(string $path): string
|
||||
{
|
||||
$parser = new Parser();
|
||||
|
||||
try {
|
||||
$pdf = $parser->parseFile($path);
|
||||
$text = $pdf->getText();
|
||||
} catch (\Throwable $e) {
|
||||
throw new \RuntimeException("Failed to parse PDF: {$path}", 0, $e);
|
||||
}
|
||||
|
||||
return $this->normalize($text);
|
||||
}
|
||||
|
||||
/**
|
||||
* Zentraler Normalizer für alle Dokumenttypen
|
||||
*/
|
||||
private function normalize(string $text): string
|
||||
{
|
||||
// Silbentrennung entfernen
|
||||
$text = preg_replace('/-\n/', '', $text);
|
||||
|
||||
// Windows-Zeilenumbrüche
|
||||
$text = str_replace("\r\n", "\n", $text);
|
||||
|
||||
// Mehrfache Leerzeichen
|
||||
$text = preg_replace('/[ \t]+/', ' ', $text);
|
||||
|
||||
// Mehrfache Leerzeilen
|
||||
$text = preg_replace('/\n{3,}/', "\n\n", $text);
|
||||
|
||||
return trim($text);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user