pdfctl 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #!/bin/sh
  2. usage () {
  3. cat <<EOF>&2
  4. Usage: ${0##*/} COMMAND [-i] [-o OUTPUT] [OPTIONS] FILES...
  5. Wrapper around various PDF operations.
  6. OUTPUT is ignored when processing in-place (-i) or when more than one input
  7. files is given.
  8. Otherwise, or if OUTPUT is unspecified, a file with a random suffix is produced.
  9. List of commands and their options:
  10. compress [OPTIONS]
  11. Compress PDFs.
  12. -c: Compress raster graphics at the expense of a quality loss.
  13. -n: Do not compress raster graphics at all. Output may be bigger.
  14. extract [-f FIRST-PAGE] [-l LAST-PAGE]
  15. Extract a range of pages to a single PDF.
  16. resize
  17. Resize to A4.
  18. size
  19. Same as pdfinfo, but append size in milimeters.
  20. xerox
  21. Convert a PDF to a blurred, black & white raster image.
  22. For more details on compression:
  23. $ gs -sDEVICE=pdfwrite -o /dev/null \\
  24. -c "currentpagedevice { exch ==only ( ) print == } forall"
  25. http://partners.adobe.com/public/developer/en/acrobat/sdk/pdf/pdf_creation_apis_and_specs/DistillerParameters.pdf
  26. EOF
  27. }
  28. [ $# -eq 0 ] && usage && exit
  29. cmd="$1"
  30. shift 1
  31. NO_OUTPUT=false
  32. INPLACE=false
  33. COMPRESS_OPT="-dColorConversionStrategy=/LeaveColorUnchanged -dDownsampleMonoImages=false -dDownsampleGrayImages=false -dDownsampleColorImages=false -dAutoFilterColorImages=false -dAutoFilterGrayImages=false -dColorImageFilter=/FlateEncode -dGrayImageFilter=/FlateEncode"
  34. EXTRACT_FIRST=1
  35. EXTRACT_LAST=1
  36. case $cmd in
  37. compress)
  38. while getopts ":cino:" opt; do
  39. case $opt in
  40. c)
  41. COMPRESS_OPT="" ;;
  42. i)
  43. INPLACE=true ;;
  44. n)
  45. COMPRESS_OPT="-dColorConversionStrategy=/LeaveColorUnchanged -dEncodeColorImages=false -dEncodeGrayImages=false -dEncodeMonoImages=false" ;;
  46. o)
  47. output="$OPTARG" ;;
  48. \?)
  49. usage
  50. exit 1 ;;
  51. esac
  52. done
  53. ;;
  54. extract)
  55. while getopts ":f:il:o:" opt; do
  56. case $opt in
  57. f)
  58. EXTRACT_FIRST="$OPTARG" ;;
  59. i)
  60. INPLACE=true ;;
  61. l)
  62. EXTRACT_LAST="$OPTARG" ;;
  63. o)
  64. output="$OPTARG" ;;
  65. \?)
  66. usage
  67. exit 1 ;;
  68. esac
  69. done
  70. ;;
  71. resize|xerox)
  72. while getopts ":io:" opt; do
  73. case $opt in
  74. i)
  75. INPLACE=true ;;
  76. o)
  77. output="$OPTARG" ;;
  78. \?)
  79. usage
  80. exit 1 ;;
  81. esac
  82. done
  83. ;;
  84. size)
  85. NO_OUTPUT=true ;;
  86. *)
  87. usage
  88. exit 1 ;;
  89. esac
  90. shift $((OPTIND - 1))
  91. if [ $# -eq 0 ]; then
  92. usage
  93. exit 1
  94. fi
  95. [ $# -gt 1 ] && unset output
  96. mkoutput () {
  97. if [ -z "$output" ] || [ -e "$output" ]; then
  98. case "$input" in
  99. */*) ;;
  100. *) input="./$input" ;;
  101. esac
  102. dirname="${input%/*}"
  103. basename="${input##*/}"
  104. output=$(mktemp "$dirname/${basename%.*}-$cmd"-XXXXXX.pdf)
  105. fi
  106. echo "$output"
  107. }
  108. ## Some JPEG options
  109. # -sDEVICE=jpeg
  110. # -dUseCIEColor
  111. # -dJPEGQ=100
  112. # -r300
  113. # -dTextAlphaBits=4
  114. # -dGraphicsAlphaBits=4
  115. # -dMaxStripSize=8192
  116. compress () {
  117. gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite $COMPRESS_OPT -sOutputFile="$output" "$input"
  118. }
  119. extract () {
  120. gs -sDEVICE=pdfwrite -dNOPAUSE -dSAFER -dBATCH -dFirstPage="$EXTRACT_FIRST" -dLastPage="$EXTRACT_LAST" -sOutputFile="$output" "$input"
  121. }
  122. resize () {
  123. gs -q -o "$output" -sDEVICE=pdfwrite -sPAPERSIZE=a4 -dFIXEDMEDIA -dPDFFitPage -dCompatibilityLevel=1.4 "$input"
  124. }
  125. size () {
  126. pdfinfo "$input" | awk '/^Page size:/ {printf $0; print ", " $3*0.35278 " x " $5*0.35278 " mm"; next } 1'
  127. }
  128. xerox () {
  129. convert -threshold 90% -blur 1x1 -quality 100 -flatten -density 400 "$input" "$output"
  130. }
  131. for input; do
  132. echo "==> '$input'"
  133. $NO_OUTPUT || output=$(mkoutput)
  134. $cmd
  135. if $INPLACE; then
  136. rm -f "$input"
  137. mv "$output" "$input"
  138. fi
  139. echo
  140. done