14
15
# * fixed up the if statments.
27
_NO_PANDOC_HEADER=false
25
__INVERT_COLOURS=false
26
__NO__PANDOC_HEADER=false
32
31
geometry: margin=0.5cm
34
33
mainfont: DejaVu Serif
47
46
echo "process_text_to_image.sh - Takes one text file and convernts it to a single"
48
47
echo "image using pandoc, xelatex, imagemagick, pdftoppm, pdfcrop"
62
61
echo "-o <file> --output <file>"
63
62
echo " The image to output to. (Default=big_image.png)"
65
echo "-d <integer> --dpi <integer>"
64
echo "-d <integer> -- dpi <integer>"
66
65
echo " Set the dpi of the intermediate image relative to an a5 paper."
67
66
echo " (Default=300)"
91
90
echo "---------------------"
99
94
#ESCAPED_CWD=$(echo ${CWD} | sed 's/ /\\ /g' | sed "s/'/\\\'/g" )
101
echo "IN_FILE\: $IN_FILE"
102
echo "OUT_FILE\: $OUT_FILE"
96
echo "__IN_FILE\: $__IN_FILE"
97
echo "__OUT_FILE\: $__OUT_FILE"
105
100
#echo "ESCAPED_CWD\: $ESCAPED_CWD"
107
if [[ ! -e "$CWD/$IN_FILE" ]]
102
if [[ ! -e "$__CWD/$__IN_FILE" ]]
109
104
echo "!!!in file does not exist!!!"
115
110
# first we create a temp folder.
111
mkdir -p "$__CWD/tmp"
118
113
#next we want to copy our file into it.
119
cp "$CWD/$IN_FILE" "$CWD/tmp/text.txt"
114
cp "$__CWD/$__IN_FILE" "$__CWD/tmp/text.txt"
122
117
# Now we can start the work for this.
123
if [[ $_NO_PANDOC_HEADER == false ]]
118
if [[ $__NO__PANDOC_HEADER == false ]]
125
120
# We add a special header to the file to make it pandoc know what to do.
127
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"
130
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"
133
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"
135
printf '%s' "---" "$(cat "$CWD/tmp/text.txt")" > "$CWD/tmp/text.txt"
130
printf '%s' "---" "$(cat "$__CWD/tmp/text.txt")" > "$__CWD/tmp/text.txt"
138
133
# Now we use pandoc to do to convert it to a PDF.
139
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"
141
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"
143
138
# Convert it to images
144
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
146
141
# convert make the colour space greyscale and the append to each other
147
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"
151
146
# If we invert the final image this is where we do it.
152
if [[ $INVERT_COLOURS == true ]]
154
convert "$CWD/tmp/big-page.png" -channel RGB -negate "$CWD/tmp/big-page-inverted.png"
155
FINAL_IMAGE="$CWD/tmp/big-page-inverted.png"
157
FINAL_IMAGE="$CWD/tmp/big-page.png"
160
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 () {
167
172
echo "Try --help or -h."