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

输入文件名含非ASCII字符(如ª)时Ghostscript合并PDF失败求助

解决方案:处理含非ASCII字符的PDF文件名合并问题

问题根源是你用了ANSI编码的PAnsiChar传递文件名,非ASCII字符(如ª、º)会在ANSI编码转换中丢失或乱码,导致GhostScript无法识别文件路径。以下是具体解决步骤:

  • 改用GhostScript宽字符API:替换gsapi_init_with_args为gsapi_init_with_argsW,该版本支持Unicode路径,能正确处理非ASCII字符。
  • 替换参数类型:将所有参数的PAnsiChar改为PWideChar,确保字符串以Unicode格式传递。
  • 优化设备选择:如果目标是合并PDF,建议把ps2write设备换成pdfwrite,这是GhostScript专门用于生成/合并PDF的设备,兼容性和效率更优。

修改后的代码示例:

try
    SetLength(Arg, 7);
    Arg[0] := PWideChar('-q');
    Arg[1] := PWideChar('-dSAFER');
    Arg[2] := PWideChar('-dNOPAUSE');
    Arg[3] := PWideChar('-dBATCH');
    Arg[4] := PWideChar('-sOutputFile=' + output + '.pdf');
    Arg[5] := PWideChar('-sDEVICE=pdfwrite'); // 替换为pdfwrite更适合合并PDF
    Arg[6] := PWideChar(input);

    ExitCode := gsapi_init_with_argsW(instance, Length(Arg), @Arg[0]);

    if ExitCode < 0 
       then raise Exception.Create('ERROR: init_args: '+IntToStr(ExitCode));

    gsapi_exit(instance);
finally
    gsapi_delete_instance(instance);
end;
  • 旧版Delphi适配:若使用Delphi 2009之前的版本,需将字符串转换为WideString后再转PWideChar,确保Unicode编码正确传递。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 19:32:03