Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I feel that the PDFs produced by pdflatex are too huge. So for example my 50 page book with no illustrations is 500KB. I feel it should be more like 50K or even less. The .tex files together are just 40KB. Anyone knows why the final PDF is so huge and is there a way to compress it to make smaller?

share|improve this question
13  
1  
It might be useful to know which TeX system you use. There is more compression in modern TeX Live systems, by for example compressing how hyperlinks are constructed. –  Joseph Wright May 24 '11 at 19:51
1  
It's normally mostly the fonts. Also hyperlinks can add to the size if you have many of them. My 10page package manuals jumped from 120k to 460k just for changing the font and the font encoding. –  Martin Scharrer May 24 '11 at 20:20
    
Does the size difference actually matter? The price of 1MB of hard drive storage is somewhere around 0.001 to 0.01 cent (0.1 for SSD). Bandwidth is probably something like 0.00001 (Amazon S3) to 1 cent (AT&T mobile) per MB. (These measurements are of course completely unscientific.) I'd rather have nice fonts and diagrams. –  Caramdir May 24 '11 at 20:50
1  
If you have Acrobat, you can determine how the space is being used by going to Advanced > PDF Optimizer > Audit space usage –  Emre May 25 '11 at 21:18

2 Answers 2

up vote 54 down vote accepted

A common approach is to let Ghostscript (gs) optimize and compress the PDF after it has been created with pdflatex. Ghostscript is installed by most Linux distributions and easily available for other platforms (Windows as binaries, MacOS via MacPorts). In fact, almost all size-optimizing tools for PDF (save for Acrobat) you can find on the internet, internally use Ghostscript -- so you can as well call it directly. There is a pletoria of options available; I personally use the following:

 gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dNOPAUSE -dQUIET -dBATCH -sOutputFile=foo-compressed.pdf foo.pdf

I use this mostly for beamer presentations, where it gets me a size reduction of 60-70 percent. (A ten MiB lecture note becomes three too four MiB in size.)

share|improve this answer
1  
I wonder if you get the same size reduction if you use \pdfminorversion=5 \pdfcompresslevel=9 \pdfobjcompresslevel=3, i.e. maximum compression with PDF v1.5. –  Martin Scharrer May 25 '11 at 22:39
    
@Martin Scharrer: Not really, with my lecture these options give only a size reduction of 110 Bytes 9825306 no-compression.pdf 9825196 pdflatex-compression.pdf 4318774 gs-compression.pdf 4318772 pdflatex-gs-compression.pdf –  Daniel May 26 '11 at 11:13
    
The numbers before the filename are the actual size. (It kind of bugs me that all newlines get lost when saving a comment...) –  Daniel May 26 '11 at 11:20
1  
@Martin Scharrer: I get interesting results: By setting \pdfcompresslevel=0 \pdfobjectcompresslevel=0 the resulting file size gets smaller (even though only a bit: 9630339 instead of 9825196). Piping this result through gs gives exactly the same size as piping no-compression.pdf through it:4318772. So the bottom line is that (at least for beamer presentations) gs reduces the size quite a bit, independently from the choosen pdflatex compression options. –  Daniel May 26 '11 at 12:04
3  
pdfsizeopt uses this kind of compression through gs as one of the tricks it uses to get smaller PDFs (it also uses a whole tonne of other tricks, so it will generally do better than gs alone). See also this whitepaper on a whole series of techniques for getting smaller PDFs. –  Lev Bishop Jun 6 '11 at 15:41

Here is a revised version of the script above. The basics are unchanged, but it now handles paths and such in a sane manner, automatically copies the new over the old IFF the new file is smaller then the original (and not 0). It also gives some info on space savings. For some unknown reason, running it again can give even smaller output, but usually not by much. The first run gives the big speedup.

For example:

ekl@Taro:~/Documents⟫ optpdf techResume.pdf 
Saving 1245351 bytes (now 6% of old)
ekl@Taro:~/Documents⟫ optpdf techResume.pdf 
Saving 31 bytes (now 99% of old)
ekl@Taro:~/Documents⟫ optpdf techResume.pdf 
Saving 7 bytes (now 99% of old)
ekl@Taro:~/Documents⟫ optpdf techResume.pdf 
Didn't make it smaller! Keeping original

It is legal to give it long paths, but it currently doesn't like spaces in your filenames. Sorry. Getting shell scripts to handle this correctly in all instances is a pain, so just don't don't do it.

Here is the code:

#!/bin/bash
#
# optpdf file.pdf
#   This script will attempt to optimize the given pdf
#
file=$1
filebase=$(basename $file .pdf)
optfile=/tmp/$$-${filebase}_opt.pdf
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dNOPAUSE -dQUIET -dBATCH \
        -sOutputFile=${optfile} ${file}
if [ $? == '0' ]; then
    optsize=$(stat -c "%s" ${optfile})
    orgsize=$(stat -c "%s" ${file})
    if [ "${optsize}" -eq 0 ]; then
        echo "No output!  Keeping original"
        rm -f ${optfile}
        exit;
    fi
    if [ ${optsize} -ge ${orgsize} ]; then
        echo "Didn't make it smaller! Keeping original"
        rm -f ${optfile}
        exit;
    fi
    bytesSaved=$(expr $orgsize - $optsize)
    percent=$(expr $optsize '*' 100 / $orgsize)
    echo Saving $bytesSaved bytes \(now ${percent}% of old\)
    rm ${file}
    mv ${optfile} ${file}
fi

Enjoy!

share|improve this answer
    
Nice that you output actual numbers. For a text only document of about 40 pages (created by ConTeXt), I get a reduction of only 2% in size; but for a 50 page presentation (again created by ConTeXt with lots of images and colors), I get a 40% reduction in size. –  Aditya Sep 2 at 17:47
    
I'm not sure why running it again will reduce it in size more. Its a ghostscript issue. But, its usually the first run that does the big lifting. Its safe to run it over and over - I even considered doing a loop to run it until it won't reduce any more. –  Evan Langlois Sep 20 at 6:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.