refactor: simplify PDF to image conversion by reading generated files directly from temp directory instead of pre-calculating page count
Build and Push Multi-Platform Images / build-and-push (push) Successful in 32s
Build and Push Multi-Platform Images / build-and-push (push) Successful in 32s
This commit is contained in:
@@ -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<string[]> {
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user