pdftk
来修复一些受损的PDF文件中的问题,但我遇到了另一个问题,pdftk
无法解决(或者至少我不知道该如何解决)。我有一些基于
TrueType
字体的文本PDF文件,但这些字体在PDF创建过程中没有被嵌入。现在我想将所需的字体嵌入到现有文件中。是否有像
pdftk
一样的命令行工具,可以通过提供TTF
文件路径来嵌入丢失的字体?pdftk
来修复一些受损的PDF文件中的问题,但我遇到了另一个问题,pdftk
无法解决(或者至少我不知道该如何解决)。TrueType
字体的文本PDF文件,但这些字体在PDF创建过程中没有被嵌入。现在我想将所需的字体嵌入到现有文件中。pdftk
一样的命令行工具,可以通过提供TTF
文件路径来嵌入丢失的字体?您可以使用 Ghostscript 来嵌入缺失的字体。执行以下命令:
您可以像这样运行命令:
gs \
-o file-with-embedded-fonts.pdf \
-sDEVICE=pdfwrite \
-dEmbedAllFonts=true \
-sFONTPATH="/path/to/ttf;/other/path/to/ttf" \
input-without-embedded-fonts.pdf
另见此答案:
我在Ubuntu 14.04上也遇到了同样的问题,并找到了以下解决方案:
ps2pdf foo.ps foo.pdf
,结果是一个带有嵌入字体和原始内容的文件中间的PostScript文件比输入文件大得多(650KB),但生成的PDF文件的大小适中(82KB)。
我不知道为什么会这样,即:
但结果是一个带有所有字体嵌入的PDF文件,而且大小与原始文件相似。
ps2pdf foo.pdf foo_fixed.pdf
。 - t-bltg如@t-bltg在评论中提到的那样,Ghostscript现在带有一个ps2pdf
命令,可以自动嵌入缺失的字体。
ps2pdf -sFONTPATH="." in.pdf out.pdf
That should be all you need.
The above command presumes the necessary .ttf or .otf file is in the current working directory ("."
). If it is not, you can change .
to directory where the font file lives. For example:
ps2pdf -sFONTPATH="~/.local/share/fonts/" input.pdf output.pdf
(Note, you must specify a directory, not a font file.)
Make sure that the fontname the PDF is looking for is the same as the name embedded in the font file. For example, if the PDF wants a font named "Fantasy Bold", but the font file defines a font named "Fantasy" or "Fantasy Bold Neue LT Pro", this method will not work.
即使没有指定FONTPATH,Ghostscript也知道14种标准的Postscript Level 2字体系列,包括它们的旧名称。例如,现代系统有时无法正确地呈现“New Century Schlbk”,因为它们期望的是“New Century Schoolbook”。一个简单的ps2pdf in.pdf out.pdf
就可以解决这个问题。
gs
默认进行字体子集化(即仅嵌入 PDF 中所使用的字体字形)。可以使用-dSubsetFonts=false
禁用此功能。 - Qtaxgs
标志吗? - Fuhrmanator