Useful Utilities

Embed Fonts in PDF

Figures generated using ggsave are not guaranteed to embed into the file. Although R comes with utility function (in the grDevices) embedFonts() and the extraFonts has a function embed_fonts() for the same purpose. However, sometimes both still fail to embed the font into the document. This blog plot provides a much more robust solution based on ghostscript, which is only accessible via the command line interface. Here is my R wrapper function for this purpose. Credits go to the original post of Mr. Rupp.

grDevices::embedFonts()

or

extrafont::embed_fonts()

My R wrapper function through the system command.

myEmbedFont <- function(infile, outfile) {
  script <- paste0("gs -dNOPAUSE -dBATCH -sDEVICE=pdfwrite ",
                   "-dPDFSETTINGS=/prepress ",
                   "-dEmbedAllFonts=true -sOutputFile=",
                   outfile, " -f ", infile
                   )
  system(script, intern=TRUE, wait=FALSE)
}
Avatar
Hongren Gong
Assistant Professor

My research interests include pavement performance evaluation, automatic pavement distress recognition, infrastructure assest management, traffic safety

Related