Wednesday, October 10, 2007

PDF and included images

A little earlier in the evening, I had to send a document to my parents including pictures. Though the final version definitely should have high-quality pictures, I was forced to see that the version I sent was maybe slightly too big (5MB by email, when the receiver doesn't have a very fast connection, that is painful). After some experimentations with pdftops (from xpdf) and ps2pdf (from ghostscript) that didn't give satisfying results, I tried to use ghostscript directly:
~ gs -sOutputFile=biniou.pdf -sDEVICE=pdfwrite \
  -dCompatibilityLevel=1.4 FairePart.pdf  < /dev/null
What a surprise ! The output did shrink by a factor of 4:
~ ls -lh FairePart.pdf biniou.pdf
-rw-r--r-- 1 vincent vincent 1.1M 2007-10-10 22:56 biniou.pdf
-rw-r--r-- 1 vincent vincent 5.7M 2007-10-10 18:14 FairePart.pdf
A quick check with pdfimages combined with identify shows that all the images kept the same resolution. That must be a question of JPEG conversion, or something of this spirit... Then, another try is to add the -dPDFSETTINGS=/screen option to the command-line:
~ gs -sOutputFile=biniou.pdf -sDEVICE=pdfwrite \
  -dPDFSETTINGS=/screen -dCompatibilityLevel=1.4 
  FairePart.pdf  < /dev/null
Then, output file is minuscule:
~ ll biniou.pdf 
-rw-r--r-- 1 vincent vincent 34K 2007-10-11 00:09 biniou.pdf
The downside is, the output is pretty much ugly (well, you wouldn't have hoped anything good with a win of a factor of 150). Images went down from 2576x1932 to 322x241 or even smaller (depends on the physical size of the image). -dPDFSETTINGS=/ebook gave a slightly better output (for 70K), but still not good for my case... So I tweaked the pdfwrite parameters by hand:
 gs -sOutputFile=biniou.pdf -sDEVICE=pdfwrite \
  -dColorImageDownsampleType=/Bicubic -dColorImageResolution=300 \
  -dDownsampleColorImages=true -dCompatibilityLevel=1.4 \
  FairePart.pdf  < /dev/null
This gave me a pretty nice result. And this also shows that my image resolution was way too big anyway - 300 dpi is probably the best I'll get when printing... and the file produced is still ridiculously small (172K) ! I now start to realize the power of ghostscript, and I thank its authors for it !

No comments: