Tuesday, May 24, 2011

Ubuntu Tip: Howto reduce PDF file size from command line

Link to the source.

If you want to reduce PDF file size using command line you can use ghostscript.Ghostscript is used for PostScript/PDF preview and printing. Usually as a back-end to a program such as ghostview, it can display PostScript and PDF documents in an X11 environment.

Furthermore, it can render PostScript and PDF files as graphics to be printed on non-PostScript printers. Supported printers include common dot-matrix, inkjet and laser models.

Package gsfonts contains a set of standard fonts for Ghostscript.

Use the following command syntax to reduce your pdf files

gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf

Replace your file names output.pdf, input.pdf files with your file names.

This is only example you can check man gs command for more options.

For  greater file size reduction, change /ebook for /screen but the resolution might not be good enough.

If you want to install ghostscript use the following command

sudo apt-get install ghostscript

------------
Note: You can achieve great reduction in file size using the commands pdftops and then ps2pdf.

Also you can use pdf2ps and then ps2pdf.

Or using the following script from here, and a copy of it here. The usage is the following:
./shrinkpdf.sh  in.pdf  out.pdf
We can optionally specify an output resolution in DPI (default is 72 DPI):
./shrinkpdf.sh  in.pdf  out.pdf  90

--