-
Notifications
You must be signed in to change notification settings - Fork 1
/
pdfmetaedit
executable file
·75 lines (66 loc) · 1.44 KB
/
pdfmetaedit
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
#!/usr/bin/env bash
#
# NAME
#
# pdfmetaedit - edit metadata of PDF files
#
# SYNOPSIS
#
# pdfmetaedit [-a] <file>
#
# OPTION
#
# -a use the filename as the PDF title
#
# DESCRIPTION
#
# pdfmetaedit is a wrapper script around PDFtk that will let you edit the
# metadata (e.g., "subject", "title", etc.) of PDF files using $EDITOR.
#
# DEPENDENCIES
#
# pdftk(1) and mktemp(1)
#
set -eu
set -o pipefail
tmpdir="$(mktemp -d)"
trap 'rm -rf "$tmpdir" &>/dev/null' EXIT
trap 'exit 2' HUP INT QUIT TERM
auto="no"
while getopts ":a" opt
do
case "$opt" in
a) auto="yes" ;;
\?) echo >&2 "${0##*/}: -$OPTARG is not a valid option\n"
exit 1 ;;
esac
done
shift $(( OPTIND - 1 ))
[[ "$*" ]] || {
echo >&2 "usage: ${0##*/} [-a] <file>"
exit 1
}
meta="${tmpdir}/meta.txt"
if [[ "$auto" == "yes" ]]
then
cat >"$meta" <<META
InfoBegin
InfoKey: Title
InfoValue: $1
META
else
pdftk "$1" dump_data_utf8 | grep '^Info' | sed '/^InfoBegin/! { s/^/ /; }' >"$meta"
# Generate these default keys if they're not available.
for key in "Author" "Subject" "Title" "Keywords"
do
grep -q "InfoKey: ${key}" "$meta" || {
echo "InfoBegin"
echo " InfoKey: ${key}"
echo " InfoValue:"
} >>"$meta"
done
"${EDITOR:-vi}" "$meta"
fi
pdf="${tmpdir}/updated.pdf"
pdftk "$1" update_info_utf8 <(sed 's/^ *Info/Info/' "$meta") output "$pdf"
mv -f "$pdf" "$1"