henrydenhengst

shrinkpdf

Aug 24th, 2015
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.01 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # http://www.alfredklomp.com/programming/shrinkpdf
  4. # Licensed under the 3-clause BSD license:
  5. #
  6. # Copyright (c) 2014, Alfred Klomp
  7. # All rights reserved.
  8. #
  9. # USAGE: ./shrinkpdf.sh in.pdf > out.pdf
  10. #
  11. # Redistribution and use in source and binary forms, with or without
  12. # modification, are permitted provided that the following conditions are met:
  13. # 1. Redistributions of source code must retain the above copyright notice,
  14. #    this list of conditions and the following disclaimer.
  15. # 2. Redistributions in binary form must reproduce the above copyright notice,
  16. #    this list of conditions and the following disclaimer in the documentation
  17. #    and/or other materials provided with the distribution.
  18. # 3. Neither the name of the copyright holder nor the names of its contributors
  19. #    may be used to endorse or promote products derived from this software
  20. #    without specific prior written permission.
  21. #
  22. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  26. # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. # POSSIBILITY OF SUCH DAMAGE.
  33.  
  34.  
  35. shrink ()
  36. {
  37.     gs                  \
  38.       -q -dNOPAUSE -dBATCH -dSAFER      \
  39.       -sDEVICE=pdfwrite         \
  40.       -dCompatibilityLevel=1.3      \
  41.       -dPDFSETTINGS=/screen         \
  42.       -dEmbedAllFonts=true          \
  43.       -dSubsetFonts=true            \
  44.       -dColorImageDownsampleType=/Bicubic   \
  45.       -dColorImageResolution=72     \
  46.       -dGrayImageDownsampleType=/Bicubic    \
  47.       -dGrayImageResolution=72      \
  48.       -dMonoImageDownsampleType=/Bicubic    \
  49.       -dMonoImageResolution=72      \
  50.       -sOutputFile="$2"         \
  51.       "$1"
  52. }
  53.  
  54. check_smaller ()
  55. {
  56.     # If $1 and $2 are regular files, we can compare file sizes to
  57.     # see if we succeeded in shrinking. If not, we copy $1 over $2:
  58.     if [ ! -f "$1" -o ! -f "$2" ]; then
  59.         return 0;
  60.     fi
  61.     ISIZE="$(echo $(wc -c "$1") | cut -f1 -d\ )"
  62.     OSIZE="$(echo $(wc -c "$2") | cut -f1 -d\ )"
  63.     if [ "$ISIZE" -lt "$OSIZE" ]; then
  64.         echo "Input smaller than output, doing straight copy" >&2
  65.         cp "$1" "$2"
  66.     fi
  67. }
  68.  
  69. usage ()
  70. {
  71.     echo "Reduces PDF filesize by lossy recompressing with Ghostscript."
  72.     echo "Not guaranteed to succeed, but usually works."
  73.     echo "  Usage: $1 infile [outfile]"
  74. }
  75.  
  76. IFILE="$1"
  77.  
  78. # Need an input file:
  79. if [ -z "$IFILE" ]; then
  80.     usage "$0"
  81.     exit 1
  82. fi
  83.  
  84. # Output filename defaults to "-" (stdout) unless given:
  85. if [ ! -z "$2" ]; then
  86.     OFILE="$2"
  87. else
  88.     OFILE="-"
  89. fi
  90.  
  91. shrink "$IFILE" "$OFILE" || exit $?
  92.  
  93. check_smaller "$IFILE" "$OFILE"
Advertisement
Add Comment
Please, Sign In to add comment