summaryrefslogtreecommitdiff
path: root/localpdf2ps2pdf
blob: 686d000f5b441cc338cd9e8a14d2af03deb3b34a (plain)
  1. #!/bin/sh
  2. #
  3. # /usr/local/bin/localpdf2ps2pdf
  4. # Copyright 2008-2023 Jonas Smedegaard <dr@jones.dk>
  5. #
  6. # Refry PDF file.
  7. #
  8. set -e
  9. PRG="$(basename "$0")"
  10. # Default options
  11. PDFTOPSOPTS="-preload -paper match"
  12. PS2PDFOPTS="-dDownsampleColorImages=true -dColorImageResolution=150"
  13. showhelp() {
  14. cat <<EOF
  15. Usage: $PRG INFILE [ OUTFILE ] [ pdftops opts ] [ -- ps2pdf opts ]
  16. Defaults:
  17. OUTFILE: INFILE with trailing ".pdf" replaced with "_lowres.pdf".
  18. pdftops: $PDFTOPSOPTS
  19. ps2pdf: $PS2PDFOPTS
  20. Examples:
  21. $PRG file.pdf -f 7 -l 9
  22. $PRG file.pdf -- -dPDFA -dPDFACompatibilityPolicy=1
  23. $PRG file.pdf -- -dPDFSETTINGS=/screen
  24. $PRG file.pdf -level3 -- -dPDFSETTINGS=/prepress
  25. First example above picks pages 7-9.
  26. Second example above reduces PDF complexity.
  27. Third example reduces image size for screen-only use.
  28. Fourth example preserves complexity and image size for professional use.
  29. Defaults reduce file size for 300 DPI color print use.
  30. EOF
  31. }
  32. exit1() {
  33. echo >&2 "ERROR: $1"
  34. exit 1
  35. }
  36. [ $# -gt 0 ] || exit1 "Input file missing"
  37. infile=
  38. outfile=
  39. pdftopsopts=
  40. pstopdfopts=
  41. while [ $# -gt 0 ]; do
  42. case $1 in
  43. -h|--help)
  44. showhelp
  45. exit 0
  46. ;;
  47. --)
  48. shift
  49. break
  50. ;;
  51. -*)
  52. pdftopsopts="$pdftopsopts $1"
  53. shift
  54. ;;
  55. *)
  56. if [ -z "$infile" ]; then
  57. infile="$1"
  58. elif [ -z "$outfile" ]; then
  59. outfile="$1"
  60. else
  61. exit1 "Too many parameters"
  62. fi
  63. shift
  64. ;;
  65. esac
  66. done
  67. ps2pdfopts="$@"
  68. # Use defaults if not overridden
  69. outfile="${outfile:-$(basename "$infile" .pdf)_lowres.pdf}"
  70. pdftopsopts="${pdftopsopts:-$PDFTOPSOPTS}"
  71. ps2pdfopts="${ps2pdfopts:-$PS2PDFOPTS}"
  72. [ ! -e "$outfile" ] || exit1 "Output file already exists"
  73. tmpfile=$(mktemp -t "$PRG.XXXXXXXXXX") || exit 1
  74. pdftops $pdftopsopts "$infile" "$tmpfile" || rm "$tmpfile"
  75. ps2pdf $ps2pdfopts "$tmpfile" "$outfile"
  76. rm "$tmpfile"
  77. exit 0