How to repair a PDF file and embed missing fonts

30,553

Solution 1

You can use Ghostscript to embed missing fonts. Run the command like this:

gs                                             \
  -o file-with-embedded-fonts.pdf              \
  -sDEVICE=pdfwrite                            \
  -dEmbedAllFonts=true                         \
  -sFONTPATH="/path/to/ttf;/other/path/to/ttf" \
   input-without-embedded-fonts.pdf

See also this answer:

Solution 2

I just had the same problem (on Ubuntu 14.04) and I found the following solution:

  • install Acrobat Reader
  • print "print to file" into a postscript file ("foo.ps") and "advanced -> print as image"
  • then on the console use ps2pdf foo.ps foo.pdf and the result is a file with embedded fonts and the original content

The intermediate postscript file is much bigger (650KB) than the input file (56KB) but the resulting PDF is moderate in size again (82KB).

I do not know why this works, i.e.,

  • why "print as image to file" seems to create an image but also preserves font information,
  • why ps2pdf recovers this font information, and
  • why there are fonts in the resulting PDF at all because it should only be an image, right?.

But the result is a PDF with all fonts embedded and a size similar to the original file.

Share:
30,553
Googlebot
Author by

Googlebot

intentionally left blank

Updated on December 16, 2020

Comments

  • Googlebot
    Googlebot over 3 years

    I use pdftk to repair some failures in corrupted PDF files, but I faced another problem which is not fixed by pdftk (or at least I do not know how to do so).

    I have PDF files with text based on TrueType fonts, but the fonts have not been embedded during PDF creation. Now I want to embed the required fonts to the existing files.

    Is there a command-line tool (like pdftk) to embed missing fonts by providing path to TTF files?

  • fstang
    fstang almost 10 years
    On win32, if you have installed ghostScript, the command may look like: gswin32c -sFONTPATH=C:\Windows\Fonts -o output-pdf-with-embedded-fonts.pdf -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress input-pdf-where-some-fonts-are-not-embedded.pdf (find the exe file on your system, maybe add it to PATH -- the environment variable, if necessary)
  • Backlin
    Backlin over 9 years
    It worked for me by just printing to ps-file, without saving it as an image. Some people complain that pdf -> ps -> pdf conversion is not the way to go, but you seriously rescued my PhD thesis from doom with this post.
  • Kurt Pfeifle
    Kurt Pfeifle over 6 years
    @Qtax: not needed I think -- this is supposed to be the default setting for Ghostscript anyway when embedding fonts.
  • Qtax
    Qtax over 6 years
    @KurtPfeifle you are right! Removing that comment (and this one), and adding one to make people aware that font subsetting is done.
  • Qtax
    Qtax over 6 years
    gs does font subsetting by default when embedding fonts (that is only embedding the glyphs of the font that are used in the PDF). Can be disabled with -dSubsetFonts=false.
  • Fuhrmanator
    Fuhrmanator over 5 years
    This breaks PDF files that have forms in them. Does anyone know of the gs flag that will preserve the forms?
  • Kurt Pfeifle
    Kurt Pfeifle over 5 years
    @Fuhrmanator: FOSS software and PDF forms -- a long chapter in the book "List of important functionality missing or sucking in FOSS"....
  • white_gecko
    white_gecko over 3 years
    I like your approach but changed it in my case. I did not want to use Acrobat so I just did it with evince (standard Gnome PDF reader) print to File, chose Post Script (no image option needed) and then ps2pdf the resulting pdf again. That worked and I did not need to search for the paths for the fonts as necessary for the other answer (stackoverflow.com/a/13131101/414075).
  • t-bltg
    t-bltg over 2 years
    Using ps2pdf directly on the damaged pdf worked for me for fixing fonts: ps2pdf foo.pdf foo_fixed.pdf