Skip to content

Instantly share code, notes, and snippets.

@ram-ai-kumar
Last active February 24, 2024 12:25
Show Gist options
  • Select an option

  • Save ram-ai-kumar/2428c6148c7cfa9ca0b4843e4d67ed58 to your computer and use it in GitHub Desktop.

Select an option

Save ram-ai-kumar/2428c6148c7cfa9ca0b4843e4d67ed58 to your computer and use it in GitHub Desktop.

Revisions

  1. ram-ai-kumar revised this gist Feb 24, 2024. 2 changed files with 64 additions and 38 deletions.
    38 changes: 0 additions & 38 deletions pdf-compress
    Original file line number Diff line number Diff line change
    @@ -1,38 +0,0 @@
    # compress PDF using ghostscript
    #
    # https://ghostscript.readthedocs.io/en/latest/VectorDevices.html#controls-and-features-specific-to-postscript-and-pdf-input
    #
    # -dPDFSETTINGS=/screen lower quality, smaller size. (72 dpi)
    # -dPDFSETTINGS=/ebook for better quality, but slightly larger pdfs. (150 dpi)
    # -dPDFSETTINGS=/prepress output similar to Acrobat Distiller "Prepress Optimized" setting (300 dpi)
    # -dPDFSETTINGS=/printer selects output similar to the Acrobat Distiller "Print Optimized" setting (300 dpi)
    # -dPDFSETTINGS=/default selects output intended to be useful across a wide variety of uses,
    # possibly at the expense of a larger output file
    #
    # Usage: pdf-compress sm|md|lg /path/to/input.pdf /path/to/output.pdf
    #
    function pdf-compress {
    if [ $# -lt 3 ]; then
    echo "Usage: compress-pdf sm|md|lg input.pdf output.pdf"
    else
    # default = medium size compress
    size="ebook"
    # check which size we need
    case "$1" in
    "sm") size="screen" ;;
    "md") size="ebook" ;;
    "lg") size="prepress" ;;
    esac

    # execute only if gs command exists in shell
    if which gs &>/dev/null; then
    $(which gs) -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/"$size" \
    -dNOPAUSE -dQUIET -dBATCH -sOutputFile="$3" "$2"
    # open the folder
    open -R "$3"
    else
    echo "Note: Please install Ghostscript -- brew install ghostscript"
    fi
    fi
    }

    64 changes: 64 additions & 0 deletions pdf-compress.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,64 @@
    # utility functions
    #

    # compress PDF using ghostscript
    #
    # https://ghostscript.readthedocs.io/en/latest/VectorDevices.html#controls-and-features-specific-to-postscript-and-pdf-input
    #
    # -dPDFSETTINGS=/screen lower quality, smaller size. (72 dpi)
    # -dPDFSETTINGS=/ebook for better quality, but slightly larger pdfs. (150 dpi)
    # -dPDFSETTINGS=/prepress output similar to Acrobat Distiller "Prepress Optimized" setting (300 dpi)
    # -dPDFSETTINGS=/printer selects output similar to the Acrobat Distiller "Print Optimized" setting (300 dpi)
    # -dPDFSETTINGS=/default selects output intended to be useful across a wide variety of uses,
    # possibly at the expense of a larger output file
    #
    # Usage: pdf-compress sm|md|lg /path/to/input.pdf /path/to/output.pdf
    #
    function pdf-compress {
    # Check for required arguments
    if [ $# -lt 2 ]; then
    echo "Usage: pdf-compress sm|md|lg input.pdf [output.pdf | input-<size>.pdf]"
    return 1
    fi

    # Validate size argument
    if [[ ! "$1" =~ ^(sm|md|lg)$ ]]; then # Improved pattern for exact match
    echo "Please use sm, md, or lg. *$1* is not a valid size."
    echo "Usage: pdf-compress sm|md|lg input.pdf [output.pdf]"
    return 1
    fi

    # Determine size based on argument
    # default = medium size compress
    size="ebook"
    # check which size we need
    case "$1" in
    "sm") size="screen" ;;
    "md") size="ebook" ;;
    "lg") size="prepress" ;;
    esac

    # Check if gs command exists
    if ! command -v gs &>/dev/null; then
    echo "Error: Ghostscript (gs) is required. Please install it."
    return 1
    fi

    # Construct output filename
    if [ $# -eq 3 ]; then
    filename="$3"
    else
    # Efficiently create "input-size.pdf" format
    filename="${2%.*}-${size}.pdf"
    fi

    # Execute compression using gs
    gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS="/$size" \
    -dNOPAUSE -dQUIET -dBATCH -sOutputFile="$filename" "$2"

    # Optional: Open the compressed PDF (comment out if not needed)
    open -R "$filename"

    # Optional: show success message
    # echo "PDF compressed successfully: $filename"
    }
  2. ram-ai-kumar revised this gist Jan 30, 2024. No changes.
  3. ram-ai-kumar revised this gist Jan 30, 2024. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions pdf-compress
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@
    ```bash
    # compress PDF using ghostscript
    #
    # https://ghostscript.readthedocs.io/en/latest/VectorDevices.html#controls-and-features-specific-to-postscript-and-pdf-input
    @@ -36,4 +35,4 @@ function pdf-compress {
    fi
    fi
    }
    ```

  4. ram-ai-kumar created this gist Jan 30, 2024.
    39 changes: 39 additions & 0 deletions pdf-compress
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    ```bash
    # compress PDF using ghostscript
    #
    # https://ghostscript.readthedocs.io/en/latest/VectorDevices.html#controls-and-features-specific-to-postscript-and-pdf-input
    #
    # -dPDFSETTINGS=/screen lower quality, smaller size. (72 dpi)
    # -dPDFSETTINGS=/ebook for better quality, but slightly larger pdfs. (150 dpi)
    # -dPDFSETTINGS=/prepress output similar to Acrobat Distiller "Prepress Optimized" setting (300 dpi)
    # -dPDFSETTINGS=/printer selects output similar to the Acrobat Distiller "Print Optimized" setting (300 dpi)
    # -dPDFSETTINGS=/default selects output intended to be useful across a wide variety of uses,
    # possibly at the expense of a larger output file
    #
    # Usage: pdf-compress sm|md|lg /path/to/input.pdf /path/to/output.pdf
    #
    function pdf-compress {
    if [ $# -lt 3 ]; then
    echo "Usage: compress-pdf sm|md|lg input.pdf output.pdf"
    else
    # default = medium size compress
    size="ebook"
    # check which size we need
    case "$1" in
    "sm") size="screen" ;;
    "md") size="ebook" ;;
    "lg") size="prepress" ;;
    esac

    # execute only if gs command exists in shell
    if which gs &>/dev/null; then
    $(which gs) -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/"$size" \
    -dNOPAUSE -dQUIET -dBATCH -sOutputFile="$3" "$2"
    # open the folder
    open -R "$3"
    else
    echo "Note: Please install Ghostscript -- brew install ghostscript"
    fi
    fi
    }
    ```