3
# FILE NAME process_text_to_image.sh
7
# added --no-header for when you want to use your own pandoc header
8
# * added --no-header for when you want to use your own pandoc header
10
# Fixed --no-header... Seemed to have forgotten the "$" infront of the variable.
23
_NO_PANDOC_HEADER=false
11
# * Fixed --no-header...
12
# Seemed to have forgotten the "$" infront of the variable.
15
# * fixed up the if statments.
25
__INVERT_COLOURS=false
26
__NO__PANDOC_HEADER=false
28
31
geometry: margin=0.5cm
30
33
mainfont: DejaVu Serif
43
46
echo "process_text_to_image.sh - Takes one text file and convernts it to a single"
44
47
echo "image using pandoc, xelatex, imagemagick, pdftoppm, pdfcrop"
58
61
echo "-o <file> --output <file>"
59
62
echo " The image to output to. (Default=big_image.png)"
61
echo "-d <integer> --dpi <integer>"
64
echo "-d <integer> -- dpi <integer>"
62
65
echo " Set the dpi of the intermediate image relative to an a5 paper."
63
66
echo " (Default=300)"
87
90
echo "---------------------"
95
94
#ESCAPED_CWD=$(echo ${CWD} | sed 's/ /\\ /g' | sed "s/'/\\\'/g" )
97
echo "IN_FILE\: $IN_FILE"
98
echo "OUT_FILE\: $OUT_FILE"
96
echo "__IN_FILE\: $__IN_FILE"
97
echo "__OUT_FILE\: $__OUT_FILE"
101
100
#echo "ESCAPED_CWD\: $ESCAPED_CWD"
103
if [ ! -e "$CWD/$IN_FILE" ]
102
if [[ ! -e "$__CWD/$__IN_FILE" ]]
105
104
echo "!!!in file does not exist!!!"
111
110
# first we create a temp folder.
111
mkdir -p "$__CWD/tmp"
114
113
#next we want to copy our file into it.
115
cp "$CWD/$IN_FILE" "$CWD/tmp/text.txt"
114
cp "$__CWD/$__IN_FILE" "$__CWD/tmp/text.txt"
118
117
# Now we can start the work for this.
119
if [ $_NO_PANDOC_HEADER = false ]
118
if [[ $__NO__PANDOC_HEADER == false ]]
121
120
# We add a special header to the file to make it pandoc know what to do.
123
printf '%s\n' "---" "$(cat "$CWD/tmp/text.txt")" > "$CWD/tmp/text.txt"
122
printf '%s\n' "---" "$(cat "$__CWD/tmp/text.txt")" > "$__CWD/tmp/text.txt"
126
printf '%s\n' "title: ${TITLE}" "$(cat "$CWD/tmp/text.txt")" > "$CWD/tmp/text.txt"
125
printf '%s\n' "title: ${__TITLE}" "$(cat "$__CWD/tmp/text.txt")" > "$__CWD/tmp/text.txt"
129
printf '%s' "$_PANDOC_HEADER" "$(cat "$CWD/tmp/text.txt")" > "$CWD/tmp/text.txt"
128
printf '%s' "$__PANDOC_HEADER" "$(cat "$__CWD/tmp/text.txt")" > "$__CWD/tmp/text.txt"
131
printf '%s' "---" "$(cat "$CWD/tmp/text.txt")" > "$CWD/tmp/text.txt"
130
printf '%s' "---" "$(cat "$__CWD/tmp/text.txt")" > "$__CWD/tmp/text.txt"
134
133
# Now we use pandoc to do to convert it to a PDF.
135
pandoc --pdf-engine=xelatex "$CWD/tmp/text.txt" -o "$CWD/tmp/text.pdf"
134
pandoc --pdf-engine=xelatex "$__CWD/tmp/text.txt" -o "$__CWD/tmp/text.pdf"
137
pdfcrop --margins '10 5 10 5' "$CWD/tmp/text.pdf" "$CWD/tmp/text-croped.pdf"
136
pdfcrop --margins '10 5 10 5' "$__CWD/tmp/text.pdf" "$__CWD/tmp/text-croped.pdf"
139
138
# Convert it to images
140
pdftoppm "$CWD/tmp/text-croped.pdf" "$CWD/tmp/page" -png -rx $DPI -ry $DPI -gray
139
pdftoppm "$__CWD/tmp/text-croped.pdf" "$__CWD/tmp/page" -png -rx $__DPI -ry $__DPI -gray
142
141
# convert make the colour space greyscale and the append to each other
143
convert -append -colorspace gray +matte -depth 8 "$CWD/tmp/page-*.png" "$CWD/tmp/big-page.png"
142
convert -append -colorspace gray +matte -depth 8 "$__CWD/tmp/page-*.png" "$__CWD/tmp/big-page.png"
147
146
# If we invert the final image this is where we do it.
148
if [ $INVERT_COLOURS = true ]
150
convert "$CWD/tmp/big-page.png" -channel RGB -negate "$CWD/tmp/big-page-inverted.png"
151
FINAL_IMAGE="$CWD/tmp/big-page-inverted.png"
153
FINAL_IMAGE="$CWD/tmp/big-page.png"
156
cp "$FINAL_IMAGE" "$CWD/$OUT_FILE.png"
147
if [[ $__INVERT_COLOURS == true ]]
149
convert "$__CWD/tmp/big-page.png" -channel RGB -negate "$__CWD/tmp/big-page-inverted.png"
150
FINAL_IMAGE="$__CWD/tmp/big-page-inverted.png"
152
FINAL_IMAGE="$__CWD/tmp/big-page.png"
155
cp "$FINAL_IMAGE" "$__CWD/$__OUT_FILE.png"
158
# Cleanup of eveything.
160
if [[ $__PERSERVE_TMP == true ]]
162
echo "Not cleaning up!"
169
function __parse_args () {
163
172
echo "Try --help or -h."