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

使用C# ProcessStartInfo调用Edge无头打印PDF失败求助

解决Edge无头模式HTML转PDF返回ExitCode 1002的问题

以下是针对你遇到的Edge转PDF失败(返回1002错误码)的排查和修复方案:

  • 修正Edge路径检测逻辑
    你当前硬编码了32位Edge的安装路径,但64位Windows系统中,Edge的64位版本默认安装在C:\Program Files\Microsoft\Edge\Application\msedge.exe,32位版本才在x86目录。建议同时检测两个路径,优先匹配对应系统位数的版本:

    string edgePath = null;
    // 优先检测64位路径
    if (File.Exists(@"C:\Program Files\Microsoft\Edge\Application\msedge.exe"))
        edgePath = @"C:\Program Files\Microsoft\Edge\Application\msedge.exe";
    // 再检测32位路径
    else if (File.Exists(@"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"))
        edgePath = @"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe";
    
    if (edgePath == null)
    {
        // 处理Edge未安装的异常情况
        return;
    }
    
  • 给文件路径添加双引号
    临时目录路径通常包含空格(比如C:\Users\XXX\AppData\Local\Temp),如果参数中的文件路径未加双引号,Edge会解析失败。修改参数格式如下,同时改用Edge推荐的新版无头模式--headless=new(旧版--headless存在兼容性问题):

    process.StartInfo.Arguments = $"--headless=new --print-to-pdf-no-header --disable-gpu --run-all-compositor-stages-before-draw --print-to-pdf=\"{pdfFile}\" \"{htmlFile}\"";
    
  • 开启错误流捕获定位问题
    当前代码未正确捕获Edge的错误输出,建议开启重定向并读取错误信息,方便排查具体问题:

    process.StartInfo.RedirectStandardError = true;
    process.StartInfo.UseShellExecute = false;
    process.ErrorDataReceived += (sender, e) => 
    {
        if (!string.IsNullOrEmpty(e.Data))
            Console.WriteLine($"Edge错误输出: {e.Data}");
    };
    
    process.Start();
    process.BeginErrorReadLine();
    process.WaitForExit();
    
  • 更新Edge到最新版本
    旧版本Edge的无头模式存在已知bug,确保Edge是最新版,可避免因版本兼容导致的转PDF失败。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 22:26:07