简体   繁体   English

如何使用 Ghostscript 将 TTF 格式的字体嵌入到 PDF 中?

[英]How to use Ghostscript to embed fonts in TTF-Format into PDFs?

Atm I want to use GhostScript v9.26 (on Debian) and embed fonts into a PDF. Atm 我想使用 GhostScript v9.26(在 Debian 上)并将字体嵌入到 PDF 中。 I use " Roboto " font.我使用“ Roboto ”字体。 But everytime I use GhostScript to embed the font it does not embed Roboto, but instead it embeds " Helvetica " & " Arial " which do not even get used in my PDF.但是每次我使用 GhostScript 嵌入字体时,它都没有嵌入 Roboto,而是嵌入了“ Helvetica ”和“ Arial ”,它们甚至没有在我的 PDF 中使用。

I think this just falls back to some other fonts as it can not load my Roboto.ttf font with我认为这只是回退到其他一些字体,因为它无法加载我的 Roboto.ttf 字体

-sFONTPATH='../../templates/fonts/Roboto/'

I heard I first have to convert my TTF font to some kind of FontMap.我听说我首先必须将我的 TTF 字体转换为某种 FontMap。 But how exactly to do so?但是具体怎么做呢?

My command for executing the GhostScript (from PHP) is this:我执行 GhostScript(来自 PHP)的命令是这样的:

exec("gs -o '../../archiv/tmp/Ghost.pdf' -sDEVICE=pdfwrite -sFONTPATH='../../templates/fonts/Roboto/' '../../archiv/tmp/source.pdf'");

source.pdf does not contain anything related to any other font then "Roboto" I have doublechecked this with Adobe Acrobat DC Pro. source.pdf不包含与任何其他字体相关的任何内容,然后“Roboto”我已经用 Adob​​e Acrobat DC Pro source.pdf过。 But Ghost.pdf does.但是Ghost.pdf可以。 It does have all 3 fonts and most important.它确实有所有 3 种字体,最重要的是。 The parts which have been " Roboto " but not embedded yet are now Arial or Helvetica .已经“ Roboto ”但尚未嵌入的部分现在是ArialHelvetica The already embedded parts ofc stay how they have been before.已经嵌入的部分 ofc 保持原样。

Example source.pdf (Screenshot):示例 source.pdf(截图): 在此处输入图片说明 ( Roboto ) ( Roboto )

Ghost.pdf (Screenshot): Ghost.pdf(截图): 在此处输入图片说明 ( Arial ) ( Arial )

Do I have to convert the TTF fonts to OTF/WOFF/WOFF2/SVG or any other common format to use them with GhostScript?我是否必须将 TTF 字体转换为 OTF/WOFF/WOFF2/SVG 或任何其他常见格式才能在 GhostScript 中使用它们?

The TTF fonts can be used directly by ghostscript as you figured out.如您所知,TTF 字体可以由 ghostscript 直接使用。 The -sFONTPATH also worked for my tests to result in TTF sourced embedded fonts with pdfwrite as you figured out again. -sFONTPATH 也适用于我的测试,如您再次发现的那样,使用 pdfwrite 生成 TTF 来源的嵌入字体。 So this must be a php problem.所以这一定是php的问题。 Maybe an absolute path in the -sFONTPATH would be better in case php is changing directories.也许 -sFONTPATH 中的绝对路径会更好,以防 php 更改目录。

Various fonts can also be copied to a working directory and then just use the gs -P option to find fonts in the current directory.也可以将各种字体复制到工作目录,然后只需使用 gs -P 选项即可在当前目录中查找字体。 This requires the font be specified by "font file name" like this for example:这需要通过“字体文件名”指定字体,例如:

/Roboto-Regular.ttf 12 selectfont

I just tested renaming the Roboto-Regular.ttf to just the basename of Roboto-Regular and ghostscript was able to correctly findfont in the current directory using gs -P so all of the font names don't need to be changed if helpful:我刚刚测试了将 Roboto-Regular.ttf 重命名为 Roboto-Regular 的基本名称,ghostscript 能够使用 gs -P 在当前目录中正确找到字体,因此如果有帮助,则不需要更改所有字体名称:

/Roboto-Regular 12 selectfont

EDIT: To add a TTF font to the Fontmap just find the actual font name used by ghostscript and add to the Fontmap.编辑:要将 TTF 字体添加到 Fontmap,只需找到 ghostscript 使用的实际字体名称并将其添加到 Fontmap。 The fontforge is an easy way to find the font name because that is sometimes difficult with a TTF file. fontforge 是一种查找字体名称的简单方法,因为这对于 TTF 文件有时很困难。 Another way is to convert the TTF to type42 just to look in the file and locate /FontName.另一种方法是将 TTF 转换为 type42,以便在文件中查找并找到 /FontName。 Then add to the Fontmap like this if using a system wide Fontmap or without the full path for just a directory Fontmap (I wonder if -sFONTPATH is optional with a full path):然后,如果使用系统范围的 Fontmap 或没有目录 Fontmap 的完整路径,则像这样添加到 Fontmap (我想知道 -sFONTPATH 是否可以选择带有完整路径):

(Roboto-Black)          (/usr/local/share/fonts/Roboto-Black.ttf)       ;
(Roboto-BlackItalic)            (/usr/local/share/fonts/Roboto-BlackItalic.ttf) ;
(Roboto-Bold)           (/usr/local/share/fonts/Roboto-Bold.ttf)        ;
(Roboto-BoldItalic)             (/usr/local/share/fonts/Roboto-BoldItalic.ttf)  ;
(Roboto-Italic)         (/usr/local/share/fonts/Roboto-Italic.ttf)      ;
(Roboto-Light)          (/usr/local/share/fonts/Roboto-Light.ttf)       ;
(Roboto-LightItalic)            (/usr/local/share/fonts/Roboto-LightItalic.ttf) ;
(Roboto-Medium)         (/usr/local/share/fonts/Roboto-Medium.ttf)      ;
(Roboto-MediumItalic)           (/usr/local/share/fonts/Roboto-MediumItalic.ttf)        ;
(Roboto-Regular)                (/usr/local/share/fonts/Roboto-Regular.ttf)     ;
(Roboto-Thin)           (/usr/local/share/fonts/Roboto-Thin.ttf)        ;
(Roboto-ThinItalic)             (/usr/local/share/fonts/Roboto-ThinItalic.ttf)  ;
(RobotoCondensed-Bold)          (/usr/local/share/fonts/RobotoCondensed-Bold.ttf)       ;
(RobotoCondensed-BoldItalic)            (/usr/local/share/fonts/RobotoCondensed-BoldItalic.ttf) ;
(RobotoCondensed-Italic)                (/usr/local/share/fonts/RobotoCondensed-Italic.ttf)     ;
(RobotoCondensed-Light)         (/usr/local/share/fonts/RobotoCondensed-Light.ttf)      ;
(RobotoCondensed-LightItalic)           (/usr/local/share/fonts/RobotoCondensed-LightItalic.ttf)        ;
(RobotoCondensed-Regular)               (/usr/local/share/fonts/RobotoCondensed-Regular.ttf)    ;
(RobotoMono-Bold)               (/usr/local/share/fonts/RobotoMono-Bold.ttf)    ;
(RobotoMono-BoldItalic)         (/usr/local/share/fonts/RobotoMono-BoldItalic.ttf)      ;
(RobotoMono-Italic)             (/usr/local/share/fonts/RobotoMono-Italic.ttf)  ;
(RobotoMono-Light)              (/usr/local/share/fonts/RobotoMono-Light.ttf)   ;
(RobotoMono-LightItalic)                (/usr/local/share/fonts/RobotoMono-LightItalic.ttf)     ;
(RobotoMono-Medium)             (/usr/local/share/fonts/RobotoMono-Medium.ttf)  ;
(RobotoMono-MediumItalic)               (/usr/local/share/fonts/RobotoMono-MediumItalic.ttf)    ;
(RobotoMono-Regular)            (/usr/local/share/fonts/RobotoMono-Regular.ttf) ;
(RobotoMono-Thin)               (/usr/local/share/fonts/RobotoMono-Thin.ttf)    ;
(RobotoMono-ThinItalic)         (/usr/local/share/fonts/RobotoMono-ThinItalic.ttf)      ;
(RobotoSlab-Bold)               (/usr/local/share/fonts/RobotoSlab-Bold.ttf)    ;
(RobotoSlab-Light)              (/usr/local/share/fonts/RobotoSlab-Light.ttf)   ;
(RobotoSlab-Regular)            (/usr/local/share/fonts/RobotoSlab-Regular.ttf) ;
(RobotoSlab-Thin)               (/usr/local/share/fonts/RobotoSlab-Thin.ttf)    ;

This also works:这也有效:

/Roboto-Regular  (/usr/share/fonts/TTF/Roboto-Regular.ttf) ;

EDIT2: Also, there is a perl program called ttfontmap to easily generate a Fontmap file without needing to find the /FontName first except the ttfontmap depends on the ttf2pt1 program. EDIT2:此外,还有一个名为ttfontmap的 perl 程序可以轻松生成 Fontmap 文件,而无需先找到 /FontName,除非 ttfontmap 依赖于 ttf2pt1 程序。

Just found putting a Fontmap file in a working directory finds the fonts correctly with gs -P option.刚刚发现将 Fontmap 文件放在工作目录中可以使用 gs -P 选项正确找到字体。 For example:例如:

/Candara-Bold    (Candara_Bold.ttf);
/Hack-Regular    (/usr/share/fonts/TTF/Hack-Regular.ttf);

Notice where the Candara file name is not the same as the FontName and the fonts can be anywhere.注意 Candara 文件名与 FontName 不同的地方,字体可以在任何地方。 Just be sure to run gs -P in the same directory with the custom Fontmap needed.只要确保在与所需的自定义字体映射相同的目录中运行 gs -P 即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM