5

I have a PDF created with PDF Sandwich. Each page of the PDF contains a background image and some foreground text.

I want to reduce the filesize (not the resolution) of the background image - and preserve the selectable text.

  • Using ImageMagick's convert reduces the size, but removes the text.
  • ps2pdf does not significantly reduce the filesize.
  • gs -sDEVICE=pdfwrite -dPDFSETTINGS=/screen … reduces the resolution, but doesn't seem to re-compress the images.
  • qpdf --compress-streams=y --recompress-flate --optimize-images in.pdf out.pdf only makes a minor difference.

If I extract the background images and use pngquant or similar, the filesize is cut dramatically. But I have no way to use that new image to replace the old image.

Is there any way to compress the images in a PDF while keeping the text intact?


As suggested in the comments, running pdfimages -list output.pdf

Gives:

page   num  type   width height color comp bpc  enc interp  object ID x-ppi y-ppi size ratio
--------------------------------------------------------------------------------------------
   1     0 image    3483  4800  rgb     3   8  jpeg   no        11  0   300   300 1147K 2.3%
   2     1 image    3483  4800  rgb     3   8  jpeg   no        24  0   300   300  397K 0.8%
   3     2 image    3483  4800  rgb     3   8  jpeg   no        37  0   300   300 1441K 2.9%
   4     3 image    3483  4800  rgb     3   8  jpeg   no        50  0   300   300 1880K 3.8%
   5     4 image    3483  4800  gray    1   8  jpeg   no        63  0   300   300 1050K 6.4%
…
2

1 Answer 1

3

qpdf has modes to recompress data streams and especially images within PDFs.

So, something like

qpdf --compress-streams=y --recompress-flate --optimize-images in.pdf out.pdf
2
  • Sadly that only reduces it by a couple of bytes. It doesn't seem to recompress the images. Dec 21, 2021 at 11:17
  • Thanks. That reduced a pdf from 22MB to 2.9MB for me. It seems --optimize-images was important in my case. Adding --compression-level=9 --object-streams=generate saved only a few more KB.
    – user7543
    Oct 11, 2023 at 10:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .