Embedding fonts in a PDF

I recently tried to print a pdf file from my laptop, but the result looked quite weird. The font that was used was Courier, but the spacing was very off, as if it was treated as a proportional font, as it was visible in the orignial file. Turns out that Courier is the default font of the printer and was used because the printer did not have the proper font file for the specified font. Once I figured that out, all that needed to be done was to embed the font into the pdf so that it gets transmitted along with the file that’s supposed to get printed.

After googling around for a bit, I came up with the following script:


# embeds the fonts, if possible, in the pdf and replaces the original
# pdf with the result

FONTPATH=$(fc-list | xargs dirname | sed 's/\.//g;/^$/d' | paste -s -d":" -)

gs                                             \
  -o $TMPPDF                                   \
  -sDEVICE=pdfwrite                            \
  -dEmbedAllFonts=true                         \
  -sFONTPATH="$FONTPATH"                       \

mv $TMPPDF "$1"

The script creates a list of all the fonts, then filters out the empty lines and dots that are produced by dirname and then concatenates the remaining lines with a colon in order to create the font path where gs will look for the font files and then embed the referenced ones into the pdf.

Last modified: