Guest User

Merge pupils' solutions to PDF script

a guest
Feb 28th, 2021
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.19 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Open all image files in feh before working on them, to rotate them to proper orientation by hand
  4. feh --recursive --edit --scale-down --auto-zoom --geometry 1280x740 "$1"
  5.  
  6. cd "$1"
  7.  
  8. # Iterate over folders, each of which contains files from one pupil
  9. for FOLDER in "$1"/*/; do
  10.    
  11.     # Get the pupil's name
  12.     echo "$FOLDER" >> "$1"/log.log
  13.     cd "$FOLDER"
  14.     NAME=$(basename "$FOLDER" | cut -f1 -d "_")
  15.     echo $NAME >> "$1"/log.log
  16.    
  17.     # If the pupil submitted PDFs, join them; if the result is larger than 5MB, compress it with ps2pdf
  18.     if [[ $(ls) == *"pdf"* ]]; then
  19.  
  20.         pdfunite *.pdf "$NAME".pdf
  21.  
  22.         if [ $(stat --printf="%s" "$NAME".pdf) -gt "5000000" ]; then
  23.             echo "pdf too large, compressing" >> "$1"/log.log
  24.             ps2pdf "$NAME".pdf _compressed_"$NAME".pdf
  25.             mv _compressed_"$NAME".pdf "$NAME".pdf
  26.         fi
  27.    
  28.         mv "$NAME".pdf ../"$NAME".pdf
  29.     else
  30.    
  31.     # If the pupil submitted images, iterate over them, remove the orientation EXIF data to preserve the manually set orientation, then compress the individual image files, if they are larger than 1,5MB; then merge the image files to a PDF
  32.         for filename in ./*; do
  33.             exiv2 rm "filename"
  34.            
  35.                 if [ $(stat --printf="%s" "$filename") -gt "1500000" ]; then
  36.                     echo "image $filename too large, compressing" >> "$1"/log.log
  37.                     name=$(basename "$filename" | awk -F'.' 'sub(FS $NF,x)')
  38.                     echo "$name" >> "$1"/log.log
  39.                     magick "$filename" -interlace Plane -gaussian-blur 0.05 -quality 85% _compressed_"$name".jpg
  40.                     rm "$filename"
  41.                 fi
  42.         done
  43.  
  44.         convert * ../"$NAME".pdf
  45.  
  46.     fi
  47.    
  48.     # Set the size to A4 such that the pen thickness in Xournal++ is suiting
  49.     gs -sDEVICE=pdfwrite -sPAPERSIZE=a4 -dPDFFitPage -dPDFSETTINGS=/default -dNOPAUSE -dQUIET -dBATCH -sOutputFile=../out.pdf  ../"$NAME".pdf
  50.    
  51.     # Set the DPI to a low value such that the pixel dimensions are not too large and Xournal++ won't hang
  52.     convert -units PixelsPerInch -density 120 ../out.pdf ../"$NAME".pdf
  53.     rm ../out.pdf
  54. done
Advertisement
Add Comment
Please, Sign In to add comment