Creating high-quality PDF files from LaTeX documents

The usual way of creating PDF files in Linux is by using ps2pdf. This is the default script on many systems that parses the command line and calls ghostscript (gs):
OPTIONS="-dSAFER"
while true
do
    case "$1" in
    -?*) OPTIONS="$OPTIONS $1" ;;
    *)  break ;;
    esac
    shift
done
infile="$1"
if [ $# -eq 1 ]
then
   case "${infile}" in
     -)      outfile=- ;;
     *.eps)  base=`basename "${infile}" .eps`; outfile="${base}.pdf" ;;
     *.ps)   base=`basename "${infile}" .ps`; outfile="${base}.pdf" ;;
     *)      base=`basename "${infile}"`; outfile="${base}.pdf" ;;
   esac
else
   outfile="$2"
fi

gs $OPTIONS -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite \
   -sOutputFile="$outfile" $OPTIONS -c \
   .setpdfwrite -f "$infile"
Usage:
ps2pdf myfile.ps myfile.pdf

Unfortunately, when viewed in Acrobat reader, the quality of the text fonts in the PDFs produced by the above script is very poor because of the type of image compression that is used. The following script, which creates PDFs from .dvi files, produces PDFs with high-quality text rendering, at the cost of making the PDF file larger:
# script to make pdf files with good quality text
# also turn off image compression  in pdf file.
dvips -Pamz -Pcmz -o $1.ps $1.dvi
ps2pdf -dMaxSubsetPct=100 -dCompatibilityLevel=1.2  -dSubsetFonts=true \
       -dEmbedAllFonts=true -dAutoFilterColorImages=false \
       -dAutoFilterGrayImages=false -dColorImageFilter=/FlateEncode \
       -dGrayImageFilter=/FlateEncode \
       -dModoImageFilter=/FlateEncode  $1.ps

( Note: I can't take credit for this trick. It was discovered by someone on the Internet.)

Usage:
dvi2pdf myfile
(where "myfile" is a .dvi file). Here is an example of a typical LaTeX file converted to PDF using the above script, viewed in Acroread:
good formula

Compare this with a PDF produced by the normal method: bad formula

ps2gif

I also use a customized ps2gif, which makes .gif files that are larger than the default. This script requires the pbmplus utilities.
#!/bin/sh
if [ $# != 2 ] ; then
   echo " Usage: ps2gif <file.ps> <file.gif>" 1>&2
   exit 1
else 
   echo "Calling ghostscript to convert, please wait ..." >&2
   gs -sDEVICE=ppmraw -r240x240 -sOutputFile=- -sNOPAUSE \
      -q $1 -c showpage -c quit | pnmcrop| pnmmargin 1| \
      ppmquant 256 | ppmtogif >$2
fi

Back