diff --git a/Dockerfile b/Dockerfile index 8e5439631f24f8db2a236b43e3dc29eae6ab2418..a1424cdd1fce46db75cb6108df52a651f4c94965 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,8 +2,43 @@ FROM node:18-slim WORKDIR /usr/src/app -# Install system dependencies including Chromium -RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/* +# Configure a shared Puppeteer cache so build (root) and runtime (appuser) see the same Chrome +ENV PUPPETEER_CACHE_DIR=/usr/local/share/puppeteer + +# Install system dependencies required by Chrome/Chromium +RUN apt-get update && apt-get install -y \ + ca-certificates \ + fonts-liberation \ + libasound2 \ + libatk-bridge2.0-0 \ + libatk1.0-0 \ + libc6 \ + libcairo2 \ + libcups2 \ + libdbus-1-3 \ + libexpat1 \ + libfontconfig1 \ + libgbm1 \ + libglib2.0-0 \ + libgtk-3-0 \ + libnss3 \ + libnspr4 \ + libpango-1.0-0 \ + libx11-6 \ + libx11-xcb1 \ + libxcb1 \ + libxcomposite1 \ + libxcursor1 \ + libxdamage1 \ + libxi6 \ + libxrandr2 \ + libxrender1 \ + libxss1 \ + libxtst6 \ + wget \ + curl \ + xdg-utils \ + && rm -rf /var/lib/apt/lists/* RUN npm install -g pm2@latest @@ -13,19 +48,23 @@ COPY package*.json ./ # Install production dependencies RUN npm ci --production --unsafe-perm +# Ensure Chrome for Puppeteer is downloaded into the shared cache +RUN npx --yes puppeteer browsers install chrome@stable + # Now copy actual app code (after user creation) COPY . . # Create non-root user RUN useradd --create-home --shell /bin/bash appuser -# Set permissions for app directory +# Set permissions for app directory and puppeteer cache RUN mkdir -p /usr/src/app/data \ && touch /usr/src/app/data/blacklist.json && chmod 666 /usr/src/app/data/blacklist.json \ && mkdir -p /usr/src/app/allImages \ && mkdir -p /usr/src/app/uploads \ && chmod 755 /usr/src/app/uploads \ - && chown -R appuser:appuser /usr/src/app + && mkdir -p "$PUPPETEER_CACHE_DIR" \ + && chown -R appuser:appuser /usr/src/app "$PUPPETEER_CACHE_DIR" EXPOSE 8097 diff --git a/utils/pdfGenerator.js b/utils/pdfGenerator.js index 75685feaaf02872ce109df674ca7bc433d5bafd4..2597f8b9f2ed6e803883f2cc3fa229067b9ca396 100644 --- a/utils/pdfGenerator.js +++ b/utils/pdfGenerator.js @@ -19,7 +19,13 @@ const findChrome = () => { } } - // If no Chrome found, return null to use Puppeteer's bundled Chromium + // If no Chrome found, return Puppeteer's bundled Chromium path (if available) + try { + const bundled = puppeteer.executablePath(); + if (bundled && fs.existsSync(bundled)) { + return bundled; + } + } catch (_) {} return null; }; @@ -48,12 +54,10 @@ const htmlToPdf = async (html) => { ] }; - // Only set executablePath if Chrome is found + // Prefer system Chrome; otherwise use Puppeteer's bundled Chromium if resolved if (chromePath) { launchOptions.executablePath = chromePath; console.log(`🔧 Using Chrome at: ${chromePath}`); - } else { - console.log('⚠️ Chrome not found, using Puppeteer bundled Chromium'); } // Launch browser @@ -117,12 +121,10 @@ const htmlToPdfFile = async (html, filePath) => { ] }; - // Only set executablePath if Chrome is found + // Prefer system Chrome; otherwise use Puppeteer's bundled Chromium if resolved if (chromePath) { launchOptions.executablePath = chromePath; console.log(`🔧 Using Chrome at: ${chromePath}`); - } else { - console.log('⚠️ Chrome not found, using Puppeteer bundled Chromium'); } // Launch browser @@ -186,12 +188,10 @@ const htmlToShippingLabelPdf = async (html) => { ] }; - // Only set executablePath if Chrome is found + // Prefer system Chrome; otherwise use Puppeteer's bundled Chromium if resolved if (chromePath) { launchOptions.executablePath = chromePath; console.log(`🔧 Using Chrome at: ${chromePath}`); - } else { - console.log('⚠️ Chrome not found, using Puppeteer bundled Chromium'); } // Launch browser