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.
|
* Verwendet einen einzigen Ghostscript-Aufruf mit %d-Platzhalter für alle Seiten.
|
||||||
*/
|
*/
|
||||||
async pdfToImages(pdfPath: string, dpi = 200): Promise<string[]> {
|
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 tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'pdf-'));
|
||||||
const outputPattern = path.join(tmpDir, 'page-%d.png');
|
|
||||||
|
|
||||||
await execFileAsync('gs', [
|
await execFileAsync('gs', [
|
||||||
'-dNOPAUSE',
|
'-dNOPAUSE',
|
||||||
@@ -52,22 +48,26 @@ export class PdfService {
|
|||||||
'-dSAFER',
|
'-dSAFER',
|
||||||
'-sDEVICE=png16m',
|
'-sDEVICE=png16m',
|
||||||
`-r${dpi}`,
|
`-r${dpi}`,
|
||||||
`-sOutputFile=${outputPattern}`,
|
`-sOutputFile=${path.join(tmpDir, 'page-%d.png')}`,
|
||||||
pdfPath,
|
pdfPath,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const images: string[] = [];
|
const entries = await fs.readdir(tmpDir);
|
||||||
for (let i = 1; i <= pageCount; i++) {
|
const images = entries
|
||||||
const imgPath = path.join(tmpDir, `page-${i}.png`);
|
.filter(f => f.endsWith('.png'))
|
||||||
try {
|
.sort((a, b) => {
|
||||||
await fs.access(imgPath);
|
const numA = parseInt(a.match(/\d+/)?.[0] ?? '0', 10);
|
||||||
images.push(imgPath);
|
const numB = parseInt(b.match(/\d+/)?.[0] ?? '0', 10);
|
||||||
} catch {
|
return numA - numB;
|
||||||
this.logger.warn(`Ghostscript hat Seite ${i} nicht erstellt: ${imgPath}`);
|
})
|
||||||
}
|
.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;
|
return images;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user