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

使用Headless Chrome生成高棉语PDF时遇Devtools could not start错误求助

解决Headless Chrome生成高棉语PDF时"Devtools could not start"问题

针对你遇到的页面持续加载、Devtools无法启动的问题,结合代码给出以下具体排查和修复方案:

1. 确保Chrome/Chromium版本与依赖库兼容

如果使用的是chrome-php/chrome这类库,需保证本地安装的Chrome/Chromium版本≥60,且库版本与浏览器版本匹配(建议使用最新稳定版的浏览器和库)。版本不匹配会直接导致Devtools协议握手失败。

2. 显式指定Chrome可执行文件路径

服务器环境下系统常无法自动定位Chrome路径,需在创建浏览器时手动指定:

$browser = (new BrowserFactory())->createBrowser([
    'windowSize' => [1920, 1080],
    'debug' => true,
    // 根据系统修改路径
    'binary' => '/usr/bin/google-chrome', // Linux环境
    // 'binary' => 'C:/Program Files/Google/Chrome/Application/chrome.exe', // Windows环境
    'arguments' => [
        '--headless=new', // 使用新版Headless模式,稳定性更强
        '--no-sandbox', // 服务器环境通常需禁用沙箱
        '--disable-gpu', // 禁用GPU加速,避免无头模式兼容性问题
        '--remote-debugging-port=9222', // 显式指定调试端口,避免冲突
        '--disable-dev-shm-usage', // 解决Linux下共享内存不足问题
    ],
]);

3. 调整页面导航等待逻辑

默认waitForNavigation()可能未等待页面完全加载(尤其是含高棉语字体或异步内容的页面),建议修改为等待网络空闲状态:

$page->navigate($request->url)->waitForNavigation([
    'waitUntil' => 'networkidle0', // 等待网络连接数为0,确保页面加载完成
    'timeout' => 30000, // 增加超时时间,适配高棉语资源加载速度
]);

4. 解决高棉语字体渲染问题

高棉语字体缺失或加载失败会导致页面渲染卡住,进而影响Devtools连接:

  • 服务器系统安装高棉语字体(如Khmer OS System、Khmer OS Content等);
  • 在网页中通过@font-face嵌入高棉语字体,摆脱系统字体依赖:
@font-face {
    font-family: 'KhmerOS';
    src: url('/fonts/KhmerOS.ttf') format('truetype');
    font-weight: normal;
    font-style: normal;
}
body {
    font-family: 'KhmerOS', sans-serif;
}

5. 检查文件写入权限

确保public_path("uploads/")目录拥有web服务器的写入权限,避免PDF保存失败引发后续异常:

# Linux环境设置权限
chmod -R 755 /path/to/public/uploads
chown -R www-data:www-data /path/to/public/uploads

修改后的完整代码示例

$browser = (new BrowserFactory())->createBrowser([
    'windowSize' => [1920, 1080],
    'debug' => true,
    'binary' => '/usr/bin/google-chrome', // 根据系统调整路径
    'arguments' => [
        '--headless=new',
        '--no-sandbox',
        '--disable-gpu',
        '--remote-debugging-port=9222',
        '--disable-dev-shm-usage',
    ],
]);

$page = $browser->createPage();

// 等待页面完全加载完成
$page->navigate($request->url)->waitForNavigation([
    'waitUntil' => 'networkidle0',
    'timeout' => 30000,
]);

$pageTitle = $page->evaluate('forward_documents.print_flow_no_modal',[
    'forward_histories'=>$forward_histories,
    'fulltitle'=>$fullTitle,
    'document'=>$document
])->getReturnValue();

$options = [
    'landscape'           => true,
    'printBackground'     => false,
    'marginTop'           => 0.0, 
    'marginBottom'        => 0.0, 
    'marginLeft'          => 0.0,
    'marginRight'         => 0.0, 
];

$name = public_path("uploads/".time().'.pdf');
$page->pdf($options)->saveToFile($name);

return response()->download($name);

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 07:57:29