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

Ghostscript转换PDF页面为PNG时缺失文本的问题求助

Ghostscript转PDF为PNG时文本缺失(仅保留表格边框)

问题详情

使用Ghostscript将PDF页面转换为PNG图片时,生成的PNG仅保留原PDF中的表格边框,单元格内文本全部缺失。

生成结果:
生成结果

使用的命令:

gs -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 \
'-sDEVICE=png16m' -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dBackgroundColor=16#ffffff '-r300x300' \
-sOutputFile=/tmp/testpdf/res1.png /tmp/testpdf/1.pdf

执行命令后的输出:

GPL Ghostscript 9.56.1 (2022-04-04)
Page 1
Loading CIDFont TimesNewRoman substitute from /usr/share/ghostscript/Resource/CIDFSubst/DroidSansFallback.ttf
Loading CIDFont TimesNewRoman,Bold substitute from /usr/share/ghostscript/Resource/CIDFSubst/DroidSansFallback.ttf
Loading font Times-Roman (or substitute) from /usr/share/ghostscript/Resource/Font/NimbusRoman-Regular

运行环境:Amazon Linux 2023

原因分析

  • CID字体替换失效:从输出可见,Ghostscript尝试用DroidSansFallback.ttf替代TimesNewRoman系列CID字体,但该 fallback 字体可能不包含PDF中文本对应的字符,或字体替换逻辑存在兼容性问题,导致文本无法渲染。
  • 系统字体缺失:Amazon Linux 2023默认的Ghostscript字体包缺少完整的PostScript标准字体(如Times系列),只能用替代字体,而替代字体匹配度不足。
  • 旧版本Ghostscript bug:9.56.1版本存在已知的CID字体渲染问题,后续版本已修复该类兼容性bug。

解决方案

  1. 安装完整标准字体包
    在Amazon Linux 2023上安装URW Base 35字体包(包含PostScript标准字体):

    sudo dnf install urw-base35-fonts
    

    安装后,Ghostscript会优先使用这些标准字体,避免无效的 fallback 替换。

  2. 指定字体路径参数
    在转换命令中添加-sFONTPATH参数,指向标准字体目录,确保Ghostscript能找到正确字体:

    gs -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 \
    -sDEVICE=png16m -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dBackgroundColor=16#ffffff \
    -r300x300 -sFONTPATH=/usr/share/fonts/urw-base35 \
    -sOutputFile=/tmp/testpdf/res1.png /tmp/testpdf/1.pdf
    
  3. 更新Ghostscript到最新版本
    尝试更新系统中的Ghostscript到最新稳定版,修复旧版本的字体渲染bug:

    sudo dnf update ghostscript
    

    若系统repo无最新版,可从官方渠道下载源码编译安装。

  4. 调整渲染参数
    添加-dNOCCF参数禁用字体缓存,避免缓存导致的字体加载异常:

    gs -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 \
    -sDEVICE=png16m -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dBackgroundColor=16#ffffff \
    -r300x300 -dNOCCF -sOutputFile=/tmp/testpdf/res1.png /tmp/testpdf/1.pdf
    

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

相关产品推荐
方舟 Agent Plan

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

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