简体   繁体   中英

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. I use " Roboto " font. 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.

I think this just falls back to some other fonts as it can not load my Roboto.ttf font with

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

I heard I first have to convert my TTF font to some kind of FontMap. But how exactly to do so?

My command for executing the GhostScript (from PHP) is this:

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. But Ghost.pdf does. It does have all 3 fonts and most important. The parts which have been " Roboto " but not embedded yet are now Arial or Helvetica . The already embedded parts ofc stay how they have been before.

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

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

Do I have to convert the TTF fonts to OTF/WOFF/WOFF2/SVG or any other common format to use them with GhostScript?

The TTF fonts can be used directly by ghostscript as you figured out. The -sFONTPATH also worked for my tests to result in TTF sourced embedded fonts with pdfwrite as you figured out again. So this must be a php problem. Maybe an absolute path in the -sFONTPATH would be better in case php is changing directories.

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. 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 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. The fontforge is an easy way to find the font name because that is sometimes difficult with a TTF file. Another way is to convert the TTF to type42 just to look in the file and locate /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):

(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.

Just found putting a Fontmap file in a working directory finds the fonts correctly with gs -P option. 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. Just be sure to run gs -P in the same directory with the custom Fontmap needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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