Skip to content

Instantly share code, notes, and snippets.

@BebeSparkelSparkel
Last active December 4, 2024 20:08
Show Gist options
  • Save BebeSparkelSparkel/3e03d8473a40bd1b24835e005cc8d42d to your computer and use it in GitHub Desktop.
Save BebeSparkelSparkel/3e03d8473a40bd1b24835e005cc8d42d to your computer and use it in GitHub Desktop.
Consolidates PDFs, man pages, and web pages into a single PDF file.
#!/bin/sh
set -e
# Default values
output=""
pdfdocs=""
manpages=""
webpages=""
sources=""
# Function to display usage information
usage() {
echo "Consolidates PDFs, man pages, and web pages into a single PDF file."
echo "Usage: $0 [-o OUTPUT_FILE] [-m MANPAGE1,MANPAGE2,...] [-w WEBPAGE1,WEBPAGE2,...]"
echo " -o OUTPUT_FILE : Specify the output PDF file name"
echo " -p PDFPAGES : Comma-separated list of pdf pages to include"
echo " -m MANPAGES : Comma-separated list of man pages to convert to PDF"
echo " -w WEBPAGES : Comma-separated list of web pages to convert to PDF"
echo " -s SOURCES : Comma-separated list of source files to convert to put in PDF code blocks"
echo " -h : Display this help message"
}
# Parse command line options
while getopts ":o:p:m:w:s:h" opt; do
case $opt in
o)
output="$OPTARG"
;;
p)
pdfdocs="$OPTARG"
;;
m)
manpages="$OPTARG"
;;
w)
webpages="$OPTARG"
;;
s)
sources="$OPTARG"
;;
h)
usage
exit 0
;;
\?)
echo "Invalid option: -$OPTARG" >&2
usage >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
usage
exit 1
;;
esac
done
# Check if required commands are installed
required_programs='mandoc man pandoc pdfjam'
for cmd in $required_programs; do
if ! command -v "$cmd" > /dev/null; then
echo "Error: $cmd is not installed." >&2
echo "Required programs: $required_programs" >&2
exit 1
fi
done
# Check if output file is specified
if [ -z "$output" ]; then
output=/dev/stdout
fi
# Convert comma-separated strings to space-separated for iteration
manpages=$(echo "$manpages" | tr ',' ' ')
webpages=$(echo "$webpages" | tr ',' ' ')
sources=$(echo "$sources" | tr ',' ' ')
docs=$(echo "$pdfdocs" | tr ',' ' ')
# Process manpages
for m in $manpages; do
path=/tmp/$m.pdf
mandoc -T pdf "$(man -w "$m")" > "$path"
docs="$docs $path"
done
# Process webpages
for w in $webpages; do
path=/tmp/$(basename "$w" .html).pdf
pandoc -o "$path" "$w"
docs="$docs $path"
done
extension() {
path=$1
extension="${path##*.}"
if [ "$extension" != "$path" ]
then echo "$extension"
fi
}
# Process sources
code_block() {
s=$1
echo file: "$s"
printf '```%s\n' "$(extension "$s")"
cat "$s"
echo '```'
}
for s in $sources; do
path=/tmp/$(basename "$s" ".$(extension "$s")").pdf
echo path: "$path"
code_block "$s" | pandoc -f markdown -o "$path"
docs="$docs $path"
done
if [ -z "$docs" ]; then
echo "Error: no documents specified." >&2
usage >&2
exit 1
fi
# Combine all documents into one PDF
# shellcheck disable=SC2086
pdfjam -o "$output" -- $docs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment