Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

imagemagick reduce size of pdf

I need to automatically reduce the size of some user uploaded pdfs so that they can be sent via email.

I have a little imagemagick oneliner that reduces the size for me:

convert -density 120 -quality 10 -compress jpeg original.pdf output.pdf

basically exports every page of the pdf in jpg, updates density and quality and repacks the pages in a new PDF.

this works perfectly, except that with this command sometimes the files end up bigger, and I need to rerun tweaking density and quality to get the lowest size where the text in the pdf documents is still readable.

I'm not sure how to automate it. I thought to use identify to get characteristics of the files (height width density... ) and do stuff like half the figures or sth similar. but I'm struggling to get this info about the files.

Any suggestions?

Thanks,

like image 777
Don Giulio Avatar asked Sep 09 '25 20:09

Don Giulio


1 Answers

Addendum: Parameter -compress jpeg must be explicitly submitted or else you will end up with uncompressed image assets in your PDF:

$ cd ~/Pictures/Scans/

$ pdfimages -list Test.pdf 
page   num  type   width height color comp bpc  enc interp  object ID x-ppi y-ppi size ratio
--------------------------------------------------------------------------------------------
   1     0 image    4961  7016  icc     3   8  jpeg   yes        5  0   600   600 5907K 5.8%

$ convert -density 150 -quality 60  Test.pdf Test-150-060.pdf 

$ pdfimages -list Test-150-060.pdf 
page   num  type   width height color comp bpc  enc interp  object ID x-ppi y-ppi size ratio
--------------------------------------------------------------------------------------------
   1     0 image    1240  1754  rgb     3   8  image  no         8  0   150   150 6397K 100%
   1     1 smask    1240  1754  gray    1   8  image  no         8  0   150   150 33.5K 1.6%

$ convert -density 150 -quality 60 -compress jpeg  Test.pdf Test-150-060-jpeg.pdf 

$ pdfimages -list Test-150-060-jpeg.pdf 
page   num  type   width height color comp bpc  enc interp  object ID x-ppi y-ppi size ratio
--------------------------------------------------------------------------------------------
   1     0 image    1240  1754  rgb     3   8  jpeg   no         8  0   150   150 42.5K 0.7%
   1     1 smask    1240  1754  gray    1   8  image  no         8  0   150   150 33.5K 1.6%
like image 138
porg Avatar answered Sep 13 '25 14:09

porg