You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
3.0 KiB

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