Bash Script to scale and/or resize PDFs from the command line.
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 

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