-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdf2pdfa
More file actions
executable file
·176 lines (136 loc) · 3.21 KB
/
Copy pathpdf2pdfa
File metadata and controls
executable file
·176 lines (136 loc) · 3.21 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env bash
set -euo pipefail
usage() {
cat <<'EOF'
Usage:
pdf2pdfa.sh [options] INPUT.pdf
Convert a PDF to PDF/A by first converting PDF -> PS with pdftops,
then PS -> PDF/A with Ghostscript.
Options:
-o, --output FILE Output PDF/A file path
Default: INPUT_a.pdf
--keep-ps Keep intermediate .ps file
Default: delete it after conversion
--ps-output FILE Explicit intermediate .ps path
Default: INPUT.ps
--color-model MODEL Ghostscript process color model
Default: DeviceCMYK
Common values: DeviceCMYK, DeviceRGB, DeviceGray
--policy N PDF/A compatibility policy
Default: 1
-h, --help Show this help message
Examples:
pdf2pdfa.sh document.pdf
pdf2pdfa.sh -o document_pdfa.pdf document.pdf
pdf2pdfa.sh --keep-ps document.pdf
pdf2pdfa.sh --color-model DeviceRGB document.pdf
EOF
}
print_error() {
echo "Error: $*" >&2
#exit 1
}
die() {
echo "Error: $*" >&2
exit 1
}
require_cmd() {
command -v "$1" >/dev/null 2>&1 || echo "Required command not found: $1" || usage
}
pdf_input=""
pdfa_output=""
ps_output=""
keep_ps=false
color_model="DeviceCMYK"
policy="1"
while [[ $# -gt 0 ]]; do
case "$1" in
-o|--output)
[[ $# -ge 2 ]] || print_error "$1 requires a file path"
pdfa_output="$2"
shift 2
;;
--ps-output)
[[ $# -ge 2 ]] || print_error "$1 requires a file path"
ps_output="$2"
shift 2
;;
--keep-ps)
keep_ps=true
shift
;;
--color-model)
[[ $# -ge 2 ]] || print_error "$1 requires a value"
color_model="$2"
shift 2
;;
--policy)
[[ $# -ge 2 ]] || print_error "$1 requires a value"
policy="$2"
shift 2
;;
-h|--help)
usage
exit 0
;;
--)
shift
break
;;
-*)
print_error "Unknown option: $1";
usage;
;;
*)
if [[ -n "$pdf_input" ]]; then
print_error "Only one input PDF may be provided"
fi
pdf_input="$1"
shift
;;
esac
done
[[ -n "$pdf_input" ]] || {
usage >&2
exit 1
}
[[ -f "$pdf_input" ]] || print_error "Input file does not exist: $pdf_input"
case "$pdf_input" in
*.pdf|*.PDF) ;;
*) print_error "Input file must be a PDF: $pdf_input" ;;
esac
require_cmd pdftops
require_cmd gs
base="${pdf_input%.*}"
if [[ -z "$ps_output" ]]; then
ps_output="${base}.ps"
fi
if [[ -z "$pdfa_output" ]]; then
pdfa_output="${base}_a.pdf"
fi
if [[ "$pdfa_output" == "$pdf_input" ]]; then
print_error "Output file must not overwrite input file"
fi
cleanup() {
if [[ "$keep_ps" == false && -f "$ps_output" ]]; then
rm -f "$ps_output"
fi
}
trap cleanup EXIT
echo "Converting PDF to PS:"
echo " $pdf_input -> $ps_output"
pdftops "$pdf_input" "$ps_output"
echo "Converting PS to PDF/A:"
echo " $ps_output -> $pdfa_output"
gs \
-dPDFA \
-dBATCH \
-dNOPAUSE \
-dNOOUTERSAVE \
-sProcessColorModel="$color_model" \
-sDEVICE=pdfwrite \
-dPDFACompatibilityPolicy="$policy" \
-sOutputFile="$pdfa_output" \
"$ps_output"
echo "Done:"
echo " $pdfa_output"