-
Notifications
You must be signed in to change notification settings - Fork 5
/
redo_ocr_PDF.sh
executable file
·149 lines (130 loc) · 3.7 KB
/
redo_ocr_PDF.sh
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
#!/usr/bin/env bash
datestring=`date +%Y-%m-%d_%H.%M.%S`
dir_path=$(dirname $0)
# Get length so final finalname is not too long
ldate=`echo ${#datestring}`
maxl=`expr 255 - $ldate`
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 it 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 "\aYou 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
final=`basename "$input"`
final=`echo ${final%.*}`
final=`echo ${final:0:$maxl}`
final="$final"_"$datestring".pdf
origdir=`dirname "$input"`
inputold="$input"
# If desired, remove the first page right away and save a little time
if [[ $first ]]
then
input="$tmpdir"input_new.pdf
qpdf "$inputold" --pages . 2-z -- "$input"
fi
# Strip metadata from input file
inputclean="$input"
# exiftool won't overwrite an existing file, so give output a unique name
input="$tmpdir"input_"$datestring".pdf
exiftool -q -q "$inputclean" -all='' -o "$input"
# strip text from the PDF
python3 "$dir_path/remove_PDF_text.py" "$input" "$tmpdir/no_text.pdf"
#gs -o "$tmpdir/no_text.pdf" -dFILTERTEXT -sDEVICE=pdfwrite "$input"
# Make sure output file exists
if [[ ! -s "$tmpdir/no_text.pdf" ]]
then
echo ''
echo "\a"Work file \""$input"\" does not appear to exist or has no content! Exiting...
echo ''
exit 0
fi
# 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"
#strip images from that result
gs -o "$tmpdir/textonly.pdf" -dFILTERIMAGE -dFILTERVECTOR -sDEVICE=pdfwrite "$tmpdir/ocr_output.pdf"
# 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 "$inputold" -title -author "$tmpdir/final.pdf"
# Remove some added metadata while we're at it
exiftool -Producer='' -XMPToolkit='' "$tmpdir/final.pdf"
# replace first page if required
if [ $first ]
then
qpdf "$tmpdir/final.pdf" --pages "$inputold" 1 . 1-z -- "$origdir"/"$final"
else
mv "$tmpdir/final.pdf" "$origdir"/"$final"
fi
if [[ $(uname) == 'Darwin' ]]
then
terminal-notifier -message "Your OCR is complete." -title "Yay!" -sound default
fi