From 70b458ad01d96de17e1639ba2d2862a77e22fc8d Mon Sep 17 00:00:00 2001 From: Otthorn Date: Mon, 15 Feb 2021 00:21:22 +0100 Subject: [PATCH] :tada: initial commit --- README.md | 28 ++++++++++ pdf-optimize | 144 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 172 insertions(+) create mode 100644 README.md create mode 100755 pdf-optimize diff --git a/README.md b/README.md new file mode 100644 index 0000000..70b984f --- /dev/null +++ b/README.md @@ -0,0 +1,28 @@ +# pdf-optimize + +pdf-optimize is a simple script to optimize pdf using ghostscript. +You need to have ghoscript installed on your machine. + +## Installation +``` +git clone https://gitea.auro.re/otthorn/pdf-optimize +cd pdf-optimize +chmod +x pdf-optimize.sh +cp pdf-optimize.sh /opt/bin/pdf-optimize +``` + +Don't forget to add `/opt/bin` to your `PATH`. +If you can add this line at the end of your `bashrc` (or whatever your use) + +``` +PATH=$PATH:/opt/bin" +``` + +## Usage + +You can get help using +``` +pdf-optimize -h +``` + + diff --git a/pdf-optimize b/pdf-optimize new file mode 100755 index 0000000..d2a8678 --- /dev/null +++ b/pdf-optimize @@ -0,0 +1,144 @@ +#!/bin/bash +# Compress pdf using Ghostscript + +function usage +{ + echo "usage: pdf-optimize [options] input.pdf" + echo " "; + echo " -o | --output : output filename"; + echo " -q | --quiet : run quietly"; + echo " -v | --verbose : run verbosly"; + echo " -y | --yes-to-replace : yes to replace existing files"; + echo " -h | --help : print this message"; + echo " "; + echo "If no output path is specified, given input.pdf as input will output +input_compressed.pdf" +} + +function arg_parse +{ + # if no argument is given print help + if [[ -z "$1" ]]; then + usage; exit; + fi + + # positional arguments + args=() + + while [ "$1" != "" ]; do + case "$1" in + -o | --output ) output_path="$2"; shift;; + -q | --quiet ) quiet=1;; + -v | --verbose ) verbose=1;; + -y | --yes-to-replace ) replace=1;; + -h | --help ) usage; exit;; + * ) args+=("$1") + esac + shift + done + + # restore positional argument + set -- "${args[@]}" + + # only the first one should be used + input_path="${args[0]}" + + # add default output path if none is given + if [[ -z "$output_path" ]]; then + output_path="${input_path%.*}_compressed.pdf"; + fi + + # check if the output file already exists before writting to it + if [[ -f "$output_path" && "$replace" == 0 ]]; then + yes_or_no "$output_path already exists, do you want to replace it?" || + exit 1; + fi + + # check if the input file exists + if [[ ! -f "$input_path" ]]; then + echo "$input_path does not exist." + echo "Aborting"; + exit 1; + fi +} + +function yes_or_no +{ + read -p "$* [y/N]: " ans + case "$ans" in + [yY] | [yY]es ) return 0;; + * ) echo "Aborting"; return 1;; + esac +} + +function run +{ + arg_parse "$@"; + + # print general information + if [[ "$quiet" != 1 ]]; then + echo "Compressing from ${bold}$input_path${normal} into \ +${bold}$output_path${normal}." + fi + + # run the compression using ghostscript + if [[ "$verbose" != 1 ]]; then + ghostscript -sDEVICE=pdfwrite \ + -dCompatibilityLevel=1.4 \ + -dPDFSETTINGS=/prepress \ + -dNOPAUSE \ + -dQUIET \ + -dBATCH \ + -sOutputFile=$output_path \ + $input_path + else + echo "input_path=$input_path"; + echo "output_path=$output_path"; + echo "quiet=$quiet"; + echo "verbose=$verbose"; + echo "replace=$replace"; + echo " "; + ghostscript -sDEVICE=pdfwrite \ + -dCompatibilityLevel=1.4 \ + -dPDFSETTINGS=/prepress \ + -dNOPAUSE \ + -dBATCH \ + -sOutputFile=$output_path \ + $input_path + fi + + # catching errors + if [[ "$?" != 0 ]]; then + echo "Something went wrong with ghostscript."; + echo "Aborting."; + exit 1; + fi + + # compute the compresion rate + if [[ "$quiet" != 1 ]]; then + compute_compression; + fi +} + +function compute_compression +{ + input_size=`du "$input_path" | cut -f1` + output_size=`du "$output_path" | cut -f1` + percentage=`echo "scale=1;$output_size / $input_size * 100" | bc -l` + input_size_h=`du -h "$input_path" | cut -f1` + output_size_h=`du -h "$output_path" | cut -f1` + + echo "Size reduced from ${bold}$input_size_h${normal} to ${bold}$output_size_h${normal} [$percentage %]" +} + +# font settings +bold=$(tput bold) +normal=$(tput sgr0) + +# default values +quiet=0; # do not be quiet +verbose=0; # do not be verbose +replace=0; # do not replace existing files + +# execute main function +run "$@";