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