-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdfrasterize
More file actions
executable file
·44 lines (38 loc) · 1.19 KB
/
pdfrasterize
File metadata and controls
executable file
·44 lines (38 loc) · 1.19 KB
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
42
43
44
#!/bin/sh
if [ $# != 3 ]; then # If too many / too few paraters are given, print help
echo "Usage: $0 fromfile.pdf tofile.pdf dpi" >&2
echo 'This makes a rasterized version of fromfile.pdf, optimizes it with ghostscript and outputs into into tofile.pdf' >&2
exit 1
fi
if [ ! -f "$1" ]; then # Check input file paramter
echo 'Inputfile does not exist' >&2
exit 1
fi
if [ -f "$2" ]; then # Check output file paramter
echo 'Outputfile does already exist' >&2
exit 1
fi
if ! echo -n "$3" | grep -Pq '^\d+$'; then # Check dpi paramter
echo 'DPI Paramter is not a number' >&2
exit 1
fi
# Create temporary file
TMPFILE="$(mktemp --suffix=.pdf)"
# Rasterize image with image magick
echo "Creating raster version... (in $TMPFILE)"
if ! convert -render -density "$3" "$1" "$TMPFILE"; then
echo 'Imagemagick error' >&2
exit 1
fi
# Shrink pdf with ghostscript
echo "Optimizing to shrink pdf file..."
if ! gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dNOPAUSE -dQUIET -dBATCH -sOutputFile="$2" "$TMPFILE"; then
echo 'Ghostscript error' >&2
exit 1
fi
echo "Deleting $TMPFILE ..."
if ! env rm "$TMPFILE"; then
echo "Error while removing temporary file $TMPFILE" >&2
exit 1
fi
echo 'Finished without errors'