How to reduce the file size of a pdf file in linux cli

How to reduce the file size of a pdf file in linux cli

March 8, 2024
  1. Install Ghostscript: If it’s not already installed, you can install Ghostscript using your distribution’s package manager. For Debian-based systems like Ubuntu, use:

    sudo apt-get update
    sudo apt-get install ghostscript
    

    For Red Hat-based systems like Fedora, use:

    sudo dnf install ghostscript
    
  2. Compress the PDF: Once Ghostscript is installed, you can compress your PDF file using the following command:

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

    In this command:

    • input.pdf is the name of your original PDF file.
    • output.pdf is the name of the compressed PDF file that will be created.
    • -dPDFSETTINGS=/screen sets the quality of the PDF. This setting is for the lowest quality and smallest file size, which is suitable for viewing on a screen. Other options include /ebook for medium quality, /printer for high quality, and /prepress for high quality with large file size.

To reduce the compression level in Ghostscript when converting or processing a PDF file, you can adjust the -dPDFSETTINGS option. The command you provided is set to use the /screen setting, which applies high compression and low-resolution output, suitable for viewing on a screen but not ideal for printing or archiving.

The available settings for -dPDFSETTINGS are:

  1. /screen – lowest quality, highest compression.
  2. /ebook – better quality, but still a fair amount of compression.
  3. /printer – high quality, suitable for printing documents.
  4. /prepress – high quality, similar to /printer but with a higher resolution, suitable for printing to a prepress standard.
  5. /default – a useful starting point, balancing quality and compression.

To reduce the compression level (i.e., to increase the quality of the output PDF), you can switch from /screen to a higher quality setting like /ebook, /printer, or /prepress. For example, if you want a balance between quality and file size, you might choose /ebook:

gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile=patient-record-oldbury-health-centre-1.pdf patient-record-oldbury-health-centre.pdf

Or, for higher quality suitable for printing, use /printer:

gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/printer -dNOPAUSE -dQUIET -dBATCH -sOutputFile=patient-record-oldbury-health-centre-1.pdf patient-record-oldbury-health-centre.pdf