7
# 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
28
geometry: margin=0.5cm
30
mainfont: DejaVu Serif
43
echo "process_text_to_image.sh - Takes one text file and convernts it to a single"
44
echo "image using pandoc, xelatex, imagemagick, pdftoppm, pdfcrop"
46
echo "!IMPORTANT! The folder \"./tmp/\" in the current working directory will be"
47
echo " used as a temporary storage, and may be deleted, along with it's"
50
echo "---------------------"
53
echo " Print this help message"
55
echo "-i <file> --input <file>"
56
echo " The file to use to convert to an image. "
58
echo "-o <file> --output <file>"
59
echo " The image to output to. (Default=big_image.png)"
61
echo "-d <integer> --dpi <integer>"
62
echo " Set the dpi of the intermediate image relative to an a5 paper."
66
echo " Do not delete the TMP folder."
69
echo " Invert the colours of the final image."
71
echo "-t \"name\" --title \"name\""
72
echo " Set the title on the the title page."
75
echo " Do not insert the pandoc header. (Default:"
76
echo "$_PANDOC_HEADER"
79
echo "---------------------"
81
echo "If you are getting an error from convert that the height or width exeeds"
82
echo "some value, you may want to check the ImageMagick policy.xml file."
84
echo "The path to ImageMagick policy file is:"
85
convert -list policy | grep .xml
87
echo "---------------------"
95
#ESCAPED_CWD=$(echo ${CWD} | sed 's/ /\\ /g' | sed "s/'/\\\'/g" )
97
echo "IN_FILE\: $IN_FILE"
98
echo "OUT_FILE\: $OUT_FILE"
101
#echo "ESCAPED_CWD\: $ESCAPED_CWD"
103
if [ ! -e "$CWD/$IN_FILE" ]
105
echo "!!!in file does not exist!!!"
111
# first we create a temp folder.
114
#next we want to copy our file into it.
115
cp "$CWD/$IN_FILE" "$CWD/tmp/text.txt"
118
# Now we can start the work for this.
119
if [ $_NO_PANDOC_HEADER = false ]
121
# 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"
126
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"
131
printf '%s' "---" "$(cat "$CWD/tmp/text.txt")" > "$CWD/tmp/text.txt"
134
# 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"
137
pdfcrop --margins '10 5 10 5' "$CWD/tmp/text.pdf" "$CWD/tmp/text-croped.pdf"
139
# Convert it to images
140
pdftoppm "$CWD/tmp/text-croped.pdf" "$CWD/tmp/page" -png -rx $DPI -ry $DPI -gray
142
# 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"
147
# 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"
163
echo "Try --help or -h."
202
_NO_PANDOC_HEADER=true
226
if [ $PERSERVE_TMP = true ]
228
echo "Not cleaning up!"