Tuesday, December 02, 2014

Making a huge PDF into a reasonably-sized one (by tossing superfluous bits) ... in gs(1)

Short version:
$ gs -o reasonable.pdf -sDEVICE=pdfwrite -r300x300 huge.pdf
or, to turn foo-big.pdffoo.pdf and save a little typing, bash and some other shells allow:
$ gs -sDEVICE=pdfwrite -r300x300  -o foo{,-big}.pdf
Last week, I wrote an annual update, a family "newsletter" of sorts. This time, because of the baby (our first grandchild) we had more photos than usual. (We briefly considered having him in every photo, but there actually is one where Baby isn't visible.) I did this in OpenOffice, and then, to send to the printer, I clicked on the "PDF" button. The resulting file was, ahem, huge.
-rw-r--r-- 1 collin 19067843 Nov 30 20:01 Dec2014.pdf
Yes, over 19 million bytes; it was probably because the photos in there were too big. I told openoffice to make them about 2 inches high or whatever, but I guess all the bits were still there (the ".odt" file itself was nearly 12 Mbytes). I did a web search on "downsample pdftk" (no quotes), which led me to this hint, briefly
gs \
  -o output.pdf \
  -sDEVICE=pdfwrite \
  -dPDFSETTINGS=/screen \
   input.pdf
I tried this, and the output shrank down to about 70 kbytes. Impressive, but the photos were quite ugly. I mean, they looked OK on the screen, but for printing? Blech. I guess that's what /screen means.

Remembering the old saying, rtfm, I said "man gs" and found:

   Some devices can support different resolutions (densities).  To specify
   the resolution on such a printer, use the "-r" switch:

 gs -sDEVICE= -rx

   For  example,  on a 9-pin Epson-compatible printer, you get the lowest-
   density (fastest) mode with

 gs -sDEVICE=epson -r60x72
"Aha!" I thought. Let me do this:
$ gs -o output.pdf -sDEVICE=pdfwrite -r300x300 Dec2014.pdf
and the result, besides being under 1MB in size, looked great in okular, which is the PDF viewer on my Linux distro.