diff --git a/paperless-backend/src/preprocessing/pdf.service.ts b/paperless-backend/src/preprocessing/pdf.service.ts index 3da643d..e64027e 100644 --- a/paperless-backend/src/preprocessing/pdf.service.ts +++ b/paperless-backend/src/preprocessing/pdf.service.ts @@ -40,11 +40,7 @@ export class PdfService { * Verwendet einen einzigen Ghostscript-Aufruf mit %d-Platzhalter für alle Seiten. */ async pdfToImages(pdfPath: string, dpi = 200): Promise { - const pageCount = await this.getPageCount(pdfPath); - if (pageCount === 0) return []; - const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'pdf-')); - const outputPattern = path.join(tmpDir, 'page-%d.png'); await execFileAsync('gs', [ '-dNOPAUSE', @@ -52,22 +48,26 @@ export class PdfService { '-dSAFER', '-sDEVICE=png16m', `-r${dpi}`, - `-sOutputFile=${outputPattern}`, + `-sOutputFile=${path.join(tmpDir, 'page-%d.png')}`, pdfPath, ]); - const images: string[] = []; - for (let i = 1; i <= pageCount; i++) { - const imgPath = path.join(tmpDir, `page-${i}.png`); - try { - await fs.access(imgPath); - images.push(imgPath); - } catch { - this.logger.warn(`Ghostscript hat Seite ${i} nicht erstellt: ${imgPath}`); - } + const entries = await fs.readdir(tmpDir); + const images = entries + .filter(f => f.endsWith('.png')) + .sort((a, b) => { + const numA = parseInt(a.match(/\d+/)?.[0] ?? '0', 10); + const numB = parseInt(b.match(/\d+/)?.[0] ?? '0', 10); + return numA - numB; + }) + .map(f => path.join(tmpDir, f)); + + if (images.length === 0) { + this.logger.warn(`Ghostscript hat keine Seiten erstellt: ${pdfPath} — Verzeichnisinhalt: [${entries.join(', ')}]`); + } else { + this.logger.debug(`PDF konvertiert: ${images.length} Seite(n) in ${tmpDir}`); } - this.logger.debug(`PDF konvertiert: ${images.length}/${pageCount} Seite(n) in ${tmpDir}`); return images; }