📜  linux compress pdf - Shell-Bash (1)

📅  最后修改于: 2023-12-03 14:43:54.951000             🧑  作者: Mango

Linux compress pdf - Shell-Bash

As a programmer, you might often need to manipulate PDF files, and sometimes you need to compress them to reduce their size. In this tutorial, we will show you how to compress PDF files in Linux using the Shell/Bash commands.

Requirements
  • Linux operating system (Ubuntu, Debian, CentOS, etc.)
  • Ghostscript package (usually pre-installed)
Compressing PDF files

To compress a PDF file, you need to use a command-line tool called gs which is part of the Ghostscript package. The gs tool can compress PDF files by removing unnecessary data, optimizing fonts, and reducing image quality.

Here is the basic command to compress a PDF file:

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

Let's break down this command and explain each parameter:

  • -sDEVICE=pdfwrite: This parameter tells gs to write the output as a new PDF file.
  • -dCompatibilityLevel=1.4: This parameter sets the PDF version to 1.4, which is supported by most PDF viewers.
  • -dPDFSETTINGS=/screen: This parameter sets the PDF output for screen viewing, with a low-resolution output of 72 dpi.
  • -dNOPAUSE: This parameter tells gs not to stop the output between pages.
  • -dQUIET: This parameter tells gs not to print any messages during the compression process.
  • -dBATCH: This parameter tells gs to exit after processing the input file.
  • -sOutputFile=output.pdf: This parameter specifies the name of the output file.
  • input.pdf: This is the input file that you want to compress.

After running this command, you should see a new file named output.pdf in the same directory as the input file.

Conclusion

Compressing PDF files can help reduce file size and make them easier to share or upload to cloud storage. By using the gs tool, you can easily compress PDF files in Linux with the Shell/Bash commands.

You can also create a shell script and run it for multiple PDF files.

Happy coding!