PDF combine and compress scripts

You should run `brew install ghostscript` before you can use these.
mail@pastecode.io avatar
unknown
plain_text
a month ago
1.7 kB
2
Indexable
Never
pdf_compress () {
    # Check if there is at least one argument
    if [ "$#" -ne 1 ]; then
        echo "Usage: pdf_compress <input.pdf>"
        return 1
    fi

    # Get the base name of the input file
    input_pdf="$1"
    compressed_output_pdf="${input_pdf%.*}.compressed.pdf"

    # Run the Ghostscript command for compression
    gs -q -dNOPAUSE -dBATCH -dSAFER \
       -sDEVICE=pdfwrite \
       -dCompatibilityLevel=1.3 \
       -dPDFSETTINGS=/screen \
       -dEmbedAllFonts=true \
       -dSubsetFonts=true \
       -dColorImageDownsampleType=/Bicubic \
       -dColorImageResolution=144 \
       -dGrayImageDownsampleType=/Bicubic \
       -dGrayImageResolution=144 \
       -dMonoImageDownsampleType=/Bicubic \
       -dMonoImageResolution=144 \
       -sOutputFile="$compressed_output_pdf" "$input_pdf"

    # Check if the command succeeded
    if [ $? -eq 0 ]; then
        echo "Successfully compressed PDF into $compressed_output_pdf"
    else
        echo "Error occurred during PDF compression"
        return 1
    fi
}

pdf_merge () {
    # Check if there are enough arguments
    if [ "$#" -lt 2 ]; then
        echo "Usage: merge_pdfs <input1.pdf> <input2.pdf> ... <inputN.pdf>"
        return 1
    fi

    # Get the base name of the first input file
    base_name="${1%.*}"
    merged_output_pdf="${base_name}.merged.pdf"

    # Run the Ghostscript command
    gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile="$merged_output_pdf" "$@"

    # Check if the command succeeded
    if [ $? -eq 0 ]; then
        echo "Successfully merged PDFs into $merged_output_pdf"
    else
        echo "Error occurred during PDF merge"
        return 1
    fi
}
Leave a Comment