Skip to content

Instantly share code, notes, and snippets.

@gildotdev
Created October 27, 2010 17:13
Show Gist options
  • Save gildotdev/649485 to your computer and use it in GitHub Desktop.
Save gildotdev/649485 to your computer and use it in GitHub Desktop.
Simple Bash script to create a rich text format file from a plain text file
#!/bin/sh
#check to see if arguments are set
E_BADARGS=65
if [ ! -n "$1" ]
then
echo "Usage: `basename $0` textfilename [rtffilename optional]"
exit $E_BADARGS
fi
#Set font face
#font="Courier"
font="Arial"
#font="Times New Roman"
#Set font size in pt
fontsize=12
#Set document height in inches
height=11 #letter
#height=14 #legal
#Set document width in inches
width=8.5
#Set document orientation
orientation="portrait"
#orientation="landscape"
#set document margins in inches
leftm=0.5
rightm=0.5
topm=0.5
bottomm=0.5
##################################
#NOTHING BELOW NEEDS TO BE EDITED#
##################################
#calculate rtf sizes
fontsize=$(echo "$fontsize*2" | bc | sed 's/\.[^.]*$//')
height=$(echo "$height*1440" | bc | sed 's/\.[^.]*$//')
width=$(echo "$width*1440" | bc | sed 's/\.[^.]*$//')
leftm=$(echo "$leftm*1440" | bc | sed 's/\.[^.]*$//')
rightm=$(echo "$rightm*1440" | bc | sed 's/\.[^.]*$//')
topm=$(echo "$topm*1440" | bc | sed 's/\.[^.]*$//')
bottomm=$(echo "$bottomm*1440" | bc | sed 's/\.[^.]*$//')
#set output filename
if [ ! -n "$2" ]
then
fname=`echo $1 | sed 's/\.[^.]*$//'`.rtf
else
fname=`echo $2`
fi
#start header
echo "{\rtf1\ansi\deff0 {\fonttbl {\f0 $font;}}" > $fname
echo "\paperh$height \paperw$width" >> $fname
echo "\margl$leftm \margr$rightm \margt$topm \margb$bottomm" >> $fname
echo "\f0\fs$fontsize" >> $fname
#add \line to the end of each line and replace an form feed characters
#with \page for page breaks
sed s/\$/'\\line'/ $1 | sed s/\\f/'\\page'/ >> $fname
#close rtf file
echo "}" >> $fname
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment