aboutsummaryrefslogtreecommitdiffstats
path: root/cli/jpg2pdf.sh
blob: ccdd4d73791227ea90d6f2944d26cbb4d3fa574a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/bin/sh
#
# Copyright (c) 2019 Aaron LI <aly@aaronly.me>
# MIT License
#
# Convert a set of JPEG files to a multipage PDF using Ghostscript and
# ImageMagick (requires the "identify" command).
#
# Credit: pipitas@gmail.com
#

if [ $# -lt 2 ]; then
    echo "usage: ${0##*/} output.pdf 1.jpg ..."
    exit 1
fi

outfile="$1"
shift

fcmd=$(mktemp)
N=$#
i=0
echo "Total images: ${N}"
while [ -n "$1" ]; do
    f="$1"; shift; i=$((${i} + 1))
    echo -n "${i}.."
    dim=$(identify -format "%[fx:(w)] %[fx:(h)]" "${f}")
    echo "<</PageSize [${dim}]>> setpagedevice (${f}) viewJPEG showpage" \
        >> ${fcmd}
done

gs \
  -sDEVICE=pdfwrite \
  -dPDFSETTINGS=/prepress \
  -o "${outfile}" \
  viewjpeg.ps \
  -f ${fcmd}

rm -f ${fcmd}
echo "Output file: ${outfile}"