-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtinypdf-script-poc
More file actions
executable file
·79 lines (66 loc) · 2.78 KB
/
tinypdf-script-poc
File metadata and controls
executable file
·79 lines (66 loc) · 2.78 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/bash
# Thanks to Mickaël
# src: https://superuser.com/a/1217306
# gs options:
# -dPDFSETTINGS=/screen (screen-view-only quality, 72 dpi images)
# -dPDFSETTINGS=/ebook (low quality, 150 dpi images)
# -dPDFSETTINGS=/printer (high quality, 300 dpi images)
# -dPDFSETTINGS=/prepress (high quality, color preserving, 300 dpi imgs)
# -dPDFSETTINGS=/default (almost identical to /screen)
pdftrim_usage() {
printf "%s\n" 'Usage: pdftrim [input file] [output file] [screen|ebook|printer|prepress]'
printf "\n%s\n\n" 'Example: pdftrim input.pdf output.pdf ebook'
printf "%s\n" "Trim Options:"
printf "%s\n" " screen - Screen-view-only quality, 72 dpi images"
printf "%s\n" " ebook - Low quality, 150 dpi images"
printf "%s\n" " printer - High quality, 300 dpi images"
printf "%s\n" " prepress - High quality, color preserving, 300 dpi images"
printf "%s\n" " default - Almost identical to /screen"
}
pdftrim() {
if [ $# -lt 2 ]; then
printf "%s\n" 'Error: Not enough arguments provided.'
pdftrim_usage
return 1
fi
if [ ! -f "$1" ]; then
printf "%s\n" "Error: Input file '$1' does not exist."
pdftrim_usage
return 1
fi
if [ -z "$2" ]; then
printf "%s\n" 'Error: Output file must be specified.'
pdftrim_usage
return 1
fi
# if not specified, use "screen" as default
if [ -z "$3" ]; then
printf "\n%s\n" 'Warning: No quality setting specified, using "screen" as default.'
fi
# gs -sDEVICE=pdfwrite -dNOPAUSE -dQUIET -dBATCH -dPDFSETTINGS=/${3:-"screen"} -dCompatibilityLevel=1.4 -sOutputFile="$2" "$1"
#TODO: ask user for gray scale or color conversion based on that try filters
# [EXPERIMENT] Use the following command to build a opinionated PDF based on the input file
# pdfimages -list file.pdf | awk '$1 ~ /^[0-9]+$/ && $7 != 1 { found=1 } END { print (found ? "Color" : "Grayscale") }'
# Download pdfimages @ https://poppler.freedesktop.org/
# Docs: https://ghostscript.readthedocs.io/en/latest/VectorDevices.html#distiller-parameters
gs -sDEVICE=pdfwrite \
-dCompatibilityLevel=1.4 \
-dPDFSETTINGS=/${3:-"screen"} \
-dNOPAUSE -dQUIET -dBATCH \
-dDownsampleColorImages=true \
-dColorImageResolution=200 \
-dColorImageDownsampleType=/Bicubic \
-dDownsampleMonoImages=true \
-dMonoImageResolution=700 \
-dMonoImageDownsampleType=/Subsample \
-dDownsampleGrayImages=true \
-dGrayImageDownsampleType=/Bicubic \
-dGrayImageResolution=150 \
-dColorConversionStrategy=/RGB \
-sOutputFile="$2" "$1"
}
if ! command -v gs &>/dev/null; then
printf "%s\n" 'Error: Ghostscript (gs) is not installed.'
return 1
fi
pdftrim "$@"