Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace all font glyphs in a PDF by converting them to outline shapes

Tags:

I am looking for a way to 'outline' all text/fonts in a PDF file, i.e. convert them to curves.

I would prefer to do this without having to convert the PDF to PostScript and back. Also, I would like to use free lightweight cross-platform tools that can be automated from the command line, such as Ghostscript or MuPDF.

like image 425
Szabolcs Avatar asked Mar 01 '15 18:03

Szabolcs


People also ask

Do I need to outline fonts for a PDF?

If you create a PDF file from your document, all the Adobe Creative Suite applications (and most other applications) will embed all the fonts. Outlining in almost all cases is not necessary.

Why does my font change when I print to PDF?

Sometimes a PDF file looks fine on screen but it prints in an unsightly substitute font which impedes reading, or symbols are replaced by small rectangles. This is due to Adobe trying to re-create the document using its own fonts instead of the document's fonts.


1 Answers

Yes, you can use Ghostscript to achieve what you want.

I. For Ghostscript versions up to 9.14

You need to go through 2 steps:

  1. Convert the PDF to a PostScript file, but use the side effect of a relatively unknown parameter: it is called -dNOCACHE. This will convert all used fonts to outline shapes:

    gs -o somepdf.ps -dNOCACHE -sDEVICE=pswrite somepdf.pdf 
  2. Convert the PS back to PDF (and, maybe delete the intermediate PS again):

    gs -o somepdf-with-outlines.pdf -sDEVICE=pdfwrite somepdf.ps  rm somepdf.ps 

This method is not reliable long-term, because the Ghostscript developers have stated that -dNOCACHE may not be present in future versions.

Note: the resulting PDF will very likely be larger than the original one. Plus, without additional command line parameters, all images in the original PDF will likely also be processed according to Ghostscript builtin defaults. This can lead to unwanted side-effects. Those side-effects can be avoided by adding more command line parameters to do otherwise.


II. Ghostscript versions 9.15 or newer

Ghostscript version 9.15 (released in September 2014) supports a new command line parameter:

 -dNoOutputFonts 

This will cause the output devices pdfwrite, ps2write and eps2write "to 'flatten' glyphs into 'basic' marking operations (rather than writing fonts to the output)".

This means: the two steps described for pre-9.15 GS versions can be avoided. The desired result can be achieved with a single command:

 gs -o file-with-outlines.pdf -dNoOutputFonts -sDEVICE=pdfwrite file.pdf 

Note: the same caveat is true as already noted in part I. If your PDF includes images, there may be unwanted side effects introduced by the simple command line above. To avoid these, you need to add more specific parameters.

like image 153
Kurt Pfeifle Avatar answered Oct 02 '22 15:10

Kurt Pfeifle