Symfony 6.4中eckinox/pdf-bundle调用Puppeteer启动浏览器失败
Symfony 6.4 使用 eckinox/pdf-bundle 调用 Puppeteer Node 脚本失败问题
问题详情
使用Symfony 6.4的eckinox/pdf-bundle生成PDF时,调用执行Puppeteer的Node脚本失败。将脚本简化为仅获取Chrome版本的测试代码,仍出现相同错误。直接在命令行执行该脚本一切正常,且PHP的exec调用与命令行使用相同用户、环境变量和权限。
报错信息
2024-07-03T11:03:39.923Z puppeteer:browsers:launcher Launched 7250 2024-07-03T11:03:40.026Z puppeteer:browsers:launcher Trying to kill 7250 2024-07-03T11:03:40.026Z puppeteer:browsers:launcher Browser process 7250 exists /home/platform.company.com/public_html/node_modules/@puppeteer/browsers/lib/cjs/launch.js:310 reject(new Error([ ^ Error: Failed to launch the browser process! TROUBLESHOOTING: https://pptr.dev/troubleshooting at Interface.onClose (/home/platform.company.com/public_html/node_modules/@puppeteer/browsers/lib/cjs/launch.js:310:24) at Interface.emit (node:events:531:35) at Interface.close (node:internal/readline/interface:528:10) at Socket.onend (node:internal/readline/interface:254:10) at Socket.emit (node:events:531:35) at endReadableNT (node:internal/streams/readable:1696:12) at process.processTicksAndRejections (node:internal/process/task_queues:82:21) Node.js v20.15.0
简化测试脚本
const puppeteer = require('puppeteer'); (async()=>{const x = require("puppeteer"); console.log(await(await(await x.launch()).newPage()).browser().version())})()
排查建议
- 添加无沙箱启动参数:PHP执行环境通常缺少完整的沙箱权限,在
launch()中添加参数绕过:await x.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox'] }) - 手动指定浏览器路径:避免环境路径差异导致找不到浏览器,直接指定Chrome/Chromium的绝对路径:
await x.launch({ executablePath: '/usr/bin/chromium-browser' }) // 路径根据实际环境调整 - 输出详细启动日志:修改脚本添加错误捕获和日志输出,定位具体启动失败原因:
const puppeteer = require('puppeteer'); (async() => { try { const browser = await puppeteer.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox'], dumpio: true // 输出浏览器启动的所有控制台日志 }); const page = await browser.newPage(); console.log(await browser.version()); await browser.close(); } catch (error) { console.error('启动失败详细错误:', error); } })() - 检查系统权限限制:排查服务器的SELinux、AppArmor是否阻止了浏览器进程启动,同时确认PHP的
exec函数是否有进程数、内存等资源限制。
内容的提问来源于stack exchange,提问作者user25977261
相关产品推荐
相关产品推荐

