"Fossies" - the Fresh Open Source Software Archive

Member "littleutils-1.2.4/extra/opt-pdf.in" (28 Mar 2021, 5869 Bytes) of package /linux/privat/littleutils-1.2.4.tar.lz:


As a special service "Fossies" has tried to format the requested text file into HTML format (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "opt-pdf.in": 1.2.3_vs_1.2.4.

    1 #! PROGBASH
    2 # vim: set filetype=bash:
    3 
    4 # opt-pdf: Recompresses .pdf files using Ghostscript or Poppler
    5 
    6 # Copyright (C) 2013-2021 by Brian Lindholm.  This file is part of the
    7 # littleutils utility set.
    8 #
    9 # The opt-pdf utility is free software; you can redistribute it and/or modify
   10 # it under the terms of the GNU General Public License as published by the Free
   11 # Software Foundation; either version 3, or (at your option) any later version.
   12 #
   13 # The opt-pdf utility is distributed in the hope that it will be useful, but
   14 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   15 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
   16 # more details.
   17 #
   18 # You should have received a copy of the GNU General Public License along with
   19 # the littleutils.  If not, see <https://www.gnu.org/licenses/>.
   20 
   21 # get command-line options
   22 BACKUP='n'
   23 COLORDPI=''
   24 FORCE='n'
   25 GRAYDPI=''
   26 MONODPI=''
   27 QUIETFLAG='-q'
   28 TOUCH='n'
   29 VERBOSE='y'
   30 WEBOPT='n'
   31 while getopts bc:fg:hm:oqtv opts
   32 do
   33   case $opts in
   34     b) BACKUP='y' ;;
   35     c) COLORDPI="-dAutoFilterColorImages=false -dColorImageResolution=$OPTARG -dColorImageDownsampleType=/Bicubic" ;;
   36     f) FORCE='y' ;;
   37     g) GRAYDPI="-dAutoFilterGrayImages=false -dGrayImageResolution=$OPTARG -dGrayImageDownsampleType=/Bicubic" ;;
   38     h) echo 'opt-pdf LU_VERSION'
   39        echo 'usage: opt-pdf [-b(ackup)] [-c(olor) DPI] [-f(orce_overwrite)]'
   40        echo '         [-g(rayscale) DPI] [-h(elp)] [-m(onochrome) DPI] [-t(ouch)]'
   41        echo '         [-o(ptimize_for_web)] [-q(uiet)] [-v(erbose)] PDF_filename ...'
   42        exit 0 ;;
   43     m) MONODPI="-dAutoFilterMonoImages=false -dMonoImageResolution=$OPTARG -dMonoImageDownsampleType=/Bicubic" ;;
   44     o) WEBOPT='y' ;;
   45     q) VERBOSE='n' ;;
   46     t) TOUCH='y' ;;
   47     v) QUIETFLAG='' ;;
   48     *) echo 'opt-pdf LU_VERSION'
   49        echo 'usage: opt-pdf [-b(ackup)] [-c(olor) DPI] [-f(orce_overwrite)]'
   50        echo '         [-g(rayscale) DPI] [-h(elp)] [-m(onochrome) DPI] [-t(ouch)]'
   51        echo '         [-o(ptimize_for_web)] [-q(uiet)] [-v(erbose)] PDF_filename ...'
   52        exit 1 ;;
   53   esac
   54 done
   55 shift $((${OPTIND}-1))
   56 
   57 # set up traps
   58 trap 'rm -f $TMPPDF0 $TMPPDF1 $TMPPDF2 ; exit 1' 1 2 3 13 15
   59 
   60 # handle special PDF options
   61 declare -i GSVER=$(gs --version | sed -e 's/\.//' -e 's/\..*$//')
   62 if [ "$WEBOPT" = 'y' ]; then
   63   if [ $GSVER -ge 950 ]; then
   64     PDFOPT='-dFastWebView=true -c 33550336 setvmthreshold'
   65   elif [ $GSVER -ge 907 ]; then
   66     PDFOPT='-dFastWebView=true -c .setpdfwrite'
   67   else
   68     PDFOPT='-c .setpdfwrite -f pdfopt.ps'
   69   fi
   70 else
   71   if [ $GSVER -ge 950 ]; then
   72     PDFOPT='-c 33550336 setvmthreshold'
   73   else
   74     PDFOPT='-c .setpdfwrite'
   75   fi
   76 fi
   77 
   78 # run through files
   79 declare -i S0=0 S1=0 S2=0
   80 while [ $# -gt 0 ]; do
   81 
   82   # make sure we can read and modify file
   83   if [ ! -f "$1" -o ! -r "$1" -o ! -w "$1" ]; then
   84     echo "opt-pdf warning: $1 is not a writeable non-directory file"
   85     shift; continue
   86   fi
   87 
   88   # make sure it's a PDF
   89   file "$1" | grep -q 'PDF document'
   90   if [ $? -ne 0 ]; then
   91     echo "opt-pdf warning: $1 is not a PDF"
   92     shift; continue
   93   fi
   94 
   95   # skip already-processed files
   96   if [ "$FORCE" = 'n' ]; then
   97     command -v pdfinfo > /dev/null
   98     if [ $? -eq 0 ]; then
   99       TMPPDF0=$(tempname opt-pdf_$$) || exit 99
  100       pdfinfo "$1" > $TMPPDF0
  101       grep Producer $TMPPDF0 | grep -q Ghostscript
  102       if [ $? -eq 0 ]; then
  103         echo "opt-pdf message: skipping ghostscript-processed $1"
  104         rm -f $TMPPDF0
  105         shift; continue
  106       fi
  107       grep Producer $TMPPDF0 | grep -q cairo
  108       if [ "$?" = '0' ]; then
  109         echo "opt-pdf message: skipping poppler-processed $1"
  110         rm -f $TMPPDF0
  111         shift; continue
  112       fi
  113     fi
  114   fi
  115 
  116   # run through ghostscript
  117   TMPPDF1=$(tempname -s .pdf opt-pdf_$$) || exit 99
  118   gs ${QUIETFLAG} -dSAFER -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 \
  119     -dDetectDuplicateImages=true -sBandListStorage=memory -dSubsetFonts=true \
  120     -dCompressFonts=true $COLORDPI $GRAYDPI $MONODPI -sOutputFile=$TMPPDF1 $PDFOPT -f "$1"
  121   if [ $? -eq 0 ]; then
  122     chmod --reference="$1" $TMPPDF1
  123     if [ "$TOUCH" = 'y' ]; then
  124       touch -r "$1" $TMPPDF1
  125     fi
  126     S1=$(filesize $TMPPDF1)
  127     GHOSTGOOD='y'
  128   else
  129     echo "opt-pdf warning: ghostscript failed to process $1: gs rc = $?"
  130     rm -f $TMPPDF1
  131     S1=$(((1<<63)-1))  # largest integer
  132     GHOSTGOOD='n'
  133   fi
  134 
  135   # run through poppler
  136   TMPPDF2=$(tempname -s .pdf opt-pdf_$$) || exit 99
  137   pdftocairo ${QUIETFLAG} -pdf "$1" $TMPPDF2
  138   if [ $? -eq 0 ]; then
  139     chmod --reference="$1" $TMPPDF2
  140     if [ "$TOUCH" = 'y' ]; then
  141       touch -r "$1" $TMPPDF2
  142     fi
  143     S2=$(filesize $TMPPDF2)
  144     POPPLERGOOD='y'
  145   else
  146     echo "opt-pdf warning: poppler failed to process $1: pdftocairo rc = $?"
  147     rm -f $TMPPDF2
  148     POPPLERGOOD='n'
  149     S2=$(((1<<63)-1))  # largest integer
  150   fi
  151 
  152   # see if we have any results to use
  153   if [ "$GHOSTGOOD" = 'n' -a "$POPPLERGOOD" = 'n' ]; then
  154     echo "opt-pdf error: both ghostscript and poppler failed; skipping..."
  155     shift; continue
  156   fi
  157 
  158   # ensure that new file is smaller
  159   S0=$(filesize "$1")
  160   if [ "$GHOSTGOOD" = 'n' -o $S2 -lt $S1 ]; then
  161     if [ $S2 -lt $S0 -o "$FORCE" = 'y' ]; then
  162       if [ "$BACKUP" = 'y' ]; then
  163         mv "$1" "${1}.bak"
  164       fi
  165       cp --preserve=timestamps $TMPPDF2 "$1"
  166       if [ "$VERBOSE" = 'y' ]; then
  167         echo "$1: using poppler - $S0 vs. $S2"
  168       fi
  169     elif [ "$VERBOSE" = 'y' ]; then
  170       echo "$1: unchanged"
  171     fi
  172   else
  173     if [ $S1 -lt $S0 -o "$FORCE" = 'y' ]; then
  174       if [ "$BACKUP" = 'y' ]; then
  175         mv "$1" "${1}.bak"
  176       fi
  177       cp --preserve=timestamps $TMPPDF1 "$1"
  178       if [ "$VERBOSE" = 'y' ]; then
  179         echo "$1: using ghostscript - $S0 vs. $S1"
  180       fi
  181     elif [ "$VERBOSE" = 'y' ]; then
  182       echo "$1: unchanged"
  183     fi
  184   fi
  185 
  186   # clean up afterwards
  187   rm -f $TMPPDF1 $TMPPDF2
  188   shift
  189 
  190 done