#!/bin/sh
# gs_jpgs_to_pdf.sh
# 2026-04-24
# by Gernot Walzl
# Uses Ghostscript to write multiple JPEG images into a single PDF file.
# https://ghostscript.com/blog/optimizing-pdfs.html
set -e
DPI=${DPI:-300}
DIR=$1
if [ ! -d "$DIR" ]; then
echo "ERROR: $DIR not found."
exit 1
fi
VIEWJPEGS=''
for JPG in "$DIR"/*.jpg; do
VIEWJPEGS="$VIEWJPEGS($JPG) viewJPEG showpage
"
done
gs -dNOSAFER -sDEVICE=pdfwrite \
-sPAPERSIZE=a4 -dFIXEDMEDIA \
-dFastWebView \
-dDownsampleColorImages=true -dColorImageDownsampleType=/Bicubic -dColorImageResolution="$DPI" \
-dDownsampleGrayImages=true -dGrayImageDownsampleType=/Bicubic -dGrayImageResolution="$DPI" \
-dDownsampleMonoImages=true -dMonoImageResolution="$DPI" \
-o "$(basename "$DIR").pdf" \
viewjpeg.ps -c "$VIEWJPEGS"