Bash Script to scale and/or resize PDFs from the command line.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 

44 lignes
2.0 KiB

  1. #!/bin/bash
  2. # pdfScale.sh
  3. #
  4. # Scale PDF to specified percentage of original size.
  5. # Ref: http://ma.juii.net/blog/scale-page-content-of-pdf-files.
  6. echo "This script doesn't handle files with spaces in them."
  7. SCALE=0.95 # scaling factor (0.95 = 95%, e.g.)
  8. # Validate args.
  9. [ $# -eq 1 ] || { echo "***ERROR: Usage pdfScale.sh <inFile>.pdf"; exit 99; }
  10. INFILEPDF="$1"
  11. [[ "$INFILEPDF" =~ ^..*\.pdf$ ]] || { echo "***ERROR: Usage pdfScale.sh <inFile>.pdf"; exit 99; }
  12. OUTFILEPDF=$(echo "$INFILEPDF" | sed -e s/\.pdf$// -).SCALED.pdf
  13. # Dependencies
  14. command -v identify >/dev/null 2>&1 || { echo >&2 "Please install 'imagemagick' (sudo apt-get install imagemagick). Aborting."; exit 1; }
  15. command -v gs >/dev/null 2>&1 || { echo >&2 "Please install 'ghostscript' (sudo apt-get install ghostscript ?). Aborting."; exit 1; }
  16. command -v bc >/dev/null 2>&1 || { echo >&2 "Please install 'bc' arbitrary precision calculator language. Aborting."; exit 1; }
  17. # Get width/height in postscript points (1/72-inch), via ImageMagick identify command.
  18. # (Alternatively, could use Poppler pdfinfo command; or grep/sed the PDF by hand.)
  19. IDENTIFY=($(identify $INFILEPDF 2>/dev/null)) # bash array
  20. [ $? -ne 0 ] &GEOMETRY=($(echo ${IDENTIFY[2]} | tr "x" " ")) # bash array — $IDENTIFY[2] is of the form PGWIDTHxPGHEIGHT
  21. PGWIDTH=${GEOMETRY[0]}; PGHEIGHT=${GEOMETRY[1]}
  22. # Compute translation factors (to center page.
  23. XTRANS=$(echo "scale=6; 0.5*(1.0-$SCALE)/$SCALE*$PGWIDTH" | bc)
  24. YTRANS=$(echo "scale=6; 0.5*(1.0-$SCALE)/$SCALE*$PGHEIGHT" | bc)
  25. echo $PGWIDTH , $PGHEIGHT , $OUTFILEPDF , $SCALE , $XTRANS , $YTRANS , $INFILEPDF , $OUTFILEPDF
  26. # Do it.
  27. gs \
  28. -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dSAFER \
  29. -dCompatibilityLevel="1.5" -dPDFSETTINGS="/printer" \
  30. -dColorConversionStrategy=/LeaveColorUnchanged \
  31. -dSubsetFonts=true -dEmbedAllFonts=true \
  32. -dDEVICEWIDTH=$PGWIDTH -dDEVICEHEIGHT=$PGHEIGHT \
  33. -sOutputFile="$OUTFILEPDF" \
  34. -c "<</BeginPage{$SCALE $SCALE scale $XTRANS $YTRANS translate}>> setpagedevice" \
  35. -f "$INFILEPDF"