Skip to content

Instantly share code, notes, and snippets.

@feklee
Last active October 10, 2020 07:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save feklee/1e395deafb33404372926e8db424fe91 to your computer and use it in GitHub Desktop.
Save feklee/1e395deafb33404372926e8db424fe91 to your computer and use it in GitHub Desktop.
Convert image files to PDF
#!/bin/bash
# Some of the `getopts` code has been taken from:
# http://mywiki.wooledge.org/BashFAQ/035#getopts
# Felix E. Klee <felix.klee@inka.de>
W=210
H=297
D=300
# Usage info
show_help() {
cat << EOF
Usage: ${0##*/} [-hWHD] [IMAGE FILE]...
Convert image files to: out.pdf
-h display this help and exit
-W WIDTH width in mm, default $W
-H HEIGHT height in mm, default $H
-D DPI resolution, default $D
EOF
}
# Initialize our own variables:
output_file=""
OPTIND=1
# Resetting OPTIND is necessary if getopts was used previously in the script.
# It is a good idea to make OPTIND local if you process options in a function.
while getopts hW:H:D: opt; do
case $opt in
h)
show_help
exit 0
;;
W) W=$OPTARG
;;
H) H=$OPTARG
;;
D) D=$OPTARG
;;
*)
show_help >&2
exit 1
;;
esac
done
shift "$((OPTIND-1))" # Discard the options and sentinel --
if [ "$#" -eq 0 ]; then
show_help >&2
exit 1
fi
magick "$@" -density $D -page %[fx:$W/25.4*$D]x%[fx:$H/25.4*$D] -resize %[fx:page.width]x%[fx:page.height]! out.pdf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment