diff --git a/pdfreduce.sh b/pdfreduce.sh new file mode 100755 index 0000000..c93954f --- /dev/null +++ b/pdfreduce.sh @@ -0,0 +1,84 @@ +#!/bin/bash + +# Function to display usage information and exit +usage() { + echo "Usage: $0 input.pdf output.pdf [-r resolution] [-h]" + echo "" + echo "Options:" + echo "-r Set the image resolution (default is 150ppi)" + echo "-h Display this help message" + exit 1 +} + +# Default settings +RESOLUTION=150 + +# Parse command-line options +while getopts ":r:h" opt; do + case ${opt} in + r) + RESOLUTION=$OPTARG + ;; + h) + usage + ;; + \?) + echo "Invalid option: -$OPTARG" >&2 + exit 1 + ;; + :) + echo "Option -$OPTARG requires an argument." >&2 + exit 1 + ;; + esac +done + +shift $((OPTIND - 1)) + +# Check if the correct number of arguments are provided after parsing options +if [ "$#" -ne 2 ]; then + usage +fi + +INPUT_FILE="$1" +OUTPUT_FILE="$2" + +# Check if input file exists +if [ ! -f "$INPUT_FILE" ]; then + echo "Input file '$INPUT_FILE' does not exist." + exit 1 +fi + +# Extract directory and base name from output file path +OUTPUT_DIR=$(dirname "$OUTPUT_FILE") +OUTPUT_BASENAME=$(basename "$OUTPUT_FILE") + +# Create the output directory if it doesn't already exist +mkdir -p "$OUTPUT_DIR" + +# Run Ghostscript command with specified resolution +gs \ + -sDEVICE=pdfwrite \ + -dCompatibilityLevel=1.7 \ + -dPDFSETTINGS=/prepress \ + -dDownsampleMonoImages=true \ + -dDownsampleColorImages=true \ + -dDownsampleGrayImages=true \ + -dColorImageResolution=$RESOLUTION \ + -dGrayImageResolution=$RESOLUTION \ + -dMonoImageResolution=$RESOLUTION \ + -dPreserveOPIRequests=false \ + -dPDFSETTINGS=/prepress \ + -dSubsetFonts=true \ + -dEmbedAllFonts=false \ + -sProcessColorModel=DeviceRGB \ + -dQUIET -dBATCH -dNOPAUSE \ + -sOutputFile="$OUTPUT_FILE" "$INPUT_FILE" + +# Check if Ghostscript command was successful +if [ $? -ne 0 ]; then + echo "Error: Ghostscript failed to process the file '$INPUT_FILE'." + exit 1 +fi + +echo "Successfully converted '$INPUT_FILE' to '$OUTPUT_BASENAME'"