-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathredo_ocr_PDF.sh
More file actions
executable file
·159 lines (137 loc) · 3.91 KB
/
Copy pathredo_ocr_PDF.sh
File metadata and controls
executable file
·159 lines (137 loc) · 3.91 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
#!/usr/bin/env bash
me=$USER
datestring=$(date +%Y-%m-%d_%H.%M.%S)
dir_path=$(dirname $0)
# Get length so final finalname is not too long
tag="orig"
maxl=$(( 255 - ${#tag} ))
tmpdir=$(echo ${TMPDIR:-/tmp/})
keepfirst=false
lang=""
# Read flags
# Provide help if no argument
if [[ $# == 0 ]]
then
set -- "-h"
fi
while test $# -gt 0; do
case "$1" in
-h|--help)
echo ""
echo "This script will remove existing text in a PDF and then perform OCR and"
echo "add that text to the original."
echo ""
echo "The original file is left alone and a new file is created with a datestamped name."
echo ""
echo "options:"
echo "-h, --help Show this brief help."
echo "-f, --first Keep the first page of the original PDF."
echo "-l language Use this language for OCR (defaults to English)"
echo " Use one of tesserat's 3-letter codes for this"
exit 0
;;
-f|--first)
shift
first=true
;;
-l)
shift
lang=$1
shift
;;
*)
break
;;
esac
done
input="$1"
input_extension="${1##*.}"
if [[ "$1" == "" ]]
then
echo "You must enter a filename."
exit 0
fi
# Make sure file exists
if [[ ! -s "$input" ]]
then
echo ''
echo "\a"File \""$input"\" does not appear to exist or has no content! Exiting...
echo ''
exit 0
fi
# Make sure it's a PDF
if [[ $input_extension != 'pdf' ]]
then
echo ''
echo -e "\a"File "$input" does not appear to be a PDF. Exiting...
echo ''
exit 0
fi
# Check for language. Easy to forget to specify.
# Needs some error checking
if [[ $lang == '' ]]
then
read -p 'No language was specified. Hit enter to use English or supply the 3-letter language code: ' langInput
if [[ $langInput == '' ]]
then
lang='eng'
else
lang=$langInput
fi
fi
# Get final output file name
saved=$(basename "$input")
saved=$(echo ${saved%.*})
saved=$(echo ${saved:0:$maxl})
saved="$saved"_"$tag".pdf
saveddir=$(dirname "$input")
input_new="$input"
# If desired, remove the first page right away and save a little time
if [[ $first ]]
then
input_new="$tmpdir"input.pdf
qpdf "$input" --pages . 2-z -- "$input_new"
fi
# Strip metadata from input file
input_clean="$input_new"
# exiftool won't overwrite an existing file, so give output a unique name
input_clean="$tmpdir"input_"$datestring".pdf
exiftool -q -q "$input_new" -all='' -o "$input_clean"
# strip text from the PDF
source "/Users/$me/.venv/bin/activate"
python3 "$dir_path/remove_PDF_text.py" "$input_clean" "$tmpdir/no_text.pdf"
#gs -o "$tmpdir/no_text.pdf" -dFILTERTEXT -sDEVICE=pdfwrite "$input_new"
# Make sure output file exists
if [[ ! -s "$tmpdir/no_text.pdf" ]]
then
echo ''
echo "\a"Work file \""$tmpdir/no_text.pdf"\" does not appear to exist or has no content! Exiting...
echo ''
exit 0
fi
echo "Starting ocrmypdf"
# ocr the original pdf, skipping optimization since we're throwing away the images
ocrmypdf --force-ocr --output-type pdf --optimize 0 -l $lang "$tmpdir/no_text.pdf" "$tmpdir/ocr_output.pdf"
echo "Cleaning ocrmypdf result"
#strip images from that result
gs -o "$tmpdir/textonly.pdf" -dFILTERIMAGE -dFILTERVECTOR -sDEVICE=pdfwrite "$tmpdir/ocr_output.pdf"
echo "Overlaying ocr text"
# overlay ocr text on file stripped of text
qpdf "$tmpdir/no_text.pdf" --overlay "$tmpdir/textonly.pdf" -- "$tmpdir/final.pdf"
# Restore the original metadata, if any
exiftool -tagsfromfile "$input" -title -author "$tmpdir/final.pdf"
# Remove some added metadata while we're at it
exiftool -Producer='' -XMPToolkit='' -Creator='' "$tmpdir/final.pdf"
# Rename original file to save it
mv "$input" "$saveddir"/"$saved"
# replace first page if required
if [ $first ]
then
qpdf "$tmpdir/final.pdf" --pages "$saveddir"/"$saved" 1 . 1-z -- "$input"
else
mv "$tmpdir/final.pdf" "$input"
fi
if [[ $(uname) == 'Darwin' ]]
then
terminal-notifier -message "Your OCR is complete." -title "Yay!" -sound default
fi