Skip to content

Instantly share code, notes, and snippets.

@mschwld
Created June 16, 2022 20:21
Show Gist options
  • Save mschwld/0cc004a61c98493e203ae2adc563f75b to your computer and use it in GitHub Desktop.
Save mschwld/0cc004a61c98493e203ae2adc563f75b to your computer and use it in GitHub Desktop.
Create an HTML table of pdf files from a dir alongside an image of each first page
#!/usr/bin/env bash
BASEPATH="$1"
BASEPATH_ABS=${BASEPATH:0:1}
# config
TITLE="My Library"
[ ! "$BASEPATH_ABS" = "/" ] && echo "Not an absolute path: $BASEPATH" && exit 1
[ ! -d "$BASEPATH" ] && echo "Not a folder: $BASEPATH" && exit 1
function DEPENDENCY_CHECKS {
for dep in $@; do
if ! hash "$dep" 2>/dev/null 1>&2; then echo "$dep not found in \$PATH. Abort." && exit 1; fi
done
}
DEPENDENCY_CHECKS convert sed find
# generate html
echo "<!doctype html><html lang=\"en\"> <head> <meta charset=\"utf-8\"> <title>$TITLE</title></head><body>"
echo "<style>a { text-decoration: none; color: black; } img { box-shadow: 10px 10px 5px #ccc; -moz-box-shadow: 10px 10px 5px #ccc; -webkit-box-shadow: 10px 10px 5px #ccc; -khtml-box-shadow: 10px 10px 5px #ccc;</style>"
# generate thumbnails
for x in {a..z}; do
FILES=$(find $BASEPATH -type f -iname "$x*.pdf")
FILES_COUNT=$(echo "$FILES" | wc -l)
# books section for letter
for i in $(seq 1 $FILES_COUNT); do
FILE=$(echo "$FILES" | sed "${i}q;d")
FILENAME=$(basename "$FILE")
FILEPATH=$(dirname "$FILE")
THUMBFILE=$(echo "$FILEPATH/.$FILENAME.jpg")
[ ! -f "$THUMBFILE" ] \
&& [ ! "$FILEPATH" = "." ] \
&& [ ! "$FILENAME" = "." ] \
&& echo "<!-- created thumbnail at $THUMBFILE -->" \
&& convert -density 144 -background white -alpha remove $FILE[0] -resize 70% $THUMBFILE \
&& convert $THUMBFILE -fuzz 1% -trim +repage $THUMBFILE.trim \
&& mv $THUMBFILE.trim $THUMBFILE
done
done
for x in {a..z}; do
# letter section
echo "<h1>Letter $x</h1>"
echo "<table style=\"table-layout: fixed; width: 100%; border-spacing: 0 3em;\"><tr>"
FILES=$(find $BASEPATH -type f -iname "$x*.pdf")
FILES_COUNT=$(echo "$FILES" | wc -l)
# books section for letter
for i in $(seq 1 $FILES_COUNT); do
MOD=$(expr $i % 6)
FILE=$(echo "$FILES" | sed "${i}q;d")
FILENAME=$(basename "$FILE")
FILEPATH=$(dirname "$FILE")
#FILENAME_PRINT=$(echo "$FILENAME" | cut -b 1-25)
FILENAME_PRINT=$(echo "$FILENAME")
THUMBFILE=$(echo "$FILEPATH/.$FILENAME.jpg")
[ "$FILENAME" = "." ] && continue
[ "$FILEPATH" = "." ] && continue
echo "<td style=\"text-align: center; word-break: break-all;\"><a href=\"$FILE\"><img src=\"$THUMBFILE\" width=\"150\" height=\"230\"></img><br/><br/>$FILENAME_PRINT</a></td>"
if [ "$MOD" = "0" ]; then
echo "</tr><tr>"
fi
done
echo "</tr></table>"
done
# html footer
echo "</body>"
echo "</html>"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment