使用Ghostscript合并PDF时字体损坏问题的排查与修复咨询
尝试用Ghostscript合并PDF文件时遇到字体问题:标准拉丁文字符显示正常,但非标准字符(疑似CID字体)显示异常,尽管原文件和输出文件标称使用同一字体。
执行的命令及输出(已整理换行和编号):
$ gs -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=out.pdf in.pdf GPL Ghostscript 9.26 (2018-11-20) Copyright (C) 2018 Artifex Software, Inc. All rights reserved. This software comes with NO WARRANTY: see the file PUBLIC for details. Processing pages 1 through 1. Page 1 -- 1 Can't find (or can't open) font file /usr/share/ghostscript/9.26/Resource/Font/TimesNewRomanPSMT. Can't find (or can't open) font file TimesNewRomanPSMT. Can't find (or can't open) font file /usr/share/ghostscript/9.26/Resource/Font/TimesNewRomanPSMT. Can't find (or can't open) font file TimesNewRomanPSMT. Querying operating system for font files... Can't find (or can't open) font file /usr/share/ghostscript/9.26/Resource/Font/TimesNewRomanPSMT. Can't find (or can't open) font file TimesNewRomanPSMT. Didn't find this font on the system! Substituting font Times-Roman for TimesNewRomanPSMT. Loading NimbusRoman-Regular font from /usr/share/ghostscript/9.26/Resource/Font/NimbusRoman-Regular... 5046332 3572454 3115872 1726420 4 done. -- 2 Can't find CID font "TimesNewRoman". Attempting to substitute CID font /Adobe-Identity for /TimesNewRoman, see doc/Use.htm#CIDFontSubstitution. The substitute CID font "Adobe-Identity" is not provided either. attempting to use fallback CIDFont.See doc/Use.htm#CIDFontSubstitution. Loading a TT font from /usr/share/ghostscript/9.26/Resource/CIDFSubst/DroidSansFallback.ttf to emulate a CID font Adobe-Identity ... Done. -- 3 Can't find (or can't open) font file /usr/share/ghostscript/9.26/Resource/Font/TimesNewRomanPS-ItalicMT. Can't find (or can't open) font file TimesNewRomanPS-ItalicMT. Can't find (or can't open) font file /usr/share/ghostscript/9.26/Resource/Font/TimesNewRomanPS-ItalicMT. Can't find (or can't open) font file TimesNewRomanPS-ItalicMT. Didn't find this font on the system! Substituting font Times-Italic for TimesNewRomanPS-ItalicMT. Loading NimbusRoman-Italic font from /usr/share/ghostscript/9.26/Resource/Font/NimbusRoman-Italic... 9238300 7844767 3270416 1870892 4 done. -- 4 Can't find CID font "TimesNewRoman". Attempting to substitute CID font /Adobe-Identity for /TimesNewRoman, see doc/Use.htm#CIDFontSubstitution. Can't find (or can't open) font file /usr/share/ghostscript/9.26/Resource/Font/TimesNewRomanPS-BoldMT. Can't find (or can't open) font file TimesNewRomanPS-BoldMT. Can't find (or can't open) font file /usr/share/ghostscript/9.26/Resource/Font/TimesNewRomanPS-BoldMT. Can't find (or can't open) font file TimesNewRomanPS-BoldMT. Didn't find this font on the system! Substituting font Times-Bold for TimesNewRomanPS-BoldMT. Loading NimbusRoman-Bold font from /usr/share/ghostscript/9.26/Resource/Font/NimbusRoman-Bold... 9486596 8090953 3465360 2062449 4 done. -- 5 Can't find CID font "TimesNewRoman". Attempting to substitute CID font /Adobe-Identity for /TimesNewRoman, see doc/Use.htm#CIDFontSubstitution. Can't find (or can't open) font file /usr/share/ghostscript/9.26/Resource/Font/Arial-BoldMT. Can't find (or can't open) font file Arial-BoldMT. Can't find (or can't open) font file /usr/share/ghostscript/9.26/Resource/Font/Arial-BoldMT. Can't find (or can't open) font file Arial-BoldMT. Didn't find this font on the system! Substituting font Helvetica-Bold for Arial-BoldMT. Loading NimbusSans-Bold font from /usr/share/ghostscript/9.26/Resource/Font/NimbusSans-Bold... 9735428 8335065 3720904 2297241 4 done. -- 6 Can't find CID font "Arial". Attempting to substitute CID font /Adobe-Identity for /Arial, see doc/Use.htm#CIDFontSubstitution. Can't find CID font "Arial". Attempting to substitute CID font /Adobe-Identity for /Arial, see doc/Use.htm#CIDFontSubstitution.
咨询问题
- CID font究竟是什么?
- 如何解决字体显示异常问题?
- 如何通过Ghostscript将原PDF中的嵌入字体复制到新生成的PDF中?
1. 什么是CID字体?
CID(Character Identifier)字体是专为大字符集设计的字体格式,常用于中文、日文、韩文这类包含大量字符的语言场景。和传统PostScript字体最多支持256个字符的限制不同,CID字体通过唯一的字符ID映射字符,能容纳上万甚至更多字符,专门解决多文种、复杂字符的排版需求。日志里提到的TimesNewRoman、Arial CID字体,是对应西文字体的大字符集版本,支持非标准拉丁字符或其他语言字符。
2. 解决字体显示异常的方法
从日志可见,Ghostscript找不到系统里的TimesNewRoman、Arial系列CID字体,只能用默认替代字体(比如Nimbus系列、DroidSansFallback),这些替代字体没有对应非标准字符的字形,导致显示异常。可通过以下方法解决:
方法一:安装缺失的字体文件
把Times New Roman、Arial的完整TrueType字体包(包括常规、粗体、斜体等所有变体)安装到系统字体目录,或者直接复制到Ghostscript的字体资源目录(/usr/share/ghostscript/9.26/Resource/Font/)。安装完成后,Ghostscript就能找到对应字体,无需替换,非标准字符即可正常显示。方法二:强制使用原PDF的嵌入字体
如果原PDF已经嵌入了所需字体,添加参数让Ghostscript优先使用嵌入字体,不要去系统搜索缺失字体:
gs -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dEmbedAllFonts=true -dSubsetFonts=false -sOutputFile=out.pdf in.pdf
-dEmbedAllFonts=true:强制将所有字体嵌入到输出PDF-dSubsetFonts=false:禁止字体子集化,完整保留原字体的所有字符方法三:指定自定义字体路径
如果字体存放在非默认目录,用-sFONTPATH参数告诉Ghostscript字体位置:
gs -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sFONTPATH=/path/to/your/font/folder -sOutputFile=out.pdf in.pdf
3. 复制原PDF嵌入字体到新PDF的方法
使用以下参数组合的Ghostscript命令,可完整保留原PDF中的嵌入字体:
gs -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dEmbedAllFonts=true -dSubsetFonts=false -dPreserveCopyPage=true -sOutputFile=out.pdf in.pdf
-dEmbedAllFonts=true:确保所有字体都嵌入到输出文件-dSubsetFonts=false:不做字体子集化,完整复制原嵌入字体的全部字符集-dPreserveCopyPage=true:尽可能保留原页面的资源结构,减少Ghostscript对字体的修改,最大程度还原原字体状态
内容的提问来源于stack exchange,提问作者JohnSmith

