Skip to content

LarsEjaas/compress-pdf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 

Repository files navigation

πŸ—œοΈ Compress PDF - Bash Script for Windows

A simple Bash script to compress PDF files on Windows.

It works by iteratively downsampling the resolution of images within the PDF to meet a desired target file size.

✨ Features

  • Target Size Compression: Attempts to compress a PDF to a specified target size in kilobytes.
  • Iterative Approach: Starts with a higher image resolution and gradually reduces it until the target file size is achieved.
  • Safe: Creates a new compressed file (<filename>_compressed.pdf) and never modifies the original.
  • Customizable: Easily change the target file size, and tweak resolution parameters within the script.

πŸ“‹ Requirements

This script is designed to run on Windows within a Bash environment.

🐚 Bash Shell for Windows

  • Git Bash (Recommended, comes with Git for Windows).

πŸ‘» Ghostscript

  • Download and Install: You must have the 64-bit version of Ghostscript installed. You can download it from the official website: Ghostscript - Releases - pick the latest Win 64bit AGPL release.

  • Test the Installation: After running the .exe installation file and completing the installation process, open a new Git Bash or Command Prompt window and run the following command to verify that Ghostscript is accessible:

    gswin64c --version

    If it prints a version number, you are all set!

πŸš€ Script Installation

  1. Download the compress-pdf.sh script to a folder on your computer.
  2. Make the script executable by running the following command in your Bash terminal:
    chmod +x compress-pdf.sh

πŸ’‘ Optional: Create an Alias

For easier access from any directory, you can create an alias in your shell's configuration file (.bashrc or .bash_profile).

  1. Open ~/.bashrc or ~/.bash_profile in a text editor.
  2. Add the following line, replacing ~/path/to/ with the actual path to the script(~ points to the folder ~/Users/<UserName>):
    alias cp_pdf='~/path/to/compress-pdf.sh'
    • The ~ character is a shortcut for your user's home directory (e.g., /c/Users/YourName).
    • For example, if you saved the script in C:\Users\YourName\Scripts, the path in Git Bash would be ~/Scripts/compress-pdf.sh.
    • If you are unsure where your .bashrc file is located, you can run find ~ -maxdepth 1 -name ".bashrc" from your terminal to find it.
  3. Save the file and restart your terminal or run source ~/.bashrc.

▢️ Usage

Run the script from your Bash terminal, providing the name of the PDF file without the .pdf extension.

Usage: ./compress-pdf.sh [options] <filename>

βš™οΈ Options

Flag Argument Description
-s <filesize> Target file size in KB (default: 2000).
-h Show the help message.

Important:

  • Options must come before the filename.
  • The script must be run from the same directory as the PDF file.

πŸ“ Examples

1. Compress with default settings:

Compresses My Document.pdf to a target size of 2000 KB.

./compress-pdf.sh "My Document"

2. Compress to a specific size:

Compresses Another Presentation.pdf to a target size of 800 KB.

./compress-pdf.sh -s 800 "Another Presentation"

back to top⬆️

About

A simple Bash script for Windows to compress PDF files.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages