Monday, 4 April 2011

Embedding fonts in PDFs using ghostscript without recompiling or reexporting the source text

Well, this has been tackled many times already in a variety of blogs and webpages, but I keep it here as reference.

There is always some crappy program that doesn't embed the fonts when exporting to PDF -- which is needed for some conference papers.

In my case the crappy tool is Matlab. Instead of going back and fiddling around with Matlab to export the graphs again and try to get it to embed fonts, I prefer the following shortcut found here.

Actually the solution in the comments further down works best:

pdftops file.pdf
ps2pdf -dSAFER -dNOPLATFONTS -sPAPERSIZE=letter -dEmbedAllFonts=true -dPDFSETTINGS=/prepress file.ps file.pdf

On FreeBSD you need poppler-utils and ghostscript to get the pftops and ps2pdf commands.

And because I had a bunch of graphs in PDF format without fonts embedded I needed a script to automate the process. Using tcsh it works like that:

cd figures
mkdir non-embedded
mv *.pdf non-embedded
foreach file (non-embedded/*.pdf)
pdftops $file
set psfile = `echo $file | cut -d"." -f1`.ps
set pdffile = `echo $file | cut -d "/" -f2
ps2pdf -dSAFER -dNOPLATFONTS -sPAPERSIZE=letter -dEmbedAllFonts=true -dPDFSETTINGS=/prepress $psfile $pdffile
end

And a similar script can be used to crop the graphs in PDF format using pdfcrop:

foreach file (*.pdf)
pdfcrop $file
set cropped = `echo $file | cut -d "." -f 1`-crop.pdf
rm -rf $file
mv $cropped $file
end

This removes all extra white space around all graphs. Again useful to handle crappy Matlab outputs.

Edit:

And here's the magic command to create a pdf with embedded fonts, optimized and 600dpi resolution (as requested by Sheridan Press for example):

pdftops file.pdf
ps2pdf -dSAFER -dNOPLATFONTS -sPAPERSIZE=letter -dEmbedAllFonts=true -dPDFSETTINGS=/prepress -dOPTIMIZE=true -dCompatibility=1.4 -r600 file.ps file.pdf

No comments:

Post a Comment