Compressing PDF files is a common need, especially when dealing with large documents. Ghostscript (gs) is a powerful command-line tool that offers excellent PDF compression capabilities. This guide outlines how to use Ghostscript to reduce PDF file size while maintaining readability.
Understanding Ghostscript PDF Compression Levels
Ghostscript offers various compression levels through the -dPDFSETTINGS
parameter. Each setting corresponds to a specific DPI (dots per inch) value, influencing the output quality and file size. Here are three primary compression levels:
1. Low Compression (300 dpi)
This setting preserves high image quality, resulting in a larger file size. Use this for documents requiring sharp images and text, such as print materials.
gs -sDEVICE=pdfwrite -dPDFSETTINGS=/printer -dNOPAUSE -dBATCH -sOutputFile=output.pdf input.pdf
2. Medium Compression (150 dpi) – Recommended
This setting strikes a balance between file size and readability, making it ideal for most use cases like ebooks or online sharing.
gs -sDEVICE=pdfwrite -dPDFSETTINGS=/ebook -dNOPAUSE -dBATCH -sOutputFile=output.pdf input.pdf
3. High Compression (72 dpi)
This setting produces the smallest file size but can significantly reduce image quality. Use cautiously as it might render text and images unreadable, especially for documents with low initial resolution.
gs -sDEVICE=pdfwrite -dPDFSETTINGS=/screen -dNOPAUSE -dBATCH -sOutputFile=output.pdf input.pdf
Additional Ghostscript Options
-dQUIET
: Suppresses Ghostscript’s output messages, useful for clean command-line execution.time
: Prefixing the command withtime
measures the execution duration, helpful for performance analysis. For example:time gs -sDEVICE=pdfwrite -dPDFSETTINGS=/ebook -dNOPAUSE -dBATCH -sOutputFile=output.pdf input.pdf
Alternative: ps2pdf
ps2pdf
is a wrapper around Ghostscript and offers similar functionality. While ps2pdf
simplifies the command structure, using gs
directly provides more control over the compression process.
Conclusion
Ghostscript is a versatile tool for compressing PDF files. By understanding the different compression levels and utilizing appropriate settings, you can significantly reduce file size without compromising readability. The medium compression setting (/ebook
) often provides the best balance for general use. For specific requirements like printing or archiving, adjust the settings accordingly. Remember to test different options to find the optimal balance between file size and quality for your individual needs.