From fc2cf1acc2c82b17d47059e6603ea905beb37e0b Mon Sep 17 00:00:00 2001 From: richardtekula Date: Fri, 30 Jan 2026 10:31:51 +0100 Subject: [PATCH] fix: Add Chromium to Docker for PDF certificate generation - Install chromium in Alpine Dockerfile - Add PUPPETEER_EXECUTABLE_PATH env var support - Fallback to system Chrome paths if bundled Chrome not found Co-Authored-By: Claude Opus 4.5 --- Dockerfile | 5 +++ src/services/ai-kurzy/certificate.service.js | 44 ++++++++++++++++++-- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index d29b08b..a21597c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,6 +8,11 @@ COPY package*.json ./ # Inštalácia závislostí (len produkčné) RUN npm ci --only=production +# Inštalácia Chromium pre generovanie PDF certifikátov +RUN apk add --no-cache chromium +ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browser +ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true + # Kopírovanie zdrojového kódu COPY src ./src COPY drizzle.config.js ./ diff --git a/src/services/ai-kurzy/certificate.service.js b/src/services/ai-kurzy/certificate.service.js index 9cb2784..15c83e3 100644 --- a/src/services/ai-kurzy/certificate.service.js +++ b/src/services/ai-kurzy/certificate.service.js @@ -294,10 +294,48 @@ export const generateCertificate = async (registraciaId, templateName = 'AIcerti // Launch Puppeteer and generate PDF let browser; try { - browser = await puppeteer.launch({ + const launchOptions = { headless: true, - args: ['--no-sandbox', '--disable-setuid-sandbox'], - }); + args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-gpu', '--disable-dev-shm-usage'], + }; + + // Use PUPPETEER_EXECUTABLE_PATH env var if set (for Docker), otherwise try system paths + const executablePath = process.env.PUPPETEER_EXECUTABLE_PATH; + if (executablePath) { + launchOptions.executablePath = executablePath; + logger.info(`Using Chrome from PUPPETEER_EXECUTABLE_PATH: ${executablePath}`); + } + + try { + browser = await puppeteer.launch(launchOptions); + } catch (launchError) { + // If no env var set, try common system Chrome paths + if (!executablePath) { + const systemChromePaths = [ + '/usr/bin/chromium-browser', + '/usr/bin/chromium', + '/usr/bin/google-chrome', + '/usr/bin/google-chrome-stable', + ]; + + for (const chromePath of systemChromePaths) { + try { + await fs.access(chromePath); + logger.info(`Using system Chrome at: ${chromePath}`); + browser = await puppeteer.launch({ + ...launchOptions, + executablePath: chromePath, + }); + break; + } catch { + // Continue to next path + } + } + } + if (!browser) { + throw new Error('Chrome nie je nainštalovaný. Pridajte do Dockerfile: RUN apk add --no-cache chromium && ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browser'); + } + } const page = await browser.newPage(); await page.setContent(html, { waitUntil: 'networkidle0' });