XWX-AI commited on
Commit
35f3546
·
1 Parent(s): 8dea6b9

Fix Puppeteer 24.x Chrome launch in Docker

Browse files

- Add PUPPETEER_SKIP_DOWNLOAD and PUPPETEER_EXECUTABLE_PATH env vars
- Simplify browser launch logic to use system Chromium
- Ensure compatibility with Docker deployment

Files changed (2) hide show
  1. Dockerfile +6 -1
  2. server.js +8 -26
Dockerfile CHANGED
@@ -10,13 +10,18 @@ RUN apt-get update && apt-get install -y \
10
  fonts-noto-core \
11
  fonts-noto-color-emoji \
12
  libxss1 \
 
13
  --no-install-recommends \
14
  && rm -rf /var/lib/apt/lists/*
15
 
16
  # 2. Set working directory
17
  WORKDIR /app
18
 
19
- # 3. Install Node.js Dependencies
 
 
 
 
20
  COPY package.json .
21
  RUN npm install
22
 
 
10
  fonts-noto-core \
11
  fonts-noto-color-emoji \
12
  libxss1 \
13
+ ca-certificates \
14
  --no-install-recommends \
15
  && rm -rf /var/lib/apt/lists/*
16
 
17
  # 2. Set working directory
18
  WORKDIR /app
19
 
20
+ # 3. Configure Puppeteer to skip bundled Chrome download (use system chromium)
21
+ ENV PUPPETEER_SKIP_DOWNLOAD=true
22
+ ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium
23
+
24
+ # 4. Install Node.js Dependencies
25
  COPY package.json .
26
  RUN npm install
27
 
server.js CHANGED
@@ -47,39 +47,21 @@ app.post('/api/generate_pdf', async (req, res) => {
47
  const totalWaitTime = Math.min(baseWaitTime + sizeWaitTime, 8000); // Cap at 8s max
48
  console.log(`[PDF-GEN] Calculated wait times: base=${baseWaitTime}ms, size=${sizeWaitTime}ms, total=${totalWaitTime}ms`);
49
 
50
- // Launch Chrome
51
- // Puppeteer 24.x uses Chrome for Testing by default
52
- // If chromium is installed via apt-get, use it; otherwise Puppeteer will download Chrome
53
- const launchOptions = {
54
  args: [
55
  '--no-sandbox',
56
  '--disable-setuid-sandbox',
57
- '--disable-dev-shm-usage', // Critical for Docker
58
- '--font-render-hinting=none', // Better font rendering
59
- '--disable-gpu', // Disable GPU for stability in Docker
60
  '--disable-software-rasterizer',
61
  '--memory-pressure-off'
62
  ],
63
  headless: true
64
- };
65
-
66
- // Try to use system chromium if available, otherwise use bundled Chrome
67
- try {
68
- const fs = require('fs');
69
- if (fs.existsSync('/usr/bin/chromium')) {
70
- launchOptions.executablePath = '/usr/bin/chromium';
71
- console.log('[PDF-GEN] Using system Chromium');
72
- } else if (fs.existsSync('/usr/bin/google-chrome')) {
73
- launchOptions.executablePath = '/usr/bin/google-chrome';
74
- console.log('[PDF-GEN] Using system Google Chrome');
75
- } else {
76
- console.log('[PDF-GEN] Using bundled Chrome for Testing');
77
- }
78
- } catch (e) {
79
- console.log('[PDF-GEN] Using bundled Chrome for Testing');
80
- }
81
-
82
- browser = await puppeteer.launch(launchOptions);
83
 
84
  const page = await browser.newPage();
85
 
 
47
  const totalWaitTime = Math.min(baseWaitTime + sizeWaitTime, 8000); // Cap at 8s max
48
  console.log(`[PDF-GEN] Calculated wait times: base=${baseWaitTime}ms, size=${sizeWaitTime}ms, total=${totalWaitTime}ms`);
49
 
50
+ // Launch Chrome using system Chromium (configured via env var)
51
+ browser = await puppeteer.launch({
52
+ executablePath: process.env.PUPPETEER_EXECUTABLE_PATH || '/usr/bin/chromium',
 
53
  args: [
54
  '--no-sandbox',
55
  '--disable-setuid-sandbox',
56
+ '--disable-dev-shm-usage',
57
+ '--font-render-hinting=none',
58
+ '--disable-gpu',
59
  '--disable-software-rasterizer',
60
  '--memory-pressure-off'
61
  ],
62
  headless: true
63
+ });
64
+ console.log('[PDF-GEN] Browser launched successfully');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
 
66
  const page = await browser.newPage();
67