You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Apache调用Node/Puppeteer启动Chrome失败:crashpad_handler报错

Puppeteer通过PHP调用失败:chrome_crashpad_handler报错问题

环境信息

  • Node.js v20.11.1
  • PHP 7.4.33
  • chrome/linux-124.0.6367.91

问题背景

我将index.js和puppeteerrc.cjs放在同一目录下,目的是根据指定URL生成PDF文件,puppeteerrc.cjs中配置了缓存目录。

直接在命令行执行以下命令可正常生成PDF:

node index.js 'https://www.stackoverflow.com' output.pdf

但通过PHP执行相同逻辑时失败,PHP代码如下:

$cmd = "cd '/path/to/script' && node 'index.js' 'https://www.stackoverflow.com' /path/to/output.pdf 2>&1";
exec($cmd, $out, $error_code);

进一步排查发现,命令行运行时Chrome会在我的用户目录下的.config/google-chrome-for-testing/中生成文件,推测Apache运行时Chrome也在尝试访问该目录。目前已解决Chrome可执行文件找不到的问题,Apache下的PHP能通过is_executable()检测到Chrome文件为可执行状态,但报错问题仍存在。

尝试过的解决方案

  1. 在index.js中指定Chrome的可执行文件路径,结果依然是命令行正常、Apache下失败:
const browser = await puppeteer.launch({
                executablePath:'/opt/npm/.cache/puppeteer/chrome/linux-124.0.6367.91/chrome-linux64/chrome'
          });
  1. 添加以下启动配置组合,仍输出相同错误:
args: ['--no-sandbox', "--disabled-setupid-sandbox"],
ignoreDefaultArgs: ['--disable-extensions'] ,
userDataDir: "/path/to/a/dir",
headless:true
  1. 执行过chrome_crashpad_handler --help但不知如何利用相关信息。

PHP调用时的错误输出

opt/npm/lib/node_modules/puppeteer/node_modules/@puppeteer/browsers/lib/cjs/launch.js:310
                reject(new Error([
                       ^

Error: Failed to launch the browser process!
chrome_crashpad_handler: --database is required
Try 'chrome_crashpad_handler --help' for more information.


TROUBLESHOOTING: https://pptr.dev/troubleshooting

    at Interface.onClose (/opt/npm/lib/node_modules/puppeteer/node_modules/@puppeteer/browsers/lib/cjs/launch.js:310:24)
    at Interface.emit (node:events:530:35)
    at Interface.close (node:internal/readline/interface:527:10)
    at Socket.onend (node:internal/readline/interface:253:10)
    at Socket.emit (node:events:530:35)
    at endReadableNT (node:internal/streams/readable:1696:12)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)

Node.js v20.11.1

相关文件内容

index.js

/**
 * 根据URL生成PDF文件。命令行参数第一个(除JS文件外)是URL,第二个是输出文件名。
 * 使用方法:   node  index.js <URL> <OUTPUT_FILE_NAME>
 */

const node_path = "/opt/npm/lib/node_modules";
module.paths.unshift(node_path);

const puppeteer = require('puppeteer');

(async () => {

  // 创建浏览器实例
  const browser = await puppeteer.launch();

  // 创建新页面
  const page = await browser.newPage();

  // 要导出为PDF的网站URL
  const website_url = process.argv[2];

  // 输出路径
  const output_path = process.argv[3];

  // 在当前页面打开URL
  await page.goto(website_url, { waitUntil: 'networkidle0' }); 

  // 使用打印样式
  await page.emulateMediaType('print');

  // 生成PDF
  const pdf = await page.pdf({
    path: output_path,
    margin: { top: '.39in', right: '.39in', bottom: '.39in', left: '.39in' },
    printBackground: true,
    format: 'Letter',
  });

  // 关闭浏览器实例
  await browser.close();
})();

.puppeteerrc.cjs

/**
 * @type {import("puppeteer").Configuration}
 */
module.exports = {
  // 修改Puppeteer的缓存目录
  cacheDirectory: '/opt/npm/.cache/puppeteer'
};

内容的提问来源于stack exchange,提问作者developer981

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 04:17:17