turn image folder into a single pdf

 #!/usr/bin/env bash
# Usage: imagefolder_to_pdf.sh /path/to/folder
# Creates folder.pdf from all images (JPG/PNG/etc) in that folder at 150 dpi.
set -euo pipefail
if [ "${1:-}" = "-h" ] || [ "$#" -lt 1 ]; then
  echo "Usage: $(basename "$0") /path/to/folder" >&2
  exit 1
fi
indir="$1"
# Strip trailing slash, then append .pdf
foldername="$(basename "${indir%/}")"
outfile="$(dirname "$indir")/${foldername}.pdf"
if [ ! -d "$indir" ]; then
  echo "Error: folder not found: $indir" >&2
  exit 2
fi
# Gather images
mapfile -d '' files < <(
  find "$indir" -maxdepth 1 -type f \
    \( -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.png' -o -iname '*.tif' -o -iname '*.tiff' -o -iname '*.bmp' -o -iname '*.webp' \) \
    -print0 | sort -z -V
)
if [ "${#files[@]}" -eq 0 ]; then
  echo "Error: no images found in $indir" >&2
  exit 3
fi
tmpout="$(mktemp --suffix=.pdf)"
cleanup() { rm -f "$tmpout"; }
trap cleanup EXIT
if command -v img2pdf >/dev/null 2>&1; then
  img2pdf --auto-orient --dpi 150 -o "$tmpout" "${files[@]}"
elif command -v magick >/dev/null 2>&1; then
  magick -units PixelsPerInch -density 150 -auto-orient \
    "${files[@]}" -compress jpeg -quality 85 "$tmpout"
elif command -v convert >/dev/null 2>&1; then
  convert -units PixelsPerInch -density 150 -auto-orient \
    "${files[@]}" -compress jpeg -quality 85 "$tmpout"
else
  echo "Error: need either 'img2pdf' or ImageMagick ('magick'/'convert')." >&2
  exit 4
fi
mv -f "$tmpout" "$outfile"
trap - EXIT
echo "Created: $outfile"
x

Popular posts from this blog

Automatically Fix Song Metadata and Filenames on Linux with Beets

throttle traffic on apache

script to show all files with disk usage sizes