On second though, shell script should not have .sh extension for simplicity
This commit is contained in:
parent
d6c84c7429
commit
249eb79fc9
2 changed files with 114 additions and 100 deletions
|
@ -7,9 +7,9 @@ You need to have ghoscript installed on your machine.
|
||||||
```
|
```
|
||||||
git clone https://gitea.auro.re/otthorn/pdf-optimize
|
git clone https://gitea.auro.re/otthorn/pdf-optimize
|
||||||
cd pdf-optimize
|
cd pdf-optimize
|
||||||
chmod +x pdf-optimize.sh
|
chmod +x pdf-optimize
|
||||||
sudo mkdir -p /opt/bin/
|
sudo mkdir -p /opt/bin/
|
||||||
sudo cp pdf-optimize.sh /opt/bin/pdf-optimize
|
sudo cp pdf-optimize /opt/bin/
|
||||||
```
|
```
|
||||||
|
|
||||||
Don't forget to add `/opt/bin` to your `PATH`.
|
Don't forget to add `/opt/bin` to your `PATH`.
|
||||||
|
|
210
pdf-optimize
210
pdf-optimize
|
@ -17,132 +17,146 @@
|
||||||
|
|
||||||
function usage
|
function usage
|
||||||
{
|
{
|
||||||
echo "usage: pdf-optimize [options] input.pdf"
|
echo "usage: pdf-optimize [options] input.pdf"
|
||||||
echo " ";
|
echo " ";
|
||||||
echo " -o | --output : output filename";
|
echo " -a | --archive : archive mode (PDF/A)";
|
||||||
echo " -q | --quiet : run quietly";
|
echo " -c | --compatibility : compability more with PDF 1.4";
|
||||||
echo " -v | --verbose : run verbosly";
|
echo " -h | --help : print this message";
|
||||||
echo " -y | --yes-to-replace : yes to replace existing files";
|
echo " -k | --quality : choose quality profile from:
|
||||||
echo " -h | --help : print this message";
|
* 1 HIGH QUALITY [default]
|
||||||
echo " ";
|
* 2 LOW QUALITY
|
||||||
echo "If no output path is specified, given input.pdf as input will output
|
* 3 AGGRESSIVE
|
||||||
|
* 4 VERY AGGRESIVE";
|
||||||
|
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 " ";
|
||||||
|
echo "If no output path is specified, given input.pdf as input will output
|
||||||
input_compressed.pdf"
|
input_compressed.pdf"
|
||||||
}
|
}
|
||||||
|
|
||||||
function arg_parse
|
function arg_parse
|
||||||
{
|
{
|
||||||
# if no argument is given print help
|
# if no argument is given print help
|
||||||
if [[ -z "$1" ]]; then
|
if [[ -z "$1" ]]; then
|
||||||
usage; exit;
|
usage; exit;
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# positional arguments
|
# positional arguments
|
||||||
args=()
|
args=()
|
||||||
|
|
||||||
while [ "$1" != "" ]; do
|
while [ "$1" != "" ]; do
|
||||||
case "$1" in
|
case "$1" in
|
||||||
-o | --output ) output_path="$2"; shift;;
|
-k | --quality ) quality="$2"; shift;;
|
||||||
-q | --quiet ) quiet=1;;
|
-o | --output ) output_path="$2"; shift;;
|
||||||
-v | --verbose ) verbose=1;;
|
-a | --archive ) archive=1;;
|
||||||
-y | --yes-to-replace ) replace=1;;
|
-c | --compatibility ) compatibility=1;;
|
||||||
-h | --help ) usage; exit;;
|
-q | --quiet ) quiet=1;;
|
||||||
* ) args+=("$1")
|
-v | --verbose ) verbose=1;;
|
||||||
esac
|
-y | --yes-to-replace ) replace=1;;
|
||||||
shift
|
-h | --help ) usage; exit;;
|
||||||
done
|
* ) args+=("$1")
|
||||||
|
esac
|
||||||
|
shift
|
||||||
|
done
|
||||||
|
|
||||||
# restore positional argument
|
# restore positional argument
|
||||||
set -- "${args[@]}"
|
set -- "${args[@]}"
|
||||||
|
|
||||||
# only the first one should be used
|
# only the first one should be used
|
||||||
input_path="${args[0]}"
|
input_path="${args[0]}"
|
||||||
|
|
||||||
# add default output path if none is given
|
# add default output path if none is given
|
||||||
if [[ -z "$output_path" ]]; then
|
if [[ -z "$output_path" ]]; then
|
||||||
output_path="${input_path%.*}_compressed.pdf";
|
output_path="${input_path%.*}_compressed.pdf";
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# check if the output file already exists before writting to it
|
# check if the output file already exists before writting to it
|
||||||
if [[ -f "$output_path" && "$replace" == 0 ]]; then
|
if [[ -f "$output_path" && "$replace" == 0 ]]; then
|
||||||
yes_or_no "$output_path already exists, do you want to replace it?" ||
|
yes_or_no "$output_path already exists, do you want to replace it?" ||
|
||||||
exit 1;
|
exit 1;
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# check if the input file exists
|
# check if the input file exists
|
||||||
if [[ ! -f "$input_path" ]]; then
|
if [[ ! -f "$input_path" ]]; then
|
||||||
echo "$input_path does not exist."
|
echo "$input_path does not exist."
|
||||||
echo "Aborting";
|
echo "Aborting";
|
||||||
exit 1;
|
exit 1;
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
function yes_or_no
|
function yes_or_no
|
||||||
{
|
{
|
||||||
read -p "$* [y/N]: " ans
|
read -p "$* [y/N]: " ans
|
||||||
case "$ans" in
|
case "$ans" in
|
||||||
[yY] | [yY]es ) return 0;;
|
[yY] | [yY]es ) return 0;;
|
||||||
* ) echo "Aborting"; return 1;;
|
* ) echo "Aborting"; return 1;;
|
||||||
esac
|
esac
|
||||||
}
|
}
|
||||||
|
|
||||||
function run
|
function run
|
||||||
{
|
{
|
||||||
arg_parse "$@";
|
arg_parse "$@";
|
||||||
|
|
||||||
# print general information
|
# print general information
|
||||||
if [[ "$quiet" != 1 ]]; then
|
if [[ "$quiet" != 1 ]]; then
|
||||||
echo "Compressing from ${bold}$input_path${normal} into \
|
echo "Compressing from ${bold}$input_path${normal} into \
|
||||||
${bold}$output_path${normal}."
|
${bold}$output_path${normal}."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# run the compression using ghostscript
|
# define the base command
|
||||||
if [[ "$verbose" != 1 ]]; then
|
base_command="
|
||||||
ghostscript -sDEVICE=pdfwrite \
|
gs -sDEVICE=pdfwrite \
|
||||||
-dCompatibilityLevel=1.4 \
|
-dPDFSETTINGS=/prepress \
|
||||||
-dPDFSETTINGS=/prepress \
|
-dNOPAUSE \
|
||||||
-dNOPAUSE \
|
-dBATCH ";
|
||||||
-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
|
# add compatibilty
|
||||||
if [[ "$quiet" != 1 ]]; then
|
if [[ "$compatibility" == 1 ]]; then
|
||||||
compute_compression;
|
base_command+="-dCompatibilityLevel=1.4 ";
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# ask ghostsript to be quiet
|
||||||
|
if [[ "$verbose" == 1 ]]; then
|
||||||
|
base_command+="-dQUIET ";
|
||||||
|
fi
|
||||||
|
|
||||||
|
# add archive mode option
|
||||||
|
if [[ "$archive" == 1 ]]; then
|
||||||
|
base_command+="-dPDFA=2 -dPDFACompatibilityPolicy=1 "
|
||||||
|
fi
|
||||||
|
|
||||||
|
# add input and ouput paths
|
||||||
|
base_command+="-sOutputFile=$output_path \
|
||||||
|
$input_path"
|
||||||
|
|
||||||
|
# run command
|
||||||
|
echo $base_command;
|
||||||
|
#eval $base_command;
|
||||||
|
|
||||||
|
# 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
|
function compute_compression
|
||||||
{
|
{
|
||||||
input_size=`du "$input_path" | cut -f1`
|
input_size=`du "$input_path" | cut -f1`
|
||||||
output_size=`du "$output_path" | cut -f1`
|
output_size=`du "$output_path" | cut -f1`
|
||||||
percentage=`echo "scale=1;$output_size / $input_size * 100" | bc -l`
|
percentage=`echo "scale=1;$output_size / $input_size * 100" | bc -l`
|
||||||
input_size_h=`du -h "$input_path" | cut -f1`
|
input_size_h=`du -h "$input_path" | cut -f1`
|
||||||
output_size_h=`du -h "$output_path" | cut -f1`
|
output_size_h=`du -h "$output_path" | cut -f1`
|
||||||
|
|
||||||
echo "Size reduced from ${bold}$input_size_h${normal} to \
|
echo "Size reduced from ${bold}$input_size_h${normal} to \
|
||||||
${bold}$output_size_h${normal} [$percentage %]"
|
${bold}$output_size_h${normal} [$percentage %]"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue