You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
1.7 KiB
Bash

#!/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=false \
-dDownsampleColorImages=true \
-dColorImageResolution=$RESOLUTION \
-dGrayImageResolution=$RESOLUTION \
-dMonoImageResolution=$RESOLUTION \
-dPreserveOPIRequests=false \
-dSubsetFonts=true \
-dEmbedAllFonts=true \
-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'"