Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to repair a PDF file and embed missing fonts

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?

like image 302
Googlebot Avatar asked Oct 12 '12 11:10

Googlebot


2 Answers

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:

  • How do I embed fonts in an existing PDF?
like image 168
Kurt Pfeifle Avatar answered Oct 06 '22 23:10

Kurt Pfeifle


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.

like image 26
peschü Avatar answered Oct 06 '22 22:10

peschü